ITKeyword,专注技术干货聚合推荐

注册 | 登录

最全面关于J2EE跨域资源共享的解决方案以及所需要依赖的Jar包

wqsmd1 分享于 2015-02-07

2020腾讯云共同战“疫”,助力复工(优惠前所未有!4核8G,5M带宽 1684元/3年),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1054

2020阿里云最低价产品入口,含代金券(新老用户有优惠),
地址https://www.aliyun.com/minisite/goods

J2EE跨域资源共享的解决方案

看完这些还不能解决你的问题,联系QQ:1552298726

通用解决方案:需要依耐下面两个JAR包cors-filter-1.7.jar,java-property-utils-1.9.jar

工程项目中web.xml中的配置如下:

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

<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>service.print.cloud</display-name>
    <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>

    <welcome-file-list>
       <welcome-file>index.html</welcome-file>
       <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
       <welcome-file>default.html</welcome-file>
       <welcome-file>default.htm</welcome-file>
       <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

 

http://blog.csdn.net/ye1992/article/details/43487623

 

 

 

 

 

 

 

 

 

 

 

 

SpringMVC跨域自定义Filter的解决方案:

http://www.tuicool.com/articles/euiuEv

 

1.RequestFilter

packagecom.myapp.accounting.aes.poc.controller;

 

import java.io.IOException;

import javax.servlet.FilterChain;

import javax.servlet.ServletException;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importorg.springframework.web.filter.OncePerRequestFilter;

 

 

public class CorsFilter extendsOncePerRequestFilter {

 

 @Override

   protected void doFilterInternal(HttpServletRequest request,HttpServletResponse response, FilterChain filterChain)

           throws ServletException, IOException {

       if (request.getHeader("Access-Control-Request-Method") != null&& "OPTIONS".equals(request.getMethod())) {

           // CORS "pre-flight" request

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

           response.addHeader("Access-Control-Allow-Methods", "GET,POST, PUT, DELETE, OPTIONS");

           response.addHeader("Access-Control-Allow-Headers","origin, content-type, accept, x-requested-with, sid, mycustom,smuser");

           response.addHeader("Access-Control-Max-Age","1800");//30 min

       }

       filterChain.doFilter(request, response);

    }

}

 

 

 

 

 

 

 

 

 

 

2. web.xml 

 

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

 

<web-app version="2.5"xmlns="http://java.sun.com/xml/ns/javaee"

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 

 <!-- Processes application requests forSecurities component related requests -->

 <servlet>

 <servlet-name>mvc-dispatcher</servlet-name>

 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

 <load-on-startup>1</load-on-startup>

 </servlet>

 

 <servlet-mapping>

 <servlet-name>mvc-dispatcher</servlet-name>

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

 </servlet-mapping>

 

 <context-param>

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

 <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>

 </context-param>

 

 <!-- Creates the Spring Container shared byall Servlets and Filters -->

 <listener>

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

 </listener>

 

 <filter>

  <filter-name>cors</filter-name>

 <filter-class>com.myapp.accounting.aes.poc.controller.CorsFilter</filter-class>

 </filter>

 

 <filter-mapping>

  <filter-name>cors</filter-name>

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

 </filter-mapping>

 

</web-app>

 

 

SSH跨域自定义Filter的解决方案:

根据文档整理修改,这个方案没测过

1.Filter

package com.bdznxt.util.Filter;

 

import java.io.IOException;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

import javax.servlet.Filter;

 

 

public class CorsFilter implements Filter {

 

 protected void doFilter(HttpServletRequest request,HttpServletResponse response, FilterChain filterChain)

           throws ServletException, IOException {

       if (request.getHeader("Access-Control-Request-Method") != null&& "OPTIONS".equals(request.getMethod())) {

           // CORS "pre-flight" request

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

           response.addHeader("Access-Control-Allow-Methods", "GET,POST, PUT, DELETE, OPTIONS");

           response.addHeader("Access-Control-Allow-Headers","origin, content-type, accept, x-requested-with, sid, mycustom,smuser");

           response.addHeader("Access-Control-Max-Age","1800");//30 min

       }

       filterChain.doFilter(request, response);

    }

 

@Override

public void destroy() {

       //TODO Auto-generated method stub

      

}

 

@Override

public void doFilter(ServletRequestservletRequest, ServletResponse servletResponse, FilterChain filterChain)

              throwsIOException, ServletException {

 

       HttpServletRequesthttpServletRequest = (HttpServletRequest) servletRequest;

       HttpServletResponsehttpServletResponse = (HttpServletResponse) servletResponse;

   if(httpServletRequest.getHeader("Access-Control-Request-Method") !=null && "OPTIONS".equals(httpServletRequest.getMethod())) {

       // CORS "pre-flight" request

           httpServletResponse.addHeader("Access-Control-Allow-Origin","*");

           httpServletResponse.addHeader("Access-Control-Allow-Methods","GET, POST, PUT, DELETE, OPTIONS");

           httpServletResponse.addHeader("Access-Control-Allow-Headers","origin, content-type, accept, x-requested-with, sid, mycustom,smuser");

           httpServletResponse.addHeader("Access-Control-Max-Age","1800");//30 min

    }

   filterChain.doFilter(servletRequest, servletResponse);

 

}

 

@Override

public void init(FilterConfig arg0) throwsServletException {

       //TODO Auto-generated method stub

      

}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2. web.xml 

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

<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>service.print.cloud</display-name>
    <filter>
        <filter-name>MyCORS</filter-name>
        <filter-class> com.bdznxt.util.Filter</filter-class>
      
    </filter>
    <filter-mapping>
        <filter-name> MyCORS </filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
       <welcome-file>index.html</welcome-file>
       <welcome-file>index.htm</welcome-file>
       <welcome-file>index.jsp</welcome-file>
       <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
       <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

 

 

 

 

 

 

 

 

 

CXF-rs解决方案: 依赖JAR包cxf-rt-rs-security-cors-3.0.0.jar,

对应版本自已去Maven仓库或者CXF官网下载

http://my.oschina.net/huangyong/blog/294324#OSC_h1_3

首先,添加 Maven 依赖:

 
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-rs-security-cors</artifactId>
    <version>${cxf.version}</version>
</dependency>

然后,添加 CXF 配置:

 
<jaxrs:server address="/rest">
    <jaxrs:serviceBeans>
        <ref bean="productServiceImpl"/>
    </jaxrs:serviceBeans>
    <jaxrs:providers>
        <bean class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
        <bean class="org.apache.cxf.rs.security.cors.CrossOriginResourceSharingFilter">
            <property name="allowOrigins" value="http://localhost"/>
        </bean>
    </jaxrs:providers>
</jaxrs:server>

在 CrossOriginResourceSharingFilter 中配置 allowOrigins 属性,将其设置为客户端的域名,示例中为“http://localhost”,需根据实际情况进行设置。

J2EE跨域资源共享的解决方案 看完这些还不能解决你的问题,联系QQ:1552298726 通用解决方案:需要依耐下面两个JAR包cors-filter-1.7.jar,java-property-utils-1.9.jar 工程项目中web.xml中的配

相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。