为了保护Eureka 的服务器的安全性,不行被其它微服务注册,我们可以对Eureka 设置一层密码,使只有知道Eureka 账号密码的才可以连接,当然如果是公司内网的话,可以不设置密码,那么如何实现呢,下面就来演示。
1.修改Eureka配置
修改fw-register-eureka注册服务的maven,添加已下依赖,至于Security 的其他使用功能,后续章节会讲到。
<code><dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-security/<artifactid>
/<dependency>
/<code>
2.修改Eureka应用配置
固定用户名和密码,否则每次密码都会是随机的,其它服务也注册不上来。
<code>spring:
security:
user:
name: fwcloud
password: 123455
/<code>
3.添加Security配置类
主要是因为高版本丢弃了security. basic.enabled= true配置,并且需要注意需要设置httpBasic(),否则不支持http://${user}:${password}@${host}:${port}/eureka/ 这种方式登录
<code>@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic().and().sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.NEVER)
.and().authorizeRequests().anyRequest().authenticated()
.and().csrf().disable();
}
}
/<code>
2.重启Eureka
浏览器输入http://localhost:8761会弹出一个登录框
输入用户名fwcloud,密码123456即可跳转我们熟悉的页面
3.客户端注册
客户端做简要修改即可
<code>eureka:
client:
service-url:
defaultZone: http://fwcloud:123456@localhost:8761/eureka
/<code>
4. 启动客户端
可以看到,已经成功注册上来了
微服务更多其它使用请可以Git搜索fw-spring-cloud
閱讀更多 奮戰攻城獅 的文章