Zuul:Cookie和动态路由
生活随笔
收集整理的這篇文章主要介紹了
Zuul:Cookie和动态路由
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
經常會涉及到Cookie,在這里使用到了Zuul這個組件,Cookie它是無法傳遞過去的,我們先來實驗一下,看一下這個請求有沒有帶Cookielocalhost:8040/myProduct/product/list@RestController
@RequestMapping("/product")
public class ProductController {@Autowiredprivate ProductService productService;/*** 1. 查詢所有在架的商品*/@GetMapping("/list")public List<ProductInfo> list(HttpServletRequest Request) {//1. 查詢所有在架的商品List<ProductInfo> productInfoList = productService.findUpAll();return productInfoList;}}從request里面?zhèn)鬟f過來/*** The service ID (if any) to map to this route. You can specify a physical URL or* a service, but not both.*/
private String serviceId;他都是作為這個對象的一些字段,這里有兩個字段/*** List of sensitive headers that are not passed to downstream requests. Defaults* to a "safe" set of headers that commonly contain user credentials. It's OK to* remove those from the list if the downstream service is part of the same system* as the proxy, so they are sharing authentication data. If using a physical URL* outside your own domain, then generally it would be a bad idea to leak user* credentials.*/
private Set<String> sensitiveHeaders = new LinkedHashSet<>();敏感頭,所謂敏感是不讓看的,敏感頭就不會給后面的服務了,默認值是什么/*** List of sensitive headers that are not passed to downstream requests. Defaults to a* "safe" set of headers that commonly contain user credentials. It's OK to remove* those from the list if the downstream service is part of the same system as the* proxy, so they are sharing authentication data. If using a physical URL outside* your own domain, then generally it would be a bad idea to leak user credentials.*/
private Set<String> sensitiveHeaders = new LinkedHashSet<>(Arrays.asList("Cookie", "Set-Cookie", "Authorization"));默認對三個字段,Cookie這三個,那么我們現在想讓他不過濾掉Cookie,你只需要把它設置為空就好了,zuul.routes.myProduct.sensitiveHeaders=這里報了一個錯超時,因為我們打了一個斷點,沒關系,再來刷新一下,跳到打斷點的地方,已經可以獲取到cookie了這就是敏感頭的過濾,大家以后有需要的話加上這么一個配置,對于Zuul還有什么需要改進的呢,一旦我們再增加一個路由的話,那我是不是得再配置一遍,我再重啟一下,我們能不能做大動態(tài)路由呢,也就是改了配置直接讓他生效,這不就是動態(tài)配置嗎,我們之前也有學到過,只要把這一塊的配置放到Git上面來,名字叫api-gateway-dev.yml我先全部復制過來,動態(tài)配置我們之前已經講過,Zuul動態(tài)配置需要注意的點,第一點就是我們提到的,Spring Cloud Bus版本問題,這邊你得改一下,他在這個版本上有一些Bug,這是第一點,我們如何把這上面的配置改動了之后,你代碼里面要做到動態(tài)更新,創(chuàng)建一個ZuulConfigimport org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.stereotype.Component;@Component
public class ZuulConfig {@ConfigurationProperties("zuul")@RefreshScopepublic ZuulProperties zuulProperties() {return new ZuulProperties();}
}大家肯定想到了一個前綴,這個前綴,那里面怎么寫呢,寫哪些屬性呢,其實你是不需要寫屬性的,Zuul已經寫過了有哪些屬性,所以我們只需要查到他的屬性,剛剛從屬性里面點進去,這個類還在,其實就是這個類,跟我們的寫法都是一樣的@Data
@ConfigurationProperties("zuul")
public class ZuulProperties {org.springframework.cloud.netflix.zuul.filters.ZuulProperties把這些配置寫到這個類里面來,你只需要這么寫,當然這個地方注意,你要把這些配置寫到這兒來了,@RefreshScope,這樣寫就可以完成動態(tài)注入,如果覺得沒有必要新建一個類,你也可以這樣子來寫,把它復制一下,你可以直接寫到啟動類里面,@EnableZuulProxy
@SpringBootApplication
public class APIGatewayApplication {public static void main(String[] args) {SpringApplication.run(APIGatewayApplication.class, args);}@ConfigurationProperties("zuul")@RefreshScopepublic ZuulProperties zuulProperties() {return new ZuulProperties();}
}這樣子也是可以的,動態(tài)路由希望自己去實現
?
總結
以上是生活随笔為你收集整理的Zuul:Cookie和动态路由的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Zuul:路由转发,排除和自定义
- 下一篇: Zuul:路由和高可用小结