主要實(shí)現(xiàn)對(duì)在白名單中的service級(jí)別或者api級(jí)別的網(wǎng)關(guān)路由。
創(chuàng)新互聯(lián)公司主營(yíng)永和網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都app開發(fā),永和h5小程序制作搭建,永和網(wǎng)站營(yíng)銷推廣歡迎永和等地區(qū)企業(yè)咨詢
1.service級(jí)別的網(wǎng)關(guān)路由
public class ServiceIdWhiteTableRouteLocator extends DiscoveryClientRouteLocator {
...
//主要重寫該方法,在調(diào)用完super的locateRoutes后再與白名單列表比較,提取出交集
@Override
protected LinkedHashMap<String, ZuulProperties.ZuulRoute> locateRoutes() {
LinkedHashMap<String, ZuulProperties.ZuulRoute> routeMaps = super.locateRoutes();
LinkedHashMap<String, ZuulProperties.ZuulRoute> whiteRouteMaps = new LinkedHashMap<>();
routeMaps.forEach((k, v) -> {
if (PatternMatchUtils.simpleMatch(whites, v.getServiceId())) {
whiteRouteMaps.put(k, v);
}
});
for (ZuulProperties.ZuulRoute route : this.properties.getRoutes().values()) {
whiteRouteMaps.put(route.getPath(), route);
}
return whiteRouteMaps;
}
...
}
2.api級(jí)別的網(wǎng)關(guān)路由
public class PathWhiteTableHandleMapping extends ZuulHandlerMapping {
...
//主要重寫該方法,在原有的ZuulHandlerMapping基礎(chǔ)上添加判斷是否在白名單的邏輯
@Override
protected Object lookupHandler(String urlPath, HttpServletRequest request) throws Exception {
if (this.errorController != null && urlPath.equals(this.errorController.getErrorPath())) {
return null;
}
if (isIgnoredPath(urlPath, this.routeLocator.getIgnoredPaths())) return null;
/**
* 檢查是否在白名單中,不在白名單中的不路由
*/
if (!isInPathWhiteTables(urlPath, this.pathWhites)) return null;
RequestContext ctx = RequestContext.getCurrentContext();
if (ctx.containsKey("forward.to")) {
return null;
}
if (this.dirty) {
synchronized (this) {
if (this.dirty) {
registerHandlers();
this.dirty = false;
}
}
}
return super.lookupHandler(urlPath, request);
}
private boolean isInPathWhiteTables(String urlPath, Collection<String> pathWhites) {
for (String whitePath : pathWhites) {
if (this.pathMatcher.match(whitePath, urlPath)) {
return true;
}
}
return false;
}
public void setPathWhiteTables(Collection<String> whites) {
this.pathWhites = whites;
}
...
}
1.首先卸載zuul自帶的auto config.
@SpringBootApplication(exclude = ZuulProxyAutoConfiguration.class)<br/>
2.需要全量copy三個(gè)類
org.springframework.cloud.netflix.zuul.RibbonCommandFactoryConfiguration
org.springframework.cloud.netflix.zuul.ZuulProxyAutoConfiguration
org.springframework.cloud.netflix.zuul.ZuulServerAutoConfiguration
然后修改ZuulServerAutoConfiguration
中的zuulHandlerMapping的bean注冊(cè):
@Bean(value = "discoveryRouteLocator")
public DiscoveryClientRouteLocator discoveryClientRouteLocator(ServerProperties server, DiscoveryClient discovery) {
//service白名單注入點(diǎn)
return new ServiceIdWhiteTableRouteLocator(server.getServlet().getServletPrefix(), discovery, this.zuulProperties, whiteRouteProperties.getWhiteServices());
}
@Bean(value = "zuulHandlerMapping")
public ZuulHandlerMapping zuulHandlerMapping(RouteLocator routes) {
PathWhiteTableHandleMapping mapping = new PathWhiteTableHandleMapping(routes, zuulController());
mapping.setErrorController(this.errorController);
//路徑白名單注入點(diǎn)
mapping.setPathWhiteTables(whiteRouteProperties.getWhitePaths());
return mapping;
}
其中WhiteRouteProperties是一個(gè)裝載配置屬性的屬性類,自己定義即可。ZuulProxyAutoConfiguration
需要修改其父類為上述的ZuulServerAutoConfigurationn
。
主要是在application.yaml文件中增加:
zuul:
#控制service級(jí)別白名單(list)
white-services:
- 'hello-server'
#控制api級(jí)別白名單(list)
white-paths:
- '/hello/world'
routes:
- url: hello-server
path: /hello/**
#默認(rèn)全部不路由
ignored-services: '*'
上述配置可以實(shí)現(xiàn)將/hello/**
該pattern請(qǐng)求路由到hello-server上,由于默認(rèn)設(shè)置全部不路由,通過zuul.routes加進(jìn)去(看源碼實(shí)現(xiàn)),然后由于設(shè)置了白名單功能,需要在white-services
上加上hello-server
,而white-paths
主要是控制白名單中的某個(gè)service中具體的哪個(gè)api可以被路由,如上可知是僅有/hello/world
可以被路由處理。
這樣就實(shí)現(xiàn)了多維度的白名單路由處理。
如有不足,請(qǐng)不吝賜教。
當(dāng)前文章:spring-cloud中zuul自定義service級(jí)別,api級(jí)別的路由白名單
文章路徑:http://www.rwnh.cn/article16/psgpgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站排名、域名注冊(cè)、云服務(wù)器、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)