1.java过滤器过滤

允许整个项目跨域访问,可通过filter来进行过虑:

public class simplecorsfilter implements filter{ 
 
  @override 
  public void destroy() { 
     
  } 
 
  @override 
  public void dofilter(servletrequest req, servletresponse res, 
      filterchain chain) throws ioexception, servletexception { 
      httpservletresponse response = (httpservletresponse) res; 
      response.setheader("access-control-allow-origin", "*"); 
      response.setheader("access-control-allow-methods", "post, get, options, delete"); 
      response.setheader("access-control-max-age", "3600"); 
      response.setheader("access-control-allow-headers", "x-requested-with"); 
      chain.dofilter(req, res); 
     
  } 
 
  @override 
  public void init(filterconfig arg0) throws servletexception { 
     
  }  
}

在web.xml中需要添加如下配置:

<filter> 
   <filter-name>cors</filter-name> 
   <filter-class>com.ssm.web.filter.simplecorsfilter</filter-class> 
  </filter> 
  <filter-mapping> 
   <filter-name>cors</filter-name> 
   <url-pattern>/*</url-pattern> 
  </filter-mapping>
</filter>

为单个方法提供跨域访问,直接添加请求头:

    response.setheader("access-control-allow-origin", "*"); 
      response.setheader("access-control-allow-methods", "post, get, options, delete"); 
      response.setheader("access-control-max-age", "3600"); 
      response.setheader("access-control-allow-headers", "x-requested-with");

2.后台http请求转发

使用httpclinet转发进行转发(简单的例子 不推荐使用这种方式)

try {
  httpclient client = httpclients.createdefault();      //client对象
  httpget get = new httpget("http://localhost:8080/test");  //创建get请求
  closeablehttpresponse response = httpclient.execute(get);  //执行get请求
  string mes = entityutils.tostring(response.getentity());  //将返回体的信息转换为字符串
  system.out.println(mes);
} catch (clientprotocolexception e) {
  e.printstacktrace();
} catch (ioexception e) {
  e.printstacktrace();
}

3、后台配置同源cors (推荐)

在springboot2.0 上的跨域 用以下代码配置 即可完美解决你的前后端跨域请求问题

import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.web.cors.corsconfiguration;
import org.springframework.web.cors.urlbasedcorsconfigurationsource;
import org.springframework.web.filter.corsfilter;
/**
 * 实现基本的跨域请求
 * @author linhongcun
 *
 */
@configuration
public class corsconfig {
  @bean
  public corsfilter corsfilter() {
    final urlbasedcorsconfigurationsource urlbasedcorsconfigurationsource = new urlbasedcorsconfigurationsource();
    final corsconfiguration corsconfiguration = new corsconfiguration();
    /*是否允许请求带有验证信息*/
    corsconfiguration.setallowcredentials(true);
    /*允许访问的客户端域名*/
    corsconfiguration.addallowedorigin("*");
    /*允许服务端访问的客户端请求头*/
    corsconfiguration.addallowedheader("*");
    /*允许访问的方法名,get post等*/
    corsconfiguration.addallowedmethod("*");
    urlbasedcorsconfigurationsource.registercorsconfiguration("/**", corsconfiguration);
    return new corsfilter(urlbasedcorsconfigurationsource);
  }
}

4、使用springcloud网关

服务网关(zuul)又称路由中心,用来统一访问所有api接口,维护服务。
spring cloud zuul通过与spring cloud eureka的整合,实现了对服务实例的自动化维护,所以在使用服务路由配置的时候,我们不需要向传统路由配置方式那样去指定具体的服务实例地址,只需要通过ant模式配置文件参数即可

5、使用nginx做转发

现在有两个网站想互相访问接口 在http://a.a.com:81/a中想访问 http://b.b.com:81/b 那么进行如下配置即可
然后通过访问 www.my.com/a 里面即可访问 www.my.com/b

server {
    listen    80;
    server_name www.my.com;
    location /a {
      proxy_pass http://a.a.com:81/a;
      index index.html index.htm;
    }
    location /b {
      proxy_pass http://b.b.com:81/b;
      index index.html index.htm;
    }
  }

如果是两个端口想互相访问接口 在http://b.b.com:80/api中想访问 http://b.b.com:81/api 那么进行如下配置即可
使用nginx转发机制就可以完成跨域问题

server {
    listen    80;
    server_name b.b.com;
    location /api {
      proxy_pass http://b.b.com:81/api;
      index index.html index.htm;
    }
  }

到此这篇关于java后端解决跨域的几种问题解决的文章就介绍到这了,更多相关java 跨域内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!