![Eureka 密碼訪問](http://p2.ttnews.xyz/loading.gif)
為了保護Eureka 的服務器的安全性,不行被其它微服務註冊,我們可以對Eureka 設置一層密碼,使只有知道Eureka 賬號密碼的才可以連接,當然如果是公司內網的話,可以不設置密碼,那麼如何實現呢,下面就來演示。
1.修改Eureka配置
修改fw-register-eureka註冊服務的maven,添加已下依賴,至於Security 的其他使用功能,後續章節會講到。
<code><dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-security/<artifactid>
/<dependency>
/<code>
2.修改Eureka應用配置
固定用戶名和密碼,否則每次密碼都會是隨機的,其它服務也註冊不上來。
<code>spring:
security:
user:
name: fwcloud
password: 123455
/<code>
3.添加Security配置類
主要是因為高版本丟棄了security. basic.enabled= true配置,並且需要注意需要設置httpBasic(),否則不支持http://${user}:${password}@${host}:${port}/eureka/ 這種方式登錄
<code>@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic().and().sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.NEVER)
.and().authorizeRequests().anyRequest().authenticated()
.and().csrf().disable();
}
}
/<code>
2.重啟Eureka
瀏覽器輸入http://localhost:8761會彈出一個登錄框
![Eureka 密碼訪問](http://p2.ttnews.xyz/loading.gif)
輸入用戶名fwcloud,密碼123456即可跳轉我們熟悉的頁面
3.客戶端註冊
客戶端做簡要修改即可
<code>eureka:
client:
service-url:
defaultZone: http://fwcloud:123456@localhost:8761/eureka
/<code>
4. 啟動客戶端
可以看到,已經成功註冊上來了
微服務更多其它使用請可以Git搜索fw-spring-cloud
閱讀更多 奮戰攻城獅 的文章