[英]Spring WebFlux add WebFIlter to match specific paths
在Spring啟動應用程序的上下文中,我嘗試添加WebFilter以僅過濾與特定路徑匹配的請求。
到目前為止,我有一個過濾器:
@Component
public class AuthenticationFilter implements WebFilter {
@Override
public Mono<Void> filter(ServerWebExchange serverWebExchange,
WebFilterChain webFilterChain) {
final ServerHttpRequest request = serverWebExchange.getRequest();
if (request.getPath().pathWithinApplication().value().startsWith("/api/product")) {
// logic to allow or reject the processing of the request
}
}
}
我想要實現的是從過濾器中刪除路徑匹配並將其添加到其他更合適的地方,例如,從我到目前為止所讀到的,一個SecurityWebFilterChain
。
非常感謝!
我也許是一種解決問題的更簡潔方法。 它基於UrlBasedCorsConfigurationSource中的代碼。 它使用適合您需求的PathPattern 。
@Component
public class AuthenticationFilter implements WebFilter {
private final PathPattern pathPattern;
public AuthenticationFilter() {
pathPattern = new PathPatternParser().parse("/api/product");
}
@Override
public Mono<Void> filter(ServerWebExchange serverWebExchange,
WebFilterChain webFilterChain) {
final ServerHttpRequest request = serverWebExchange.getRequest();
if (pathPattern.matches(request.getPath().pathWithinApplication())) {
// logic to allow or reject the processing of the request
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.