Java 如何与netty CorsHandler保持密切联系
你是如何让netty的联系紧密的?默认情况下,当原点通过时关闭,但当不允许原点时不关闭。我使用CorsHandler实例设置了这样的服务器Java 如何与netty CorsHandler保持密切联系,java,cors,netty,Java,Cors,Netty,你是如何让netty的联系紧密的?默认情况下,当原点通过时关闭,但当不允许原点时不关闭。我使用CorsHandler实例设置了这样的服务器 import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; impo
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();
}
}
}
导入io.netty.bootstrap.ServerBootstrap;
导入io.netty.channel.*;
导入io.netty.channel.nio.NioEventLoopGroup;
导入io.netty.channel.socket.nio.NioServerSocketChannel;
导入io.netty.handler.codec.http.HttpMethod;
导入io.netty.handler.codec.http.HttpObjectAggregator;
导入io.netty.handler.codec.http.HttpServerCodec;
导入io.netty.handler.codec.http.cors.CorsConfigBuilder;
导入io.netty.handler.codec.http.cors.CorsHandler;
导入io.netty.handler.logging.LogLevel;
导入io.netty.handler.logging.LoggingHandler;
/**
*与8080上的CORS处理程序一起运行netty
*/
公共类NettyCorsApp{
专用静态最终int端口=8080;
公共静态void main(字符串[]args)引发异常{
EventLoopGroup EventLoopGroup=新的NioEventLoopGroup();
试一试{
ServerBootstrap bootstrap=newserverbootstrap()
.group(eventLoopGroup)
.handler(新的LoggingHandler(LogLevel.INFO))
.childHandler(新的通道初始值设定项(){
@凌驾
受保护的无效初始化通道(通道ch)引发异常{
ChannelPipeline=通道管道();
addLast(新的HttpServerCodec());
pipeline.addLast(新的HttpObjectAggregator(1024*1024));//1MB
pipeline.addLast(新CorsHandler(
CorsConfigBuilder.forOrigin(“http://example.com")
.allowedRequestMethods(HttpMethod.POST)
.build())
);
}
})
.channel(NioServerSocketChannel.class);
Channel=bootstrap.bind(PORT.sync().Channel();
channel.closeFuture().sync();
}最后{
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
$curl-sv-X选项-H'原点:http://example.com“-H”访问控制请求方法:POST”http://localhost:8080
*重新生成的URL到:http://localhost:8080/
*正在尝试::1。。。
*TCP_节点集
*已连接到本地主机(::1)端口8080(#0)
>选项/HTTP/1.1
>主机:本地主机:8080
>用户代理:curl/7.54.0
>接受:*/*
>来源:http://example.com
>访问控制请求方法:POST
>
但是,当您从未通过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
<
$curl-sv-X选项-H'原点:http://invalid.com“-H”访问控制请求方法:POST”http://localhost:8080
*重新生成的URL到:http://localhost:8080/
*正在尝试::1。。。
*TCP_节点集
*已连接到本地主机(::1)端口8080(#0)
>选项/HTTP/1.1
>主机:本地主机:8080
>用户代理:curl/7.54.0
>接受:*/*
>来源:http://invalid.com
>访问控制请求方法:POST
>
这可能是netty中的一个bug,如果是的话,我将在那里提交。按照@Ferrybig指出的,CorsHandler没有故意关闭连接,因为请求中没有
连接:close
头。Http 1.0假定tcp连接在默认情况下应该关闭,而Http 1.1(默认情况下,它的结果)应该保持打开状态
curl没有在错误源上退出的原因是CorsHandler
在其响应中没有包含Content-Length
头,这是一个bug
我使用netty 4.1.6-Final和4.1.16-Final进行了测试,结果相同。堆栈溢出不是错误报告站点,您应该在github上的netty问题部分报告错误:另外,在上面的示例中,netty没有关闭连接(没有连接头)。您是对的,感谢您指出这一点。我不太明白连接保持活动与关闭以及http 1.0与1.1,但我现在明白了,看看你们在说什么。我确实追踪到了我在这里提交的netty中的一个bug:我在这里问,不知道这是否是我在第一篇文章中所说的bug。如果我知道这是一个bug,我肯定不会这么做。