java後端解決跨域的幾種問題解決

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 跨域內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!