Tomcat 未在跨域 jquery ajax 调用中接收自定义标头
Posted
技术标签:
【中文标题】Tomcat 未在跨域 jquery ajax 调用中接收自定义标头【英文标题】:Tomcat is not receiving custom headers in a cross domain jquery ajax call 【发布时间】:2016-10-05 03:30:56 【问题描述】:我知道这似乎是一个重复的问题,但我在这里的任何搜索或我尝试过的任何其他网站中都没有找到答案。
这是我的场景:
我在 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。但继续实际请求所需的标头并未发送过来。
这是打印随该请求发送的标头时在 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 天了。
【问题讨论】:
【参考方案1】:您应该阅读 spring 文档 http://docs.spring.io/autorepo/docs/spring/4.2.x/spring-framework-reference/html/cors.html 和 XMLHttpRequest cors https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
【讨论】:
Liaozq,您分享的第二个链接中的所有内容都已经考虑过了。关于第一个链接,您的经验是什么解决方案?点 26.3.1 和 26.3.2?能详细点吗? 第一个请求选项是200,所以服务器允许cors,然后客户端会发送第二个请求,spring security.not cors导致的异常。我的英语不是很好以上是关于Tomcat 未在跨域 jquery ajax 调用中接收自定义标头的主要内容,如果未能解决你的问题,请参考以下文章
我们可以在跨域中使用 Jquery $.ajax() 调用通过 https 访问 WCF 服务吗?
解决jquery ajax在跨域访问post请求的时候,ie9以下无效(包括ie9)的问题
JQuery - Ajax和Tomcat跨域请求问题解决方法!