尝试从在端口 8081 上运行的 app1 向在端口 8088 上运行的 app2 进行 ajax 调用时出现以下错误。由于 CORS 问题,重定向被浏览器阻止。
无法加载 http://localhost:8088/j_security_check?j_username=admin&j_password=admin : 重定向自 ' http://localhost:8088/j_security_check?j_username=admin&j_password=admin至 ' http://localhost:8088/resource ' 已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin” header 。 产地' http://localhost:8081 ' 因此不允许访问。
有没有办法将 CORS header 添加到自定义 j_security_check 类的响应中?
最佳答案
尝试在tomcat的web.xml中加入如下过滤器
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern> /* </url-pattern>
</filter-mapping>
并检查
关于java - 是否可以将 CORS header 添加到 Tomcat 容器中来自 j_security_check 的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53005785/