微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Spring 5.2中加载自定义CSRF令牌存储库?

如何解决如何在Spring 5.2中加载自定义CSRF令牌存储库?

我们使用spring框架的应用程序需要实现基于请求的CSRF令牌,以满足安全性要求。当前,我们有HttpSessionCsrftokenRepository作为Spring认提供的基于会话的CSRF令牌。根据我发现的说明,通过像这样配置xml

<security:csrf token-repository-ref="customrequestCsrftokenRepository"/>

<bean id="customrequestCsrftokenRepository" class="com.dev.common_web.security.configuration.CustomCsrftokenRepository"/>

将加载实现CsrftokenRepository接口的自定义令牌存储库以处理令牌请求。

但是,当应用程序启动并以调试模式运行时,我可以看到它是Spring认的HttpSessionCsrftokenRepository,用于处理令牌的加载和生成。我也尝试过在像

这样的xml配置中使用spring CookieCsrftokenRepository
<security:csrf token-repository-ref="cookieCsrftokenRepository"/>
<bean id="cookieCsrftokenRepository" class="org.springframework.security.web.csrf.CookieCsrftokenRepository"/> 

当应用程序运行时,再次加载HttpSessionCsrftokenRepository来处理令牌请求。似乎在xml中配置为“ token-repository-ref”的值无关紧要,始终使用HttpSessionCsrftokenRepository。

如何配置spring以使用其他csrf令牌存储库而不是认的HttpSessionCsrftokenRepository?我们正在使用Spring 5.2。

解决方法

我设法弄清楚了:-)。 在应用程序的security.xml中,我们还定义了自定义的csrf请求匹配器,以禁用某些页面的csrf检查。现在添加自定义的csrf令牌存储库时,必须在的同一行中定义这两个。如果在这样的两行中定义它们,则仅加载其中一行。

<security:csrf token-repository-ref="customRequestCsrfTokenRepository"/>
<security:csrf request-matcher-ref="customCsrfRequestMatcher"/>

必须是这样

<security:csrf token-repository-ref="customRequestCsrfTokenRepository" request-matcher-ref="customCsrfRequestMatcher" />
   

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。