当前位置: 首页 > 知识库问答 >
问题:

Spring Boot、Spring Security和Thymeleaf:使用表单将CsrfFilter应用于网站

鲁烨
2023-03-14

我将Spring Security与Thymeleaf结合使用,并希望在不同的站点上创建一个登录和注册表单,以同时使用CSRF保护。保护登录站点很容易,就像下面的Web安全配置一样

@Override
protected void configure(final HttpSecurity http) throws Exception {
    http
            .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
            .requestMatchers()
            .antMatchers("/login", "/oauth/authorize", "/oauth/confirm_access")
            .and()
            .authorizeRequests()
            .anyRequest()
            .authenticated();
}

Spring通常支持通过配置方法中构建的安全过滤器链添加CSRF保护。此过滤器链包含一个添加/评估CSRF令牌的CSRFFilter。然后将此过滤器链用于上述配置中定义的所有匹配项。获取应用于请求的过滤器的机制可以在方法中找到

doFilterInternal(ServletRequest、ServletResponse、FilterChain)

问题是,如果我将“/注册”站点添加到此配置中,用户将首先重定向到“/登录”站点。如果我不将其添加到上述配置中,则不会应用所提到的FilterChain(因此也不会应用CsrfFilter)。

所以我想要的是在“/注册”站点的过滤器链中重用CsrfFilter,但我不知道如何做到这一点。

我更喜欢这种方法,而不是像这里或这里建议的那样编写自定义CSRF过滤器。

共有2个答案

锺离卓
2023-03-14

事实证明,Spring Security过滤器链应用于提供给requestMatchers()的列表中提到的所有endpoint。antMatchers()。

因此,要对不是登录站点的站点使用CSRF保护,我只需将其添加到此列表中,然后允许所有访问它,因此不会重定向到登录页面。我的最终配置如下所示

@Override
protected void configure(final HttpSecurity http) throws Exception {
    http.requestMatchers()
            // consider these patterns for this config and the Security Filter Chain
            .antMatchers("/login", "/register", "/oauth/authorize", "/oauth/confirm_access", "/oauth/token_key",
                    "/oauth/check_token", "/oauth/error")
            .and()
            // define authorization behaviour
            .authorizeRequests()
            // /register is allowed by anyone
            .antMatchers("/register").permitAll()
            // /oauth/authorize needs authentication; enables redirect to /login
            .antMatchers("/oauth/authorize").authenticated()
            // any other request in the patterns above are forbidden
            .anyRequest().denyAll()
            .and()
            .formLogin()
            // we have a custom login page at /login
            // that is permitted to everyone
            .loginPage("/login").permitAll();
}
施景同
2023-03-14

从这一切中,我了解到问题在于,您希望人们无需先登录即可访问/注册。这是一个简单的修复:

@Override
protected void configure(final HttpSecurity http) throws Exception {
    http
            .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
            .requestMatchers()
// add this line
.antMatchers("/register").permitAll().and

//
            .antMatchers("/login", "/oauth/authorize", "/oauth/confirm_access")
            .and()
            .authorizeRequests()
            .anyRequest()
            .authenticated();
}
 类似资料:
  • 尝试使用springboot MVC和Thymeleaf访问POST方法中的多个对象。 这是控制器。 下面是这些观点: patient.html 和result.html 和bean类是:PatientDataModel.java 另一个bean: 现在,问题是,我需要这两个bean在GET和POST方法中都可以访问。当我运行代码时,它正在执行,但是bean没有值,所有值都是空的。请建议

  • 在Thymeleaf中,我可以使用 但是我不知道如何输出到表单动作属性中。 有人对Thymeleaf超文本标记语言有什么想法吗? 样例JavaSpring-MVC控制器代码

  • 我正在学习springsecurity(基于java的配置),我无法使注销正常工作。当我点击注销时,我看到URL更改为http://localhost:8080/logout并获取“HTTP 404-/logout”。登录功能工作正常(即使使用自定义登录表单),但问题是注销,我怀疑重定向的url“localhost:8080/logout”应该类似于“localhost:8808/springte

  • 本文向大家介绍SpringBoot中的Thymeleaf用法,包括了SpringBoot中的Thymeleaf用法的使用技巧和注意事项,需要的朋友参考一下 Thymeleaf Thymeleaf是最近SpringBoot推荐支持的模板框架,官网在thymeleaf.org这里。 我们为什么要用Thymeleaf来作为模板引擎呢?官网给了我们一个非常令人信服的解释: Thymeleaf is a m

  • 本文向大家介绍springboot中thymeleaf模板使用详解,包括了springboot中thymeleaf模板使用详解的使用技巧和注意事项,需要的朋友参考一下 这篇文章将更加全面详细的介绍thymeleaf的使用。thymeleaf 是新一代的模板引擎,在spring4.0中推荐使用thymeleaf来做前端模版引擎。 thymeleaf介绍 简单说, Thymeleaf 是一个跟 Vel

  • 本文向大家介绍SpringBoot thymeleaf的使用方法解析,包括了SpringBoot thymeleaf的使用方法解析的使用技巧和注意事项,需要的朋友参考一下 1.pom.xml添加相应依赖 2.application.properties 3.common.xml文件,注意文件路径 4.添加TemplateController.java 5.添加app.java 6.访问路径,完成