服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - java后端解决跨域的几种问题解决

java后端解决跨域的几种问题解决

2021-08-27 11:53晓宇码匠 Java教程

这篇文章主要介绍了java后端解决跨域的几种问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1.java过滤器过滤

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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中需要添加如下配置:

?
1
2
3
4
5
6
7
8
9
<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>

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

?
1
2
3
4
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转发进行转发(简单的例子 不推荐使用这种方式)

?
1
2
3
4
5
6
7
8
9
10
11
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 上的跨域 用以下代码配置 即可完美解决你的前后端跨域请求问题

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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

?
1
2
3
4
5
6
7
8
9
10
11
12
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转发机制就可以完成跨域问题

?
1
2
3
4
5
6
7
8
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 跨域内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_37651267/article/details/93367870

延伸 · 阅读

精彩推荐