java服务端解决js跨域的问题

response.setHeader("Access-Control-Allow-Origin""*");


但是这种方式并不能解决所有场景下的问题

参考如下文章:


下载cors-filter-1.7.jar,java-property-utils-1.9.jar这两个库文件,放到lib目录下(可在
http://search.maven.org上查询并下载)
或者在POM文件中添加依赖:
<!-- 添加解决跨域问题的jar包 -->
        <dependency>
            <groupId>com.thetransactioncompany</groupId>
            <artifactId>cors-filter</artifactId>
            <version>1.9.1</version>
            <scope>runtime</scope>
        </dependency>
工程项目中web.xml中的配置如下:
<!-- 添加解决跨域的代码配置,基于pom中有cors-filter的依赖 ,begin -->
<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>  
<!-- 添加解决跨域的代码配置,基于pom中有cors-filter的依赖 ,end -->

阅读更多

没有更多推荐了,返回首页