You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Spring Cloud Gateway Server MVC rateLimit filter(Bucket4jFilter) will got UnsupportedOperationException when response headers is ReadOnlyHttpHeaders
#3345
Open
zzphyh opened this issue
Apr 10, 2024
· 2 comments
Describe the bug
I use rateLimit(Bucket4jFilter) filter with a handler, the handler will return response as below.
returnServerResponse.ok().body(result);
For that response, the serverResponse.headers() will return a ReadOnlyHttpHeaders. But the Bucket4jFilter will try to add a header into the serverResponse.headers(), that will cause java.lang.UnsupportedOperationException.
if (allowed) {
ServerResponseserverResponse = next.handle(request);
serverResponse.headers().add(config.getHeaderName(), String.valueOf(remainingTokens));
returnserverResponse;
}
I use Spring Cloud Gateway Server MVC version 4.1.2
public abstract class HandlerFunctions {
public static HandlerFunction<ServerResponse> dubbo() {
return new DubboHandler();
}
public static class MyHandlerSupplier implements HandlerSupplier {
@Override
public Collection<Method> get() {
return Arrays.asList(HandlerFunctions.class.getMethods());
}
}
}
public class DubboHandler implements HandlerFunction<ServerResponse> {
@Override
public ServerResponse handle(@Nonnull ServerRequest request) {
try {
final JSONObject result = callDubboRPC(request);
return ServerResponse.ok().body(result);
} catch (Exception e) {
log.error("forward to rpc failed.", e);
return ServerResponse.status(BAD_GATEWAY).body(new Result<>(HTTP_FORWARD_DUBBO_ERROR));
}
}
}
The routing configuration is similar to the following (excluding the rate-limiting part):
Describe the bug
I use rateLimit(Bucket4jFilter) filter with a handler, the handler will return response as below.
For that response, the serverResponse.headers() will return a ReadOnlyHttpHeaders. But the Bucket4jFilter will try to add a header into the serverResponse.headers(), that will cause java.lang.UnsupportedOperationException.
I use Spring Cloud Gateway Server MVC version 4.1.2
org.springframework.cloud
spring-cloud-starter-gateway-mvc
4.1.2
The text was updated successfully, but these errors were encountered: