Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法验证提供的CSRF令牌,因为找不到您的会话_Java_Spring_Spring Mvc_Spring Security - Fatal编程技术网

Java 无法验证提供的CSRF令牌,因为找不到您的会话

Java 无法验证提供的CSRF令牌,因为找不到您的会话,java,spring,spring-mvc,spring-security,Java,Spring,Spring Mvc,Spring Security,我一直在寻找这个问题,但仍然无法避免只有在我尝试进行ajax调用时,问题才会出现。系统将返回错误无法验证提供的CSRF令牌,因为找不到您的会话。 基于,如果我使用Java配置,我需要包含以解决此问题,但如果使用XML,则需要使用以下内容: @RestController public class CsrfController { @RequestMapping("/csrf") public CsrfToken csrf(CsrfToken token) { r

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

基于,如果我使用Java配置,我需要包含以解决此问题,但如果使用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的系统,该系统将根据标题信息验证用户,而无需登录表单。

这就是答案

好的,这里的问题,从字面上来说,有相同的原因,主要是它会影响系统,使用
Rest
header
,而不使用表单登录。这意味着webapp在容器系统中。因此,无论是系统容器还是系统在容器中的位置在客户端都是完全不同的

下面是一个例子

对于容器webapp:

 www.myweb.com/main/
对于容器中的webapp:

www.myweb.com/main/child
这个不同的将拒绝访问并提供403,因为可以肯定的是,我们正在查找的这个不存在

例如:

www.myweb.com/main/child/playground
那里存在
/playway
uri,但此处确实禁止:

www.myweb.com/main/playground
因此,请检查您的uri并尝试确保您使用的是相对路径。好多了。


p/s:由于某些原因,从服务器端调用的所有内容在容器或容器中的系统中都保持不变。这是因为服务器端使用的是相对路径,而不是绝对路径。

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

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

[...]
[...]

最诚挚的问候

我是新来的SO,这是我的第一个答案,请耐心等待。 这是在使用jQuery进行AJAX调用之前必须完成的工作:

$(document).ready(

        function() {

            var token = $("meta[name='_csrf']").attr("content");

            $.ajaxSetup({
                headers : {
                    "X-CSRF-Token" : token
                }
            });

});

AJAX调用如何表示会话?这是因为AJAX中的
POST
调用。它需要csrf令牌并匹配会话令牌。我认为,您需要提供AJAX调用的代码。主机在您的示例中仍然是一样的:
www.myweb.com
,因此应该没有任何问题。