【问题标题】:Disable Spring Cloud security禁用 Spring Cloud 安全性
【发布时间】:2023-03-03 17:24:02
【问题描述】:

我尝试在 Spring 中禁用 Spring 安全性可以使用此配置:

@SpringBootApplication(scanBasePackages = { ...... },
        exclude = SecurityAutoConfiguration.class)
public class Application {

    public static void main(final String[] args)
    {
        SpringApplication.run(Application.class, args);
    }
}

当我提出请求时,我得到:An expected CSRF token cannot be found

我得到提示:

01:14:35.799 [boundedElastic-1] DEBUG DefaultWebSessionManager[lambda$createWebSession$3:94] - Created new WebSession. 01:14:35.862 [boundedElastic-1] DEBUG HttpWebHandlerAdapter[traceDebug:91] - [375ab2a1] Completed 403 FORBIDDEN

你知道最新的 Spring Cloud 如何禁用 Spring Security 吗?

【问题讨论】:

    标签: spring spring-security spring-cloud spring-security-oauth2 spring-cloud-security


    【解决方案1】:

    您可以使用属性文件禁用它: security.enable.csrf=false

    或者:

    您可以通过扩展WebSecurityConfigurerAdapter 类并覆盖configure 方法来禁用csrf。参考this spring document

    @EnableWebSecurity
    @Configuration
    public class WebSecurityConfig extends
       WebSecurityConfigurerAdapter {
    
      @Override
      protected void configure(HttpSecurity http) throws Exception {
        http
          .csrf().disable();
      }
    }
    

    【讨论】:

    • 我得到了The bean 'springSecurityFilterChain', defined in class path resource [org/springframework/boot/actuate/autoconfigure/security/reactive/ReactiveManagementWebSecurityAutoConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class] and overriding is disabled.spring.main.allow-bean-definition-overriding=true 但我又遇到了同样的问题:An expected CSRF token cannot be found 还有其他解决方案吗?
    • 在 property/yaml 文件中尝试 security.enable.csrf=false 或 security.enable.csrf: false。
    • 我添加了它,但同样的问题又出现了。
    • 如果你将它添加到属性文件中并且你使用ReactiveManagementWebSecurityAutoConfiguration 只是为了禁用csrf 那么你不需要它。您可以删除该课程并尝试。
    猜你喜欢
    • 2014-07-16
    • 2020-06-11
    • 2016-05-03
    • 1970-01-01
    • 2018-07-31
    • 2016-01-02
    • 2019-12-28
    • 2015-09-23
    相关资源
    最近更新 更多