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.