注冊中心配置了spring?security后客戶端啟動報錯
問題
注冊中心配置了security后, 報了 registration failed Cannot execute request on any known server 的錯誤, 原因是 2.1版本的security默認加上了 csrf 攔截, 所以需要通過重寫方法, 把csrf攔截禁用
解決
在啟動類上加上以下代碼(禁用csrf)即解決問題
@EnableWebSecurity
static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.httpBasic();
}
}完整代碼
/**
* @author 毛宇鵬
*/
@EnableEurekaServer
@SpringBootApplication(exclude={
DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class
})
public class RegisterApplication {
public static void main(String[] args) {
SpringApplication.run(RegisterApplication.class, args);
}
/**
* 2.1版本的security默認加上了 csrf 攔截, 所以需要通過重寫方法, 把csrf攔截禁用
* 參考: https://github.com/spring-cloud/spring-cloud-netflix/issues/2754
* <pre>
* This is because @EnableWebSecurity is now added by default when Spring Security is on the classpath.
* This enable CSRF protection by default. You will have the same problem in 1.5.10 if you add @EnableWebSecurity.
* One work around, which is not the most secure workaround if you have browsers using the Eureka dashboard, is to disable CSRF protection.
* This can be done by adding the following configuration to your app.
* </pre>
*/
@EnableWebSecurity
static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.httpBasic();
}
}
}以上就是注冊中心配置了spring security后客戶端啟動報錯的詳細內(nèi)容,更多關(guān)于注冊中心配置spring security報錯的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決java web應(yīng)用線上系統(tǒng)偶發(fā)宕機的情況
這篇文章主要介紹了解決java web應(yīng)用線上系統(tǒng)偶發(fā)宕機的情況,具有好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
關(guān)于pom.xml中maven無法下載springcloud包問題
小編遇到這樣一個問題spring-cloud-starter-feign,spring-cloud-starter-eureka 一直無法下載,maven倉庫中包路徑顯示為unknown,怎么解決呢?下面小編給大家?guī)砹藀om.xml中maven無法下載springcloud包問題,需要的朋友可以參考下2022-08-08
SpringCloud的@RefreshScope 注解你了解嗎
這篇文章主要介紹了Spring Cloud @RefreshScope 原理及使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-09-09
Java基于外觀模式實現(xiàn)美食天下食譜功能實例詳解
這篇文章主要介紹了Java基于外觀模式實現(xiàn)美食天下食譜功能,較為詳細的講述了外觀模式的概念、原理并結(jié)合實例形似詳細分析了Java基于外觀模式實現(xiàn)美食天下食譜功能的具體操作步驟與相關(guān)注意事項,需要的朋友可以參考下2018-05-05
SpringBoot實現(xiàn)嵌入式 Servlet容器
傳統(tǒng)的Spring MVC工程部署時需要將WAR文件放置在servlet容器的文檔目錄內(nèi),而Spring Boot工程使用嵌入式servlet容器省去了這一步驟,本文就來設(shè)置一下相關(guān)配置,感興趣的可以了解一下2023-12-12
SpringBoot的ConfigurationProperties或Value注解無效問題及解決
在SpringBoot項目開發(fā)中,全局靜態(tài)配置類讀取application.yml或application.properties文件時,可能會遇到配置值始終為null的問題,這通常是因為在創(chuàng)建靜態(tài)屬性后,IDE自動生成的Get/Set方法包含了static關(guān)鍵字2024-11-11
spring boot使用RabbitMQ實現(xiàn)topic 主題
本篇文章主要介紹了spring boot使用RabbitMQ實現(xiàn)topic 主題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03

