【发布时间】:2018-03-08 04:32:30
【问题描述】:
如何让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的一个bug,如果有我会在那里提交。
【问题讨论】:
-
我用netty 4.1.6-Final和4.1.16-Final测试过,结果一样。
-
Stack Overflow 不是错误报告站点,您应该在 github 上的 netty 问题部分报告错误:github.com/netty/netty/issues 另外,在上面的示例中,netty 不会关闭连接(没有存在连接头)
-
你是对的,感谢您指出这一点。我不太了解连接保持活动与关闭以及 http 1.0 与 1.1,但我现在明白了,看看你在说什么。我确实追踪到我在此处提交的 netty 中的一个错误:github.com/netty/netty/pull/7261 我在这里问过,不知道这是否是我在第一篇文章中所说的错误。如果我知道这是一个错误,绝对不会提交给 SO。