Zuul:跨域
前端是通過ajax發送請求,瀏覽器的ajax,是有同源策略的,如果違反了同源策略,就會產生跨域問題,Zuul作為微服務的網關,在他上面處理跨域,也是一種選擇,Zuul的跨域其實可以看成是Spring的跨域,Spring的跨域常用的一種方法,是在服務被調用的類或者方法上,增加@CrossOrgin注解,來聲明自己支持跨域訪問,這種方式的缺點其實挺明顯的,他的作用域是在類或者方法上,你看我們這光應用就有好多個,那一個應用里的類或者方法,就更加的多了,另外一種方法呢,是在Zuul里增加@CrosFilter過濾器,此方法是增加在網關上,對內部的代碼無任何改造
在Product項目里面,如果我們要對某個接口跨域的話,你要讓他實現跨域,要怎么做呢,用@CrossOrigin這個注解,@GetMapping("/list")
@CrossOrigin
public List<ProductInfo> list(HttpServletRequest request) {Enumeration<String> headerNames = request.getHeaderNames();while(headerNames.hasMoreElements()) {String headerName = headerNames.nextElement();String headerValue = request.getHeader(headerName);System.out.println(headerName + "========>" + headerValue);}//1. 查詢所有在架的商品List<ProductInfo> productInfoList = productService.findUpAll();return productInfoList;
}這個注解就可以實現對跨域訪問了,我們看一下里面有哪些參數,這些參數沒有什么問題,allowCredentials你設為true的話,表示允許cookie跨域/*** Whether the browser should include any cookies associated with the* domain of the request being annotated.* <p>Set to {@code "false"} if such cookies should not included.* An empty string ({@code ""}) means <em>undefined</em>.* {@code "true"} means that the pre-flight response will include the header* {@code Access-Control-Allow-Credentials=true}.* <p>If undefined, credentials are allowed.*/
String allowCredentials() default "";值得注意的就是這個字段,其他的大家看看都知道,在某個項目Controller里面,如果要對單個接口進行跨域的話,怎么來設置,那想跨域的接口特別多,我需要統一的來設置怎么做呢,這里新建一個配置,再建一個類,Cros是四個單詞的縮寫 Cross Origin Resource Sharing翻譯成中外過來就是,跨域資源共享,其實這里就是要做一個配置,這里有一個現成的類,CorsFilter,注意這個包名,是Spring框架里面的org.springframework.web.filter.CorsFilter只需要對這個類配置一下就好了,這里面需要配一個CorsConfigurationSourceorg.springframework.web.cors.CorsConfigurationSource我們使用UrlBasedCorsConfigurationSource這個org.springframework.web.cors.UrlBasedCorsConfigurationSource注意看包名,我們要把跨域的配置給他注冊到source上面去,路徑是哪些域名進行配置,我們可以設置為所有source.registerCorsConfiguration("/**", buildConfig()); // 4CorsConfiguration這里就是我們要配置的真正配置了,看一下哪些可以配置的,其實跟剛剛的注解其實是完全一樣的,第一個是否支持Cookie跨域corsConfiguration.setAllowCredentials(true);這個是放哪些原始域,他要放的是一個listcorsConfiguration.addAllowedOrigin("http://localhost:3030");所有的話你就放一個星號,原始域你自己定義的域名就好,還有允許的頭corsConfiguration.addAllowedHeader("*"); // 2也可以和上面一樣,全部允許,還有方法,GET還是POSTcorsConfiguration.addAllowedMethod("*"); // 3允許所有的話就填星號,還有一個是緩存時間corsConfiguration.setMaxAge(300L);在這個時間段里,對于相同的跨域請求,他就不再進行一個檢查了,比如你可以填個300秒,這就是在Zuul里面配置一個跨域,因為跨域都是由前端JS發起,那么我們這個項目都在寫后端,都在調試,跨域這塊大家可以當做了解,因為處理跨域的解決方案,不一定非得這么來做,有很多時候可以考慮在Nginx上解決,如果對跨域非常感興趣的小伙伴,通用的解決方案,我之前也了解跨域,也處理過跨域的一些問題
package com.learn.cloud.config;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 Leon.Sun**/@Configuration
public class CorsConfig {private CorsConfiguration buildConfig() {CorsConfiguration corsConfiguration = new CorsConfiguration();// 你需要跨域的地址 注意這里的 127.0.0.1 != localhost// * 表示對所有的地址都可以訪問corsConfiguration.addAllowedOrigin("http://localhost:3030");// 跨域的請求頭corsConfiguration.addAllowedHeader("*"); // 2// 跨域的請求方法corsConfiguration.addAllowedMethod("*"); // 3//加上了這一句,大致意思是可以攜帶 cookie//最終的結果是可以 在跨域請求的時候獲取同一個 sessioncorsConfiguration.setAllowCredentials(true);
// corsConfiguration.setMaxAge(10L);return corsConfiguration;}@Beanpublic CorsFilter corsFilter() {UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();//配置 可以訪問的地址source.registerCorsConfiguration("/**", buildConfig()); // 4return new CorsFilter(source);}
}
?
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
- 上一篇: 完成权限校验
- 下一篇: 服务容错和Hystrix