Eureka 服务加入安全认证只需要在之前的服务中增加二处步骤即可:
- 在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>
</project>
- 修改配置文件
security:
basic:
enabled: true
user:
name: miaozhihao
password: 123456
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://miaozhihao:123456@${eureka.instance.hostname}:${server.port}/eureka/
启动Eureka服务:
输入配置文件中配置的用户名和密码即可。
附上一份Eureka客户端的配置,
spring:
application:
name: order-service
eureka:
client:
service-url:
defaultZone: http://miaozhihao:123456@localhost:8761/eureka
instance:
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
server:
port: 9090
user:
service:
url: http://user-service
就改变一处eureka.client.