Cors跨域请求,配置Access-Control-Allow-Origin:"*",无效解决方案


由于应用需要跨域请求数据,博主在JDK8、Tomcat7.0的cors可以配置Access-Control-Allow-Origin:"*",但是我按照文档配置以后却没有生效,一度怀疑是tomcat或者jdk的问题,最后想起来web.xml是按照从前往后的顺序加载的。解决方案:是filter位置的问题,你把整个放到第一个filter的位置就可以了。给出web.xml:

        xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

        och

 

       

                 contextConfigLocation

                 classpath:spring/spring-*.xml

       

 

       

                

                         webAppRootKey

                         och.root

                

                 org.springframework.web.util.Log4jConfigListener

       

       

                 org.springframework.web.context.ContextLoaderListener

       

       

       

                 org.springframework.web.context.request.RequestContextListener

       

       

       

                 CORS

                 com.thetransactioncompany.cors.CORSFilter

                

                         cors.allowOrigin

                         *

                

                

                         cors.supportedMethods

                         GET,POST, HEAD, PUT, DELETE

                

                

                         cors.supportedHeaders

                         Accept,Origin, X-Requested-With, Content-Type, Last-Modified

                

                

                         cors.exposedHeaders

                         Set-Cookie

                

                

                         cors.supportsCredentials

                         true

                

       

       

                 CORS

                 /*

       

 

       

                 characterEncodingFilter

                 org.springframework.web.filter.CharacterEncodingFilter

                

                         encoding

                         UTF-8

                

                

                         ForceEncoding

                         true

                

       

       

                 characterEncodingFilter

                 /*

       

 

       

                 control

                 com.cmos.core.filter.IControlRequestFilter

                

                         controlRequestImpl

                         com.cmos.och.control.impl.ControlRequestImpl

                

                

                         controlFilePath

                         config/control.xml

                

                

                         locale

                         zh_CN

                

       

       

                 control

                 /front/*

       

 

       

                 struts2

                org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

       

       

                 struts2

                 /front/*

       



 


你可能感兴趣的:(Java相关)