我已经在spring gateway中定义了一个JwtAuthenticationGatewayFilter
,现在我定义了一个列表,其中存储了需要忽略的url,令牌有效,如下所示:
public static Predicate<ServerHttpRequest> isApiSecured;
@PostConstruct
public void initExcludePath(){
isApiSecured = r -> {
List<String> excludeList = loginExcludeUrl.getExclude();
return excludeList.stream()
.noneMatch(uri -> r.getURI().getPath().equals(uri));};
}
这loginExcludeUrl.getExclude()
将得到这忽略网址列表(数百或数千).当在这过滤器使用这功能知道忽略网址与否:
if (isApiSecured.test(request)) {
return authImpl(request, exchange, chain);
}
我担心当忽略列表增加时,整个网站的性能会变慢,因为每个请求都应该应用列表过滤器。有没有更好的方法来忽略网址?一些网址配置如下:
login-check-path:
exclude:
- /post/user/reg
- /login
- /post/user/login
- /post/user/plugin/login
- /post/user/sms
- /post/auth/access_token/refresh
- /post/auth/refresh_token/refresh
- /post/article/newstories
- /post/article/originalstories
- /post/article/officialstories
- /post/article/share
- /post/article/read
- /post/article/detail
- /post/wechat/util/verifyWxToken
- /post/wechat/login/getQRCodeUrl
- /post/wechat/login/wxCallBack
- /post/alipay/login/alipayCallBack
- /post/alipay/login/getQRCodeUrl
- /post/alipay/notification/v1/alipaySeverNotification
- /post/websocket
- /manage/admin/user/login
- /dict/user/plugin/login
- /dict/user/login
- /dict/auth/access_token/refresh
- /dict/auth/refresh_token/refresh
- /fortune/user/login
- /fortune/user/guest/login
- /fortune/user/sms
- /fortune/user/reg/verify
- /fortune/auth/access-token/refresh
- /tik/user/login
- /tik/user/guest/login
- /tik/user/sms
- /tik/user/reg/verify
1条答案
按热度按时间uyhoqukh1#
由于您将公司的所有请求集中在一个网关中,添加或排除某些url的唯一方法是逐个Map,然后核心应该逐个询问传入的url是否应该受到保护。
在特定路由上应用jwt
例如,在Kong Gateway的情况下,如果您不使用add explicitly the route,则任何主体都无法使用该路由,因为没有启用公共访问
另一种方法是,您可以创建API,然后在每个API上决定是否添加JWT插件。
资料来源:
因此,您无需逐个添加排除项。
我认为你可以只添加你需要使用这个yaml的路线
来源:https://www.javainuse.com/spring/cloud-gateway
使用正则表达式排除
使用正则表达式可以加快比较速度
您可以尝试类似于Spring Security中使用的regex或matchers:
因此代码可能如下所示(未经测试):
使用无关系数据库或内存中数据库
要加载排除列表,您可以使用mongodb或redis,这样您将能够处理大量排除项,并查询它们以决定传入的url是否应该受到保护
使用另一个API网关
您可以只对公共或不受保护的路由使用另一个网关,甚至可以使用简单的nginx,因为这些端点不应该受到保护
我希望这些选项之一能帮助您