admin管理员组文章数量:1125883
I'm trying to return a response in my gateway depend on values if found in redis without hitting the controller by but it always hit the controller and while returning the NettyRoutingFilter tries to remove a Header and then it throws UnsupportedOperationException
since that the response is already commited
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
ServerHttpResponse response = exchange.getResponse();
String api = apis.keySet().stream()
.filter(e -> request.getURI().getPath().endsWith(e))
.findFirst()
.orElse(null);
if (api == null) {
return chain.filter(exchange);
}
return ReactiveSecurityContextHolder.getContext()
.flatMap(sc -> {
try {
Authentication authentication = sc.getAuthentication();
if (authentication == null || authentication.getPrincipal() == null) {
return chain.filter(exchange);
}
Set<String> urlKey = generateCacheKey(request, apis.get(api));
List<Object> cachedResponse = redisTemplate.opsForValue().multiGet(urlKey);
if (cachedResponse != null && !cachedResponse.isEmpty()) {
return writeCachedResponse(response, cachedResponse.get(0).toString());
} else {
return chain.filter(exchange);
}
} catch (Exception e) {
return Mono.error(e);
}
})
.switchIfEmpty(Mono.defer(() -> {
return chain.filter(exchange);
}))
.doOnError(e -> log.error("Error during filter execution", e));
}
private Set<String> generateCacheKey(ServerHttpRequest request, String cacheKey) {
return redisTemplate.keys("*" + cacheKey + "*");
}
private Mono<Void> writeCachedResponse(ServerHttpResponse response, String cachedResponse) {
return response.writeWith(Mono.just(response.bufferFactory().wrap(cachedResponse.getBytes())));
}
expects to terminate the chain and returns the response directly
本文标签:
版权声明:本文标题:java - Returns the response directly without chaining filters in Spring WebFlux - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736662981a1946511.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论