iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >java后端解决跨域的几种问题解决
  • 902
分享到

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

2024-04-02 19:04:59 902人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

1.java过滤器过滤 允许整个项目跨域访问,可通过filter来进行过虑: public class SimpleCORSFilter implements Filter{

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>
@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("*");
    
    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 跨域内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: java后端解决跨域的几种问题解决

本文链接: https://www.lsjlt.com/news/122235.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作