分享

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

 WindySky 2017-12-11


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

<?xml version="1.0"encoding="UTF-8"?>

<web-app id="WebApp_ID"version="2.4"

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

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

        <display-name>och</display-name>

 

        <context-param>

                 <param-name>contextConfigLocation</param-name>

                 <param-value>classpath:spring/spring-*.xml</param-value>

        </context-param>

 

        <listener>

                 <context-param>

                         <param-name>webAppRootKey</param-name>

                         <param-value>och.root</param-value>

                 </context-param>

                 <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>

        </listener>

        <listener>

                 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

        </listener>

        <!--启用Spring的scope='request' -->

        <listener>

                 <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>

        </listener>

        <!--CORS Filter -->

        <filter>

                 <filter-name>CORS</filter-name>

                 <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>

                 <init-param>

                         <param-name>cors.allowOrigin</param-name>

                         <param-value>*</param-value>

                 </init-param>

                 <init-param>

                         <param-name>cors.supportedMethods</param-name>

                         <param-value>GET,POST, HEAD, PUT, DELETE</param-value>

                 </init-param>

                 <init-param>

                         <param-name>cors.supportedHeaders</param-name>

                         <param-value>Accept,Origin, X-Requested-With, Content-Type, Last-Modified</param-value>

                 </init-param>

                 <init-param>

                         <param-name>cors.exposedHeaders</param-name>

                         <param-value>Set-Cookie</param-value>

                 </init-param>

                 <init-param>

                         <param-name>cors.supportsCredentials</param-name>

                         <param-value>true</param-value>

                 </init-param>

        </filter>

        <filter-mapping>

                 <filter-name>CORS</filter-name>

                 <url-pattern>/*</url-pattern>

        </filter-mapping>

 

        <filter>

                 <filter-name>characterEncodingFilter</filter-name>

                 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

                 <init-param>

                         <param-name>encoding</param-name>

                         <param-value>UTF-8</param-value>

                 </init-param>

                 <init-param>

                         <param-name>ForceEncoding</param-name>

                         <param-value>true</param-value>

                 </init-param>

        </filter>

        <filter-mapping>

                 <filter-name>characterEncodingFilter</filter-name>

                 <url-pattern>/*</url-pattern>

        </filter-mapping>

 

        <filter>

                 <filter-name>control</filter-name>

                 <filter-class>com.cmos.core.filter.IControlRequestFilter</filter-class>

                 <init-param>

                         <param-name>controlRequestImpl</param-name>

                         <param-value>com.cmos.och.control.impl.ControlRequestImpl</param-value>

                 </init-param>

                 <init-param>

                         <param-name>controlFilePath</param-name>

                         <param-value>config/control.xml</param-value>

                 </init-param>

                 <init-param><!--Internationalization config -->

                         <param-name>locale</param-name>

                         <param-value>zh_CN</param-value>

                 </init-param>

        </filter>

        <filter-mapping>

                 <filter-name>control</filter-name>

                 <url-pattern>/front/*</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>/front/*</url-pattern>

        </filter-mapping>

</web-app>



 


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约