gpt4 book ai didi

java - 如何关闭与netty CorsHandler的连接

转载 作者:行者123 更新时间:2023-12-02 12:12:26 24 4
gpt4 key购买 nike

如何制作netty的CorsHandler关闭它的连接?原点通过时默认关闭,原点不允许时不关闭。我使用 CorsHandler 实例设置了一个这样的服务器。

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.cors.CorsConfigBuilder;
import io.netty.handler.codec.http.cors.CorsHandler;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

/**
* Runs netty with a CORS handler on 8080
*/
public class NettyCorsApp {
private static final int PORT = 8080;

public static void main(String[] args) throws Exception {
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap()
.group(eventLoopGroup)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new HttpServerCodec());
pipeline.addLast(new HttpObjectAggregator(1024 * 1024)); // 1MB
pipeline.addLast(new CorsHandler(
CorsConfigBuilder.forOrigin("http://example.com")
.allowedRequestMethods(HttpMethod.POST)
.build())
);
}
})
.channel(NioServerSocketChannel.class);
Channel channel = bootstrap.bind(PORT).sync().channel();
channel.closeFuture().sync();
} finally {
eventLoopGroup.shutdownGracefully();
}
}
}

当您从通过 CORS 检查的源发出请求时,CorsHandler 会像您期望的那样关闭连接。

$ curl -sv -X OPTIONS -H 'Origin: http://example.com' -H 'Access-Control-Request-Method: POST' http://localhost:8080
* Rebuilt URL to: http://localhost:8080/
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> OPTIONS / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
> Origin: http://example.com
> Access-Control-Request-Method: POST
>
< HTTP/1.1 200 OK
< access-control-allow-origin: http://example.com
< vary: origin
< access-control-allow-methods: POST
< access-control-allow-headers:
< access-control-max-age: 0
< date: "Tue, 26 Sep 2017 20:03:53 GMT"
< content-length: 0
<
* Connection #0 to host localhost left intact

但是当您从未通过 CORS 检查的源发出请求时,它不会关闭连接。

$ curl -sv -X OPTIONS -H 'Origin: http://invalid.com' -H 'Access-Control-Request-Method: POST' http://localhost:8080
* Rebuilt URL to: http://localhost:8080/
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> OPTIONS / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
> Origin: http://invalid.com
> Access-Control-Request-Method: POST
>
< HTTP/1.1 200 OK
* no chunk, no close, no size. Assume close to signal end
<

这可能是netty中的一个错误,如果是的话我会在那里提交。

最佳答案

按照 @Ferrybig 指出的,CorsHandler 不会有意关闭连接,因为请求中没有 Connection: close header 。 Http 1.0 假定 tcp 连接默认关闭,而 http 1.1(curl 默认情况下)应默认保持打开状态。

curl 在错误源上未退出的原因是 CorsHandler 未在其响应中包含 Content-Length header ,这是一个错误。

https://github.com/netty/netty/pull/7261

关于java - 如何关闭与netty CorsHandler的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46435126/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com