Eureka自帶了一個管理界面,若是不加密,全部人均可以進行訪問這個地址,這樣安全問題就來了,因此須要對其進行加密認證:web
那麼該如何進行整合呢:spring
1 在註冊中心模塊添加依賴:瀏覽器
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
2 yml文件配置:安全
spring: security: user: name: admin password: admin
3 啓動服務再次登陸嘗試:ide
以前是谷歌登陸,因此換了一個瀏覽器,須要再登陸,spring-boot
再次啓動註冊模塊,就出現一堆錯誤:微服務
那麼這個問題怎麼解決呢:ui
eureka: client: serviceUrl: defaultZone: http://admin:admin@127.0.0.1:8761/eureka/ #eureka註冊中心地址
在註冊服務上加上這個,名字,密碼就能夠了嗎?加密
再啓動仍是報一堆錯誤,服務根本註冊不進去:spa
能夠看到報錯信息:
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(); } } }