返回 导航

SpringBoot / Cloud

hangge.com

SpringCloud - 服务注册与发现组件Eureka的使用详解6(给注册中心添加密码认证)

作者:hangge | 2020-07-08 10:37
为了服务注册中心的安全考虑,很多时候我们都需要给服务注册中心加入安全校验,下面通过样例进行演示。

六、给注册中心添加密码认证

1,Eureka Server 配置

(1)编辑 server 端的 pom.xml 文件,添加安全认证 Spring Security 依赖:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

(2)在 application.properties 加入认证的用户名和密码:
spring.security.user.name=hangge
spring.security.user.password=123

(3)添加如下配置关闭 Spring Security CSRF 验证(如果不关闭,那么客户端就连接不上):
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable(); //关闭csrf
        super.configure(http);
    }
}

(4)启动项目,访问注册中心页面会发现首先要求我们输入用户名、密码:

2,Client 客户端接入设置

    如果服务注册中心像上面一样加入了安全验证,客户端这边在配置 serviceUrl 的时候,需要在 URL 中加入相应的安全校验信息(用户名、密码):
eureka.client.serviceUrl.defaultZone=http://hangge:123@localhost:1111/eureka/
评论

全部评论(0)

回到顶部