代码之家  ›  专栏  ›  技术社区  ›  tyro

如何在Spring Boot 2中处理security.enable-csrf?

  •  0
  • tyro  · 技术社区  · 6 年前

    我正在将一个应用程序从SpringBoot1.5迁移到2.0.5。 我有一套房产 security.enable-csrf=true

    我阅读了这些文档,据说在SpringBoot2.0中:

    默认情况下,在Java配置中启用CSRF保护。

    所以在默认情况下它是启用的ok fine,但是也有一个创建的类扩展了 WebSecurityConfigurerAdapter security.enable-csrf 现在被禁用了?

    如果是,我如何启用它,就像我在1.5版本的应用程序中一样。

    我没有得到任何能明确确认如何处理的文件 属性,并在声明 网络安全配置适配器

    有人知道吗?另外,我错过的任何文档链接都会有很大的帮助。

    2 回复  |  直到 6 年前
        1
  •  4
  •   MohammadReza Alagheband    6 年前

    为了与应用程序中已设置的属性保持向后兼容性, security.enable-csrf=true ,您可以使用以下代码:

    @EnableWebSecurity
    public class WebSecurityConfig extends
    WebSecurityConfigurerAdapter {
    
        @Value("${security.enable-csrf}")
        private boolean csrfEnabled;
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            if (!csrfEnabled) {
                http.csrf().disable();
            }
        }
    }
    

    http.csrf().disable(); 那个 在上面的代码中,您可以通过


    更多信息:

    有关更多详细信息,您还可以参考spring文档:

        2
  •  0
  •   M. Deinum    6 年前

    WebSecurityConfigurerAdapter 网络安全配置适配器 ,您将覆盖 void configure(HttpSecurity http) 方法。

    你可以用这种方法禁用csrf,就像那样;

    http.csrf().disable();
    

    您可以在 csrf() HttpSecurity 类)。

    添加CSRF支持。使用时默认激活 网络安全配置适配器 的默认构造函数。你可以禁用它….”

    网络安全配置适配器 工作和csrf被激活。