1.在项目的pom.xml中添加spring-boot-starter-security依赖,该jar包为EurekaServer提供用户认证的能力
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2.在application.yml中添加用户和密码配置,注意name和password的写法
security:
basic:
enabled: true # 开启基于HTTP basic的认证
user:
name: user # 配置登录的账号是user
password: 123 # 配置登录的密码是 123
server:
port: 8763 # 指定该Eureka实例的端口
spring:
application:
name: microservice-discovery-eureka-authenticating #用于指定注册到Eureka Server上的应用名称
eureka:
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://${security.user.name}:${security.user.password}@peer1:8761/eureka/,http://${security.user.name}:${security.user.password}@peer2:8762/eureka/
3.这样就为EurekaServer添加了用户认证,启动服务,在浏览器访问:http://localhost:8763,页面就会弹出输入用户名和密码界面,如图所示:
4.输入设置的用户名和密码就可以登录Eureka Server