• 引入两个jar包(java-property-utils-1.9.1.jar,cors-filter-2.4.jar)

pom.xml 中配置项目依赖

<!-- dependencies 中添加 -->
<dependency>
<groupId>com.thetransactioncompany</groupId>
<artifactId>cors-filter</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.thetransactioncompany</groupId>
<artifactId>java-property-utils</artifactId>
<version>1.9.1</version>
</dependency>
  • web.xml 中配置 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>