1. 程式人生 > >Cors跨域請求,配置Access-Control-Allow-Origin:"*",無效解決方案

Cors跨域請求,配置Access-Control-Allow-Origin:"*",無效解決方案


由於應用需要跨域請求資料,博主在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.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">

        <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>