注冊中心配置了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)容,更多關于注冊中心配置spring security報錯的資料請關注腳本之家其它相關文章!
相關文章
SpringBoot整合Kaptcha實現(xiàn)圖片驗證碼加減乘除功能
在開發(fā)Web應用時,驗證碼是一個常見的功能,它可以幫助我們防止機器人的惡意操作,今天我們將學習如何使用Kaptcha生成圖片驗證碼,并自定義驗證碼內(nèi)容為100以內(nèi)的加減乘除運算,感興趣的朋友跟隨小編一起看看吧2024-07-07
SpringCloud?eureka(server)微服務集群搭建過程
這篇文章主要介紹了微服務SpringCloud-eureka(server)集群搭建,?項目搭建的主要步驟和配置就是創(chuàng)建項目和引入pom依賴,本文通過圖文示例代碼相結合給大家介紹的非常詳細,需要的朋友可以參考下2022-07-07
IDEA 自帶的數(shù)據(jù)庫工具真的很牛(收藏版)
這篇文章主要介紹了IDEA 自帶的數(shù)據(jù)庫工具真的很牛(收藏版),本文以 IntelliJ IDEA/ Mac 版本作為演示,其他版本的應該也差距不大,需要的朋友可以參考下2021-04-04
如何利用grep-console插件使Intellij idea顯示多顏色調(diào)試日志
這篇文章主要介紹了利用grep-console插件使Intellij idea顯示多顏色調(diào)試日志,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05

