一.書寫目的
正常來說一般內部訪問註冊中心是不需要設置Eureka登錄權限的,如果暴露到外部我們爲了我們的Eureka註冊中心,不背外部惡意使用,我們可以添加Eureka訪問登錄權限來控制服務註冊。
二.操作步驟
- 主要依賴添加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
- 配置文件關鍵配置
spring:
cloud:
config:
uri: http://localhost:8084
security:
user:
name: admin
password: 123456
bus:
enabled: true
trace:
enabled: true
eureka:
client:
service-url:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@localhost:8762/eureka/
fetch-registry: false
register-with-eureka: false
management:
endpoints:
web:
exposure:
include: info,bus-refresh
- 開啓安全認證
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
{
@Override
protected void configure(HttpSecurity http) throws Exception
{
//關閉csrf
http.csrf().disable();
//開啓認證
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
}
- 結果驗證
再次登錄Eureka我們必須輸入我們配置文件設置的密碼纔可以登錄。
三.客戶端登錄驗證
- 沒有加入用戶權限
- 加入用戶權限
我們的Eureka服務端權限認證添加成功!!!