【问题标题】:Using just Spring Security CSRF feature仅使用 Spring Security CSRF 功能
【发布时间】:2015-03-28 08:41:02
【问题描述】:

我想只使用 Spring Security 的 CSRF 功能而不使用任何其他身份验证/授权功能,因为这些功能是由第三方提供商为我提供的。如果可以做到这一点,我如何告诉 Spring 不要寻找任何具有其依赖 bean 的身份验证管理器,而只是拦截所有 URL,并添加 csrf 令牌。

【问题讨论】:

  • 你试过了吗?你遇到了什么问题?

标签: spring spring-security csrf


【解决方案1】:

你可以试试这个:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .anyRequest().permitAll();
    }

默认情况下启用 CSRF。您可以使用(例如在百里香中):

<meta name="_csrf" value="dummy" th:value="${_csrf.token}" />

【讨论】:

    【解决方案2】:

    如果您打算仅使用其 CSRF 保护,我个人会避免包含 Spring Security Filter 链。如果没有正确的配置,你最终会给你的应用程序带来不必要的逻辑负担。

    Spring Security 中的 CSRF 保护由 org.springframework.security.web.csrf.CsrfFilter servlet 过滤器提供。

    默认情况下,过滤器将预期的 CSRF 令牌存储在 HttpSession 上。

    您可以轻松实现类似的东西或尝试仅在您的 servlet 过滤器链/web.xml 中声明此过滤器。

    【讨论】:

      【解决方案3】:

      我通过进行以下更改/添加使 CSRF 功能正常工作。另外,我在我的 jsp 中使用了 &lt;form:form&gt; 标签来利用 Spring 自动插入令牌。

      添加的罐子:

      spring-security-acl-4.0.0.RC1.jar
      spring-security-config-4.0.0.RC1.jar
      spring-security-core-4.0.0.RC1.jar
      spring-security-taglibs-4.0.0.RC1.jar
      spring-security-web-4.0.0.RC1.jar
      

      web.xml 添加:

      <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
      </filter>
      
      <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
      

      添加了新的 java 文件:

      import org.springframework.context.annotation.Configuration;
      import org.springframework.security.config.annotation.web.builders.HttpSecurity;
      import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
      import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
      
      @Configuration
      @EnableWebSecurity
      public class TgtWebSecurityConfigureAdapter extends WebSecurityConfigurerAdapter {
      
          @Override
          protected void configure(HttpSecurity http) throws Exception {
              http
                      .authorizeRequests()
                      .anyRequest().permitAll();
          }
      }
      

      Marco - 仅使用 CSRF 过滤器是行不通的。

      【讨论】:

        猜你喜欢
        • 2019-11-07
        • 2020-12-02
        • 1970-01-01
        • 2014-01-04
        • 2015-12-10
        • 2016-09-01
        • 1970-01-01
        • 2018-07-21
        相关资源
        最近更新 更多