Spring WebFlux 块 Http.CONNECT 方法
Spring WebFlux block Http.CONNECT method
我们在公司周围进行了一些安全测试,其中以不同的方式测试应用程序。其中之一是尝试像这样的 CONNECT:
telnet localhost 8080
CONNECT http://test.com HTTP/1.1
在这种情况下 return 一个 400
或 405
。现有的 Spring MVC apps return a 400
,但似乎我们新的 Spring WebFlux:5.1.2.RELEASE
app(Netty server) return a 200
.
我做的第一件事是切换到最新的 spring WebFlux 版本:5.1.4.RELEASE
,在这种情况下,响应 http 错误代码是:404
,但仍然还不够好。所以我尝试:
- 创建网络过滤器
- 修改 CORS 过滤器
- 修改Spring安全链
,但所有这些解决方案都失败了。你怎么解决的?创建自定义 http 处理程序是个好主意?
我已经创建了一个自定义的 http 处理程序:
public class AppContextPathCompositeHandler extends ContextPathCompositeHandler {
public AppContextPathCompositeHandler(Map<String, ? extends HttpHandler> handlerMap) {
super(handlerMap);
}
@Override
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
if (HttpMethod.CONNECT.name().equals(request.getMethodValue())) {
response.setStatusCode(HttpStatus.METHOD_NOT_ALLOWED);
return response.setComplete();
}
return super.handle(request, response);
}
}
配置如下:
@Configuration
public class NettyReactiveWebServerConfig extends NettyReactiveWebServerFactory {
@Value("${server.context-path}")
private String contextPath;
@Override
public WebServer getWebServer(HttpHandler httpHandler) {
Map<String, HttpHandler> handlerMap = new HashMap<>();
handlerMap.put(contextPath, httpHandler);
return super.getWebServer(new AppContextPathCompositeHandler(handlerMap));
}
}
我们在公司周围进行了一些安全测试,其中以不同的方式测试应用程序。其中之一是尝试像这样的 CONNECT:
telnet localhost 8080
CONNECT http://test.com HTTP/1.1
在这种情况下 return 一个 400
或 405
。现有的 Spring MVC apps return a 400
,但似乎我们新的 Spring WebFlux:5.1.2.RELEASE
app(Netty server) return a 200
.
我做的第一件事是切换到最新的 spring WebFlux 版本:5.1.4.RELEASE
,在这种情况下,响应 http 错误代码是:404
,但仍然还不够好。所以我尝试:
- 创建网络过滤器
- 修改 CORS 过滤器
- 修改Spring安全链
,但所有这些解决方案都失败了。你怎么解决的?创建自定义 http 处理程序是个好主意?
我已经创建了一个自定义的 http 处理程序:
public class AppContextPathCompositeHandler extends ContextPathCompositeHandler {
public AppContextPathCompositeHandler(Map<String, ? extends HttpHandler> handlerMap) {
super(handlerMap);
}
@Override
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
if (HttpMethod.CONNECT.name().equals(request.getMethodValue())) {
response.setStatusCode(HttpStatus.METHOD_NOT_ALLOWED);
return response.setComplete();
}
return super.handle(request, response);
}
}
配置如下:
@Configuration
public class NettyReactiveWebServerConfig extends NettyReactiveWebServerFactory {
@Value("${server.context-path}")
private String contextPath;
@Override
public WebServer getWebServer(HttpHandler httpHandler) {
Map<String, HttpHandler> handlerMap = new HashMap<>();
handlerMap.put(contextPath, httpHandler);
return super.getWebServer(new AppContextPathCompositeHandler(handlerMap));
}
}