【发布时间】:2016-12-17 14:12:57
【问题描述】:
我一直在寻找这个问题,但仍然无法避免。 只有在我尝试进行 ajax 调用时才会出现问题。系统会返回错误Could not verify the provided CSRF token because your session was not found.
基于Spring MVC and CSRF Integration,如果我使用Java Config,我需要包含@EnableWebSecurity 来解决这个问题,但如果使用XML,则需要使用这个:
@RestController
public class CsrfController {
@RequestMapping("/csrf")
public CsrfToken csrf(CsrfToken token) {
return token;
}
}
而且我不确定如何使用上面的类。
问题是如何使用上面的类,如果它真的是一个解决方案,或者有什么我可以使用的解决方案?
这是我的安全配置 xml 文件;
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- Global Security Settings -->
<global-method-security pre-post-annotations="enabled" />
<context:component-scan base-package="com.my.web" />
<!-- Reads WEB Configuration file to resolve ${} and read @Value for Security-->
<context:property-placeholder location="classpath:cfg/web.cfg" />
<context:annotation-config />
<!-- Security Access Configuration -->
<http auto-config="false" use-expressions="true" authentication-manager-ref="CAP" disable-url-rewriting="true" entry-point-ref="IAE">
<session-management session-fixation-protection="newSession" session-authentication-error-url="/logout?timeout" >
<concurrency-control max-sessions="1" expired-url="/logout?expired" />
</session-management>
<custom-filter position="PRE_AUTH_FILTER" ref="entryFilter" />
<intercept-url pattern="/resources/**" access="permitAll()" requires-channel="https" />
<intercept-url pattern="/clearcache" access="permitAll()" requires-channel="https" />
<intercept-url pattern="/logout" access="permitAll()" requires-channel="https" />
<intercept-url pattern="/**" access="isFullyAuthenticated()" requires-channel="https" />
<port-mappings >
<port-mapping http="7001" https="7002" />
</port-mappings>
<headers>
<frame-options policy="SAMEORIGIN" />
<hsts />
<cache-control />
<xss-protection />
<content-type-options />
</headers>
</http>
<beans:bean id="entryFilter" class="com.my.web.security.HeaderFilter" >
<beans:property name="authenticationManager" ref="CAP"/>
</beans:bean>
<beans:bean id="IAE" class="com.my.web.security.CustomAuthenticationEntryPoint" />
<beans:bean id="CAP" class="com.my.web.security.CustomAuthenticationManager" />
<beans:import resource="../aop/aspect-security.xml" />
</beans:beans>
此外,我是一个类似于 CA Siteminder 的使用系统,它将根据标题信息验证用户,而无需登录表单。
【问题讨论】:
-
AJAX 调用如何指示会话?
-
是因为
POST在 ajax 中调用。它需要 csrf 令牌并匹配它将会话令牌 -
我认为需要提供AJAX调用的代码。
标签: java spring spring-mvc spring-security