对eureka注册中心做登录拦截

目标

针对eureka server

这样的方式只是针对新版的springcloud,就是版本好是年份的springcloud,之前以地铁站名命名的springcloud还是用以前的办法对eureka进行拦截

实现

1: 导入security依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

2:定义安全框架配置适配器

package com.pug.cloud.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    /**
     * @Description: 高版本的丢弃了
     * security:
     *   basic:
     *     enabled: true 配置,应该使用以下方式开启
     * @Param: [http]
     * @Return: void
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // Configure HttpSecurity as needed (e.g. enable http basic).
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
        http.csrf().disable();
        //注意:为了可以使用 http://${user}:${password}@${host}:${port}/eureka/ 这种方式登录,所以必须是httpBasic,
        // 如果是form方式,不能使用url格式登录
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }
}

3:application.yml配置

主要目的是:设置账号和密码

#端口
server:
  port: 10086

#服务id
spring:
  application:
    name: eureka-server
  security:
    user:
      password: 123456
      name: user

#配置eureka客户端
eureka:
  client:
    register-with-eureka: false  #不注册
    fetch-registry: false #不拉取
    service-url:
      defaultZone: http://user:123456@localhost:10086/eureka/  #告诉现在的注册中心的地址是什么

4:重启服务

5:客户端也必须把对应的地址指定和服务端一样

http://user:123456@localhost:10086/eureka/ 

主要是增加了:账号和密码的设定。

6:这样在访问注册中心的时候就会需要密码和账号了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lemon20120331

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值