开启Eureka注册中心认证

1.目的描述

Eureka自带了一个Web的管理页面,方便我们查询注册到上面的实例信息,但是有一个问题:如果在实际使用中,注册中心地址有公网IP的话,必然能直接访问到,这样是不安全的。所以我们需要对Eureka进行改造,加上权限认证来保证安全性。

2.方式

改造eureka-server注册中心,通过集成Spring-Security来进行安全认证。
在pom.xml中添加Spring-Security的依赖包

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

在配置文件中添加配置:

spring:
  security:
    user:
      name: loong   #用户名
      password: 123456  #密码

增加Security配置类:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        //关闭csrf
        http.csrf().disable();
        //支持httpBasic
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }
}

重启注册中心后再次访问,此时浏览器会提示你输入用户名和密码,输入正确后才能继续访问Eureka提供的管理页面。

3.注意

在Eureka开启认证后,客户端注册的配置也要加上认证的用户名和密码信息:

eureka.client.serviceUrl.defaultZone=http://loong:123456@loacalhost:8761/eureka/

猜你喜欢

转载自www.cnblogs.com/gxloong/p/12364523.html
今日推荐