java - 无法验证提供的 CSRF token ,因为找不到您的 session

标签 java spring spring-mvc spring-security

我一直在寻找这个问题,但仍然无法避免。 只有当我尝试进行 ajax 调用时才会出现问题。系统将返回错误无法验证提供的 CSRF token ,因为找不到您的 session 。

基于Spring MVC and CSRF Integration ,我需要包括@EnableWebSecurity如果我使用 Java Config 来解决这个问题,但如果使用 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 的系统,它将根据 header 信息验证用户,无需登录表单。

最佳答案

Spring 要求在每次提交表单时发送 csrf token 。为了实现这一点,可以将以下代码插入到 JSP 中:

<form>
[...]
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/> 
[...]
</form>

致以诚挚的问候

关于java - 无法验证提供的 CSRF token ,因为找不到您的 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38886190/

相关文章:

spring - 使用 ControllerClassNameHandlerMapping 的 Spring MVC 配置约定?

java - 为什么这个JButton按下后没有反应?

java - 将 HttpServletResponse 注入(inject)服务层的最佳方式

java - 配置 Shiro 以允许匿名访问资源文件夹(JS、CSS 等)

java - Spring Boot MongoRepository 空指针异常

java - 如何使用 Gradle 在 Spring Boot 中覆盖来自 Thymeleaf 1.5.8.RELEASE 的传递依赖 Groovy 版本

java - 如何在运行时修改 Web 应用程序中的属性文件?

java - pattern.compile 帮助 java 程序

java - 如何将字符添加到用户光标所在的 EditText

java - 如何逐字节写入并连续显示