当我们配置完 Spring Cloud Eureka 注册中心的时候,默认访问首页直接进入注册中心。
这样不管是谁,都可以直接进入太不安全,如果是在内网还好一点,如在外网,则把你的所有服务都暴露在外,非常不安全。
好在 Spring Cloud 已近为我们考虑到了这些,而且也有了解决方案,而且非常简单
第一步:增加 spring-boot-starter-security 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
第二步:修改 application.properties 增加以下配置
## 注意 spring boot 2.x 以后配置方式
spring.security.user.name=demo
spring.security.user.password=pwd123
## spring boot 2.x 以前的配置方式
security.user.name=zux2
security.user.password=zux2
重新启动,在浏览器访问注册中心就好出现以下需要登录的页面了。
表示用户名密码启用成功。
第三步:修改注册中心的注册地址。在注册地址上增加用户名和密码
eureka.client.serviceUrl.defaultZone=http://demo:pwd123@127.0.0.1:8081/eureka/
# 对比原来没有设置用户密码的写法区别
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8081/eureka/
到这里调整就OK了是不是很简单?