CORS 筛选器无法维护 session

CORS Filter unable to maintain session

我有一个基于 Struts2 的应用程序,它接收来自另一个应用程序(跨域)的请求。为此,我在 web.xml 中的 struts2 过滤器之前添加了 CORS 过滤器:

    <filter>
        <filter-name>CorsFilter</filter-name>
        <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
        <init-param>
            <param-name>cors.allowed.origins</param-name>
            <param-value>*</param-value>
        </init-param>
        <init-param>
            <param-name>cors.allowed.methods</param-name>
            <param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
        </init-param>
        <init-param>
            <param-name>cors.allowed.headers</param-name>
            <param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,X-Test-Header
            </param-value>
        </init-param>
        <init-param>
            <param-name>cors.exposed.headers</param-name>
            <param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials
            </param-value>
        </init-param>
        <init-param>
            <param-name>cors.support.credentials</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>cors.preflight.maxage</param-name>
            <param-value>1000</param-value>
        </init-param>
    </filter>
     <filter-mapping>
        <filter-name>CorsFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
   <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

创建新 session 的应用程序的第一个请求工作正常。但是在随后的请求中,当我尝试在我的操作 class 中访问相同的 session 时,我得到 null。这是我正在做的事情:

HttpSession session = ServletActionContext.getRequest().getSession(false);

以上通常工作正常,但这里 session 可能会以某种方式失效。 我正在 Chrome 浏览器中测试这个,启用了 Allow-Control-Allow-Origin: * 扩展。这是我的 POST 请求 header 在控制台中的显示方式:

Accept:*/*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:9
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host:localhost:7070
Origin:http://evil.com/
Referer:http://testdevsvr:8989/SmartAdministration/home
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36

我想知道我是否遗漏了配置中的某些内容,或者其他错误阻止了我继续进行。

我终于根据给出的建议通过执行这两个步骤来维持会话 here:

1.) 在我的所有 ajax 调用中包含以下代码段:

  xhrFields: {withCredentials: true}

2.) 在我的 web.xml:

中包含此配置
     <session-config>
        <session-timeout>30</session-timeout> 
            <cookie-config>
                <http-only>false</http-only>
            </cookie-config>
    </session-config>