java后端解決跨域的幾種問(wèn)題解決
允許整個(gè)項(xiàng)目跨域訪問(wèn),可通過(guò)filter來(lái)進(jìn)行過(guò)慮:
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>
為單個(gè)方法提供跨域訪問(wèn),直接添加請(qǐng)求頭:
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.后臺(tái)Http請(qǐng)求轉(zhuǎn)發(fā)
使用HttpClinet轉(zhuǎn)發(fā)進(jìn)行轉(zhuǎn)發(fā)(簡(jiǎn)單的例子 不推薦使用這種方式)
try { HttpClient client = HttpClients.createDefault(); //client對(duì)象 HttpGet get = new HttpGet('http://localhost:8080/test'); //創(chuàng)建get請(qǐng)求 CloseableHttpResponse response = httpClient.execute(get); //執(zhí)行g(shù)et請(qǐng)求 String mes = EntityUtils.toString(response.getEntity()); //將返回體的信息轉(zhuǎn)換為字符串 System.out.println(mes);} catch (ClientProtocolException e) { e.printStackTrace();} catch (IOException e) { e.printStackTrace();}3、后臺(tái)配置同源Cors (推薦)
在SpringBoot2.0 上的跨域 用以下代碼配置 即可完美解決你的前后端跨域請(qǐng)求問(wèn)題
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;/** * 實(shí)現(xiàn)基本的跨域請(qǐng)求 * @author linhongcun * */@Configurationpublic class CorsConfig { @Bean public CorsFilter corsFilter() { final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource(); final CorsConfiguration corsConfiguration = new CorsConfiguration(); /*是否允許請(qǐng)求帶有驗(yàn)證信息*/ corsConfiguration.setAllowCredentials(true); /*允許訪問(wèn)的客戶(hù)端域名*/ corsConfiguration.addAllowedOrigin('*'); /*允許服務(wù)端訪問(wèn)的客戶(hù)端請(qǐng)求頭*/ corsConfiguration.addAllowedHeader('*'); /*允許訪問(wèn)的方法名,GET POST等*/ corsConfiguration.addAllowedMethod('*'); urlBasedCorsConfigurationSource.registerCorsConfiguration('/**', corsConfiguration); return new CorsFilter(urlBasedCorsConfigurationSource); }}4、使用SpringCloud網(wǎng)關(guān)
服務(wù)網(wǎng)關(guān)(zuul)又稱(chēng)路由中心,用來(lái)統(tǒng)一訪問(wèn)所有api接口,維護(hù)服務(wù)。Spring Cloud Zuul通過(guò)與Spring Cloud Eureka的整合,實(shí)現(xiàn)了對(duì)服務(wù)實(shí)例的自動(dòng)化維護(hù),所以在使用服務(wù)路由配置的時(shí)候,我們不需要向傳統(tǒng)路由配置方式那樣去指定具體的服務(wù)實(shí)例地址,只需要通過(guò)Ant模式配置文件參數(shù)即可
5、使用nginx做轉(zhuǎn)發(fā)現(xiàn)在有兩個(gè)網(wǎng)站想互相訪問(wèn)接口 在http://a.a.com:81/A中想訪問(wèn) http://b.b.com:81/B 那么進(jìn)行如下配置即可然后通過(guò)訪問(wèn) www.my.com/A 里面即可訪問(wèn) 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; } }
如果是兩個(gè)端口想互相訪問(wèn)接口 在http://b.b.com:80/Api中想訪問(wèn) http://b.b.com:81/Api 那么進(jìn)行如下配置即可使用nginx轉(zhuǎn)發(fā)機(jī)制就可以完成跨域問(wèn)題
server { listen 80; server_name b.b.com; location /Api { proxy_pass http://b.b.com:81/Api; index index.html index.htm; } }
到此這篇關(guān)于java后端解決跨域的幾種問(wèn)題解決的文章就介紹到這了,更多相關(guān)java 跨域內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Java Media Framework 基礎(chǔ)教程2. 解決vue頁(yè)面刷新,數(shù)據(jù)丟失的問(wèn)題3. Python 忽略文件名編碼的方法4. springboot項(xiàng)目整合druid數(shù)據(jù)庫(kù)連接池的實(shí)現(xiàn)5. JavaEE SpringMyBatis是什么? 它和Hibernate的區(qū)別及如何配置MyBatis6. python 讀txt文件,按‘,’分割每行數(shù)據(jù)操作7. android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器(無(wú)bug)8. 在Mac中配置Python虛擬環(huán)境過(guò)程解析9. Python趣味挑戰(zhàn)之用pygame實(shí)現(xiàn)簡(jiǎn)單的金幣旋轉(zhuǎn)效果10. Python中的min及返回最小值索引的操作
