Tomcat没有在跨域jquery ajax调用中接收自定义标头

时间:2016-06-05 12:30:05

标签: java jquery spring tomcat cors

我知道这似乎是一个重复的问题,但我没有在这里或我尝试过的任何其他网站找到任何搜索结果。

这是我的情景:

我在sub.domain.com上通过ssl暴露了在tomcat中运行的API。 API是使用spring开发的。

我正在从domain.com中的客户端应用程序访问API。

我已使用下面的过滤器

在tomcat中启用了CORS
<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>ip,sessiontoken,Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Last-Modified,Authorization</param-v$
</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>60</param-value>
</init-param></filter>
<filter-mapping>
    <filter-name>CorsFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

在cors.allowed.headers中,您可以看到ip和sessiontoken。这些是我正在使用的自定义标题。

在客户端,这是我正在使用的电话:

$.ajax({
            type: "GET",
            dataType: "json",
            url: server + apiEnpoint,
            async: false,
            headers: {
                "sessionToken": "XXXXXXXXXXXXXX"
            },

这是API的第二个请求。第一个是登录请求(用户名,密码),没有问题。然后我调用一个经过身份验证的方法来检索用户数据(以前的JS)。

如下图所示,服务器对预检请求的响应为200.但是不会发送继续实际请求所需的标头。

enter image description here 这是在引用使用该请求发送的标头时登录tomcat:

ERROR: org.springframework.security.authentication.AuthenticationManager - org.apache.tomcat.util.http.ValuesEnumerator@65beb190
ERROR: org.springframework.security.authentication.AuthenticationManager - host:sub.domain.com
ERROR: org.springframework.security.authentication.AuthenticationManager - user-agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:46.0) Gecko/20100101 Firefox/46.0
ERROR: org.springframework.security.authentication.AuthenticationManager - accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
ERROR: org.springframework.security.authentication.AuthenticationManager - accept-language:en-US,en;q=0.5
ERROR: org.springframework.security.authentication.AuthenticationManager - accept-encoding:gzip, deflate, br
ERROR: org.springframework.security.authentication.AuthenticationManager - access-control-request-method:GET
ERROR: org.springframework.security.authentication.AuthenticationManager - access-control-request-headers:sessiontoken
ERROR: org.springframework.security.authentication.AuthenticationManager - origin:https://citywallet.net
ERROR: org.springframework.security.authentication.AuthenticationManager - connection:keep-alive
DEBUG: org.springframework.security.access.vote.AffirmativeBased - Voter: org.springframework.security.web.access.expression.WebExpressionVoter@1de0bff9, returned: -1
DEBUG: org.springframework.security.web.access.ExceptionTranslationFilter - Access is denied (user is anonymous); redirecting to authentication entry point
org.springframework.security.access.AccessDeniedException: Access is denied
    at org.springframework.security.access.vote.AffirmativeBased.decide(AffirmativeBased.java:83)

我可能缺少配置中的其他内容吗?

Spring需要进行任何更改吗?

提前多多感谢。已经处理了3天了。