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

Spring SAML扩展和Spring Security CSRF保护冲突

司徒捷
2023-03-14

我们有一个带有Spring Security(3.2.4)的Spring MVC(4.0.5)应用程序,其中包括工作正常的CSRF保护。我们现在正在添加SAML安全扩展(spring-security-saml2-core 1.0.0),这会导致CSRF保护出现问题。

元数据已在SSOCIRCE上配置,正在尝试访问http://localhost:8080/myapp指向SSOCIRCE上的登录页面。验证后,浏览器重定向到http://localhost:8080/myapp/saml/SSO并生成一个错误:

HTTP状态403-未找到预期的CSRF令牌。您的会话过期了吗?

如果我们关闭CSRF保护,一切正常。我们如何在保持CSRF保护的同时仍然使用SAML扩展?

在设置SAML扩展之前,我们使用了一个登录表单,CSRF保护工作正常,我们没有在登录JSP上收到错误,它也没有令牌。

SAML之前的代码:

@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
    httpSecurity.authorizeRequests()
            .antMatchers("/login", "/login.request", "/logout").permitAll()
            .anyRequest()
                .hasAnyAuthority("MyRole")
                    .and().formLogin()
            .loginPage("/login.request").loginProcessingUrl("/login")
            .failureUrl("/login.request?error").permitAll().and().logout()
            .logoutUrl("/logout").permitAll()
            .logoutSuccessUrl("/login.request");
}

SAML代码:

@Override
protected void configure(HttpSecurity http) throws Exception {
    //http.csrf().disable();

    http.httpBasic().authenticationEntryPoint(samlEntryPoint());

    http.addFilterBefore(metadataGeneratorFilter(),
            ChannelProcessingFilter.class).addFilterAfter(samlFilter(),
            BasicAuthenticationFilter.class);

    http
        .authorizeRequests()
            .antMatchers("/error").permitAll()
            .antMatchers("/saml/**").permitAll()
            .anyRequest()
                .hasAnyAuthority("MyRole")
            .anyRequest().authenticated();

    http.logout().logoutSuccessUrl("/");
}

使现代化

重新启用CSRF保护并将日志记录设置为DEBUG后,以下是成功身份验证后立即发生的日志:

22.10.2014 16:54:17.374 [http-bio-8080-exec-8] DEBUG o.s.w.m.support.MultipartFilter -
                Using MultipartResolver 'filterMultipartResolver' for MultipartFilter

22.10.2014 16:54:17.377 [http-bio-8080-exec-8] DEBUG o.s.b.f.s.DefaultListableBeanFactory -
                Returning cached instance of singleton bean 'filterMultipartResolver'

22.10.2014 16:54:17.788 [http-bio-8080-exec-8] DEBUG o.s.w.m.support.MultipartFilter -
                Request [/epass/saml/SSO] is not a multipart request

22.10.2014 16:54:17.790 [http-bio-8080-exec-8] DEBUG o.s.s.w.u.m.AntPathRequestMatcher -
                Checking match of request : '/saml/sso'; against '/resources/**'

22.10.2014 16:54:17.791 [http-bio-8080-exec-8] DEBUG o.s.security.web.FilterChainProxy -
                /saml/SSO at position 1 of 14 in additional filter chain; firing Filter: 'MetadataGeneratorFilter'

22.10.2014 16:54:17.793 [http-bio-8080-exec-8] DEBUG o.s.security.web.FilterChainProxy -
                /saml/SSO at position 2 of 14 in additional filter chain; firing Filter: 'WebAsyncManagerIntegrationFilter'

22.10.2014 16:54:17.795 [http-bio-8080-exec-8] DEBUG o.s.security.web.FilterChainProxy -
                /saml/SSO at position 3 of 14 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'

22.10.2014 16:54:17.797 [http-bio-8080-exec-8] DEBUG o.s.s.w.c.HttpSessionSecurityContextRepository -
                HttpSession returned null object for SPRING_SECURITY_CONTEXT

22.10.2014 16:54:17.798 [http-bio-8080-exec-8] DEBUG o.s.s.w.c.HttpSessionSecurityContextRepository -
                No SecurityContext was available from the HttpSession: org.apache.catalina.session.StandardSessionFacade@b08c9c9. A new one will be created.

22.10.2014 16:54:17.800 [http-bio-8080-exec-8] DEBUG o.s.security.web.FilterChainProxy -
                /saml/SSO at position 4 of 14 in additional filter chain; firing Filter: 'HeaderWriterFilter'

22.10.2014 16:54:17.801 [http-bio-8080-exec-8] DEBUG o.s.s.w.h.writers.HstsHeaderWriter -
                Not injecting HSTS header since it did not match the requestMatcher org.springframework.security.web.header.writers.HstsHeaderWriter$SecureRequestMatcher@244a79ef

22.10.2014 16:54:17.802 [http-bio-8080-exec-8] DEBUG o.s.security.web.FilterChainProxy -
                /saml/SSO at position 5 of 14 in additional filter chain; firing Filter: 'CsrfFilter'
22.10.2014 16:54:17.805 [http-bio-8080-exec-8] DEBUG o.s.security.web.csrf.CsrfFilter -
                Invalid CSRF token found for `http://localhost:8080/myapp/saml/SSO`

22.10.2014 16:54:17.807 [http-bio-8080-exec-8] DEBUG o.s.s.w.c.HttpSessionSecurityContextRepository -
                SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.

22.10.2014 16:54:17.808 [http-bio-8080-exec-8] DEBUG o.s.s.w.c.SecurityContextPersistenceFilter -
                SecurityContextHolder now cleared, as request processing completed

共有3个答案

慎俊艾
2023-03-14

对于其他可能面临这个问题的人,我也能够通过强制过滤器的顺序来解决这个问题。

我换了这个:

http.addFilterBefore(metadataGeneratorFilter(), ChannelProcessingFilter.class)
    .addFilterAfter(samlFilter(), BasicAuthenticationFilter.class);

有了这个:

FilterChainProxy samlFilter = samlFilter();

http.addFilterBefore(metadataGeneratorFilter(), ChannelProcessingFilter.class)
    .addFilterAfter(samlFilter, BasicAuthenticationFilter.class)
    .addFilterBefore(samlFilter, CsrfFilter.class);

现在它开始工作了。

陆子默
2023-03-14

这对我很有用:

http。csrf()。ignoringAntMatchers(“/saml/**”)

因此,在处理/saml/**

发件人:https://github.com/choonchernlim/spring-security-adfs-saml2/blob/master/src/main/java/com/github/choonchernlim/security/adfs/saml2/SAMLWebSecurityConfigurerAdapter.java

花健
2023-03-14

你至少有两个选择。

一种是实现一个定制的请求匹配器org.springframework.security.web.util.RequestMatcher),它将在Spring SAML URL上不匹配,并通过以下方式将其提供给csrf配置:

http.csrf().requireCsrfProtectionMatcher(matcher);

另一个更简单的方法是在单独的超文本传输协议配置中定义Spring SAMLendpoint,该配置不启用csrf保护。

执行此操作的XML配置可以类似于:

<!-- SAML processing endpoints -->
<security:http pattern="/saml/**" entry-point-ref="samlEntryPoint">
    <security:custom-filter before="FIRST" ref="metadataGeneratorFilter"/>
    <security:custom-filter after="BASIC_AUTH_FILTER" ref="samlFilter"/>
</security:http>

<!-- Secured pages with SAML as entry point -->
<security:http entry-point-ref="samlEntryPoint">
    <security:csrf />
    <security:intercept-url pattern="/**" access="IS_AUTHENTICATED_FULLY"/>
    <security:custom-filter before="FIRST" ref="metadataGeneratorFilter"/>
</security:http>

对于Java配置,类似这样的方式应该可以工作:

@Configuration
@EnableWebSecurity
public class MutlipleHttpConfigurationConfig {

    @Configuration
    @Order(1)
    public static class SAMLWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
        protected void configure(HttpSecurity http) throws Exception {
            http.antMatcher("/saml/**");
            http.csrf().disable();
            http.httpBasic().authenticationEntryPoint(samlEntryPoint());
            http.addFilterBefore(metadataGeneratorFilter(),
                    ChannelProcessingFilter.class).addFilterAfter(samlFilter(),
                    BasicAuthenticationFilter.class);
        }
    }

    @Configuration
    public static class BasicWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
        protected void configure(HttpSecurity http) throws Exception {
            http.httpBasic().authenticationEntryPoint(samlEntryPoint());
            http.addFilterBefore(metadataGeneratorFilter(), ChannelProcessingFilter.class);
            http
                    .authorizeRequests()
                    .antMatchers("/error").permitAll()
                    .anyRequest()
                    .hasAnyAuthority("MyRole")
                    .anyRequest().authenticated();

            http.logout().logoutSuccessUrl("/");
        }
    }
}

有关使用Java配置定义多个超文本传输协议配置的详细信息,请参阅Spring Security手册。

 类似资料:
  • 由于某些跟踪调试的PHP扩展大量使用了全局变量 可能会导致Swoole协程发生崩溃。请关闭以下相关扩展: xdebug phptrace aop molten xhprof phalcon(Swoole协程无法运行在 phalcon 框架中) 其中xdebug和phptrace可以用sdebug代替,xhprof、blackfire和molten可以用SwooleTracker 代替。

  • 问题内容: 我尝试运行此命令,但它给了我这个错误。 我试图将“ extension = php_openssl.dll”添加到“ php.ini”,但仍然出现此错误 问题答案: 我也发生了同样的错误。我通过关闭Composer的TLS修复了该问题, 虽然不安全, 但是我承担了 开发机器 上的风险。 尝试这个: 并重新运行您的Composer。它对我有用! 但这是不安全的, 不建议您在Server上

  • 说明 这个 API 通常可以与其他所有包一起工作,然而,一些特殊的与 Laravel 融合很深,利用 Laravel 的路由提供某些功能的包,可能和 API 冲突。这一页列表其他的包与 API 冲突,以及一些必要的步骤使两个包正常工作。 目前还没有冲突的包.

  • 我有一个使用spring security和mvc框架开发的门户应用程序。此门户应用程序连接到IDP(使用Spring security和Spring saml开发)进行身份验证。如果用户身份验证成功,用户将被导航到主页,其中为外部应用程序提供了多个链接……当用户单击应用程序链接时,用户应成功导航到相应的应用程序,而无需质疑登录页面。 其他应用程序是使用strut和Spring Security开

  • 我正试图扩展一种特质。我的意思是创建一个特性,为不同的特性重新定义几个方法。 这是我的代码(PHP5.6和Laravel 5.4): 当我在一个模型中使用时,问题就出现了,它与冲突,因为具有。在PHP7之前,如果您尝试两次定义同一属性,traits会抛出一个致命错误。因此,由于我在中定义了,并通过借助再次定义了,因此它就中断了。 简言之: 有没有办法说服PHP,它真的是同样的事情,它可以停止与自己

  • 我正试图安装一个PostgreSQL docker化服务,与plpython。我能够成功地构建映像,但是当我来运行容器时,我得到以下错误: 错误:无法打开扩展控制文件"/usr/share/postgresql/9.5/扩展/plpython3u.control":没有这样的文件或目录声明:创建扩展"plpython3u"; psql:/docker-entrypoint-initdb. d/cr