Eureka 服务加入安全认证只需要在之前的服务中增加三处步骤即可:
1、在Eureka Server中加入spring-boot-starter-security依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
注意:只要加入了security包则自动开启了默认安全认证,密码随机生成,在控制台可以看到。
2、修改application.yml新增配置
spring:
application:
name: mima-cloud-eureka
server:
port: 8761
security:
basic:
#关闭安全验证
enabled: true
user:
name: ruthless
password: 123456
eureka:
instance:
#实例主机名
hostname: localhost
client:
#不向Eureka注册自己
register-with-eureka: false
#不检查其他的EurekaServer节点
fetch-registry: false
3、启动Eureka服务:
输入配置文件中配置的用户名和密码即可。
4、在Eureka Client中修改application.yml中的service-url,形式为下面的模式:
eureka.client.serviceUrl.defaultZone=http://${userName}:${password}@localhost:1111/eureka/
debug: false
spring:
application:
name: mima-cloud-eureka-producer
server:
port: 9907
eureka:
client:
serviceUrl:
#defaultZone: http://localhost:8761/eureka/
defaultZone: http://ruthless:123456@localhost:8761/eureka/
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${server.port}