一 Eureka注冊中心認證:
Eureka自帶了一個管理界面,如果不加密,所有人都可以進行訪問這個地址,這樣安全問題就來了,所以需要對其進行加密認證:
那么該如何進行整合呢:
1 在注冊中心模塊添加依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
2 yml文件配置:
spring:
security:
user:
name: admin
password: admin
3 啟動服務再次登錄嘗試:
之前是谷歌登錄,所以換了一個瀏覽器,需要再登錄,
再次啟動注冊模塊,就出現一堆錯誤:
那么這個問題怎么解決呢:
eureka: client: serviceUrl: defaultZone: http://admin:admin@127.0.0.1:8761/eureka/ #eureka注冊中心地址
在注冊服務上加上這個,名字,密碼就可以了嗎?
再啟動還是報一堆錯誤,服務根本注冊不進去:
可以看到報錯信息:
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
百度查詢:
但是好像並沒有解決啊:
開啟認證了,但是沒有禁用CSRF
新版本的spring-cloud2.0中: Spring Security默認開啟了CSRF攻擊防御
CSRF會將微服務的注冊也給過濾了,雖然不會影響注冊中心,但是其他客戶端是注冊不了的
解決方案:
關閉csrf攻擊:
package com.cxy.config; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity; /*** * @ClassName: WebSecurityConfig * @Description: * @Auther: 陳緒友 * @Date: 2019/1/2820:34 * @version : V1.0 */ @Configuration public class WebSecurityConfig { @EnableWebSecurity public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().anyRequest().authenticated().and().httpBasic().and().csrf().disable(); } } }