当前位置: 首页>>代码示例>>Java>>正文


Java CorsConfig类代码示例

本文整理汇总了Java中io.netty.handler.codec.http.cors.CorsConfig的典型用法代码示例。如果您正苦于以下问题:Java CorsConfig类的具体用法?Java CorsConfig怎么用?Java CorsConfig使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


CorsConfig类属于io.netty.handler.codec.http.cors包,在下文中一共展示了CorsConfig类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: initChannel

import io.netty.handler.codec.http.cors.CorsConfig; //导入依赖的package包/类
@Override
	protected void initChannel(SocketChannel ch) throws Exception {
		ChannelPipeline p = ch.pipeline();
		if(sslCtx!=null)
		{
			p.addLast(new SslHandler(sslCtx.newEngine(ch.alloc())));
		}
		p.addLast(new HttpResponseEncoder());//必须放在最前面,如果decoder途中需要回复消息,则decoder前面需要encoder
		p.addLast(new HttpRequestDecoder());
		p.addLast(new HttpObjectAggregator(65536));//限制contentLength
		//大文件传输处理
//		p.addLast(new ChunkedWriteHandler());
//		p.addLast(new HttpContentCompressor());
		//跨域配置
		CorsConfig corsConfig = CorsConfigBuilder.forAnyOrigin().allowNullOrigin().allowCredentials().build();
		p.addLast(new CorsHandler(corsConfig));
		p.addLast(new DefaultListenerHandler<HttpRequest>(listener));
	}
 
开发者ID:juebanlin,项目名称:util4j,代码行数:19,代码来源:HttpServerInitHandler.java

示例2: addHttpServerCodec

import io.netty.handler.codec.http.cors.CorsConfig; //导入依赖的package包/类
public static void addHttpServerCodec(@NotNull ChannelPipeline pipeline) {
  pipeline.addLast("httpRequestEncoder", new HttpResponseEncoder());
  // https://jetbrains.zendesk.com/agent/tickets/68315
  pipeline.addLast("httpRequestDecoder", new HttpRequestDecoder(16 * 1024, 16 * 1024, 8192));
  pipeline.addLast("httpObjectAggregator", new HttpObjectAggregator(MAX_CONTENT_LENGTH));
  // could be added earlier if HTTPS
  if (pipeline.get(ChunkedWriteHandler.class) == null) {
    pipeline.addLast("chunkedWriteHandler", new ChunkedWriteHandler());
  }
  pipeline.addLast("corsHandler", new CorsHandlerDoNotUseOwnLogger(CorsConfig
                                                                     .withAnyOrigin()
                                                                     .allowCredentials()
                                                                     .allowNullOrigin()
                                                                     .allowedRequestMethods(HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE, HttpMethod.HEAD, HttpMethod.PATCH)
                                                                     .allowedRequestHeaders("origin", "accept", "authorization", "content-type")
                                                                     .build()));
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:18,代码来源:NettyUtil.java

示例3: main

import io.netty.handler.codec.http.cors.CorsConfig; //导入依赖的package包/类
public static void main(String[] args) {
	String ip = "127.0.0.1";
	int port = 8080;
	ChannelInitializer<SocketChannel> channelInit = new ChannelInitializer<SocketChannel>() {
		@Override
		protected void initChannel(SocketChannel ch) throws Exception {
			ChannelPipeline p = ch.pipeline();
			
			CorsConfig corsConfig = CorsConfig.withAnyOrigin()
					.allowedRequestHeaders("content-type","accept","MyCustomHeader")
					.allowedRequestMethods(PUT,POST,GET,DELETE)
					.build();
			
			p.addLast(new HttpResponseEncoder());
			p.addLast(new HttpRequestDecoder());
			p.addLast(new HttpObjectAggregator(65536));
			p.addLast(new ChunkedWriteHandler());
			p.addLast(new CorsHandler(corsConfig));
			p.addLast(new SimpleCORSHandler());
		}
	};
	NettyServerUtil.newHttpServerBootstrap(ip, port, channelInit);
}
 
开发者ID:duchien85,项目名称:netty-cookbook,代码行数:24,代码来源:NettyHttpServerWithCORS.java

示例4: initChannel

import io.netty.handler.codec.http.cors.CorsConfig; //导入依赖的package包/类
@Override
protected void initChannel(SocketChannel ch)
        throws Exception {
    // Create a default pipeline implementation.
    CorsConfig corsConfig = CorsConfig.withAnyOrigin().build();
    ChannelPipeline pipeline = ch.pipeline();
    // Uncomment the following line if you want HTTPS
    //SSLEngine engine = SecureChatSslContextFactory.getServerContext().createSSLEngine();
    //engine.setUseClientMode(false);
    //pipeline.addLast("ssl", new SslHandler(engine));

    pipeline.addLast("encoder", new HttpResponseEncoder());
    pipeline.addLast("decoder", new HttpRequestDecoder());
    pipeline.addLast("aggregator", new HttpObjectAggregator(8388608)); // 8MB
    //pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
    pipeline.addLast("cors", new CorsHandler(corsConfig));
    pipeline.addLast("handler", new HttpStaticFileServerHandler());
}
 
开发者ID:classtag,项目名称:scratch_zookeeper_netty,代码行数:19,代码来源:HttpStaticFileServerInitializer.java

示例5: initChannel

import io.netty.handler.codec.http.cors.CorsConfig; //导入依赖的package包/类
@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline p = ch.pipeline();
    if (sslCtx != null) {
        p.addLast(sslCtx.newHandler(ch.alloc()));
    }
    if (enableGzip) {
        p.addLast(new HttpContentCompressor());
    }
    p.addLast(new HttpServerCodec(36192 * 2, 36192 * 8, 36192 * 16, false));
    p.addLast(new HttpServerExpectContinueHandler());
    p.addLast(new HttpObjectAggregator(Integer.MAX_VALUE));
    p.addLast(new ChunkedWriteHandler());
    if (enableCors) {
        CorsConfig corsConfig = CorsConfigBuilder.forAnyOrigin().allowNullOrigin().allowCredentials().build();
        p.addLast(new CorsHandler(corsConfig));
    }
    if (null != blade.webSocketPath()) {
        p.addLast(new WebSocketServerProtocolHandler(blade.webSocketPath(), null, true));
        p.addLast(new WebSockerHandler(blade));
    }
    service.scheduleWithFixedDelay(() -> date = new AsciiString(DateKit.gmtDate(LocalDateTime.now())), 1000, 1000, TimeUnit.MILLISECONDS);
    p.addLast(new HttpServerHandler());
}
 
开发者ID:lets-blade,项目名称:blade,代码行数:25,代码来源:HttpServerInitializer.java

示例6: WebAppServer

import io.netty.handler.codec.http.cors.CorsConfig; //导入依赖的package包/类
public WebAppServer(int port) {
    this.port = port;
    String hubHomeDirectory = System.getProperty("app.home");
    if (hubHomeDirectory.isEmpty()) {
        this.hubSiteDirectory = "src/main/webapp";
    } else {
        this.hubSiteDirectory = hubHomeDirectory + "/site";
    }
    corsConfig = CorsConfig.withAnyOrigin()
                    .allowCredentials()                                     // required for custom headers
                    .allowedRequestMethods(
                            HttpMethod.GET,
                            HttpMethod.POST,
                            HttpMethod.PUT,
                            HttpMethod.DELETE,
                            HttpMethod.OPTIONS)
                    .maxAge(1 * 60 * 60)                                    // 1 hour
                    .allowedRequestHeaders(
                            HttpHeaders.Names.CONTENT_TYPE,
                            RESTCodec.HEADER_REQUEST_ID,                    // header for tracking request ID
                            HttpHeaders.Names.AUTHORIZATION)                // header for OAuth2 authentication
                    .exposeHeaders(RESTCodec.HEADER_REQUEST_ID)
                    .build();
    LOGGER.debug("Website content served from '{}'", hubSiteDirectory);
}
 
开发者ID:kalixia,项目名称:kha,代码行数:26,代码来源:WebAppServer.java

示例7: ApiProtocolSwitcher

import io.netty.handler.codec.http.cors.CorsConfig; //导入依赖的package包/类
@Inject
public ApiProtocolSwitcher(ObjectMapper objectMapper) {
    super();
    this.objectMapper = objectMapper;
    corsConfig = CorsConfig.withAnyOrigin()
            .allowCredentials()                                     // required for custom headers
            .allowedRequestMethods(
                    HttpMethod.GET,
                    HttpMethod.POST,
                    HttpMethod.PUT,
                    HttpMethod.DELETE,
                    HttpMethod.OPTIONS)
            .maxAge(1 * 60 * 60)                                    // 1 hour
            .allowedRequestHeaders(
                    HttpHeaderNames.CONTENT_TYPE.toString(),
                    RESTCodec.HEADER_REQUEST_ID,                    // header for tracking request ID
                    HttpHeaderNames.AUTHORIZATION.toString())       // header for OAuth2 authentication
            .exposeHeaders(RESTCodec.HEADER_REQUEST_ID)
            .build();
}
 
开发者ID:kalixia,项目名称:Grapi,代码行数:21,代码来源:ApiProtocolSwitcher.java

示例8: start

import io.netty.handler.codec.http.cors.CorsConfig; //导入依赖的package包/类
public void start() throws InterruptedException {
    ServerBootstrap b = new ServerBootstrap();
    b.option(ChannelOption.SO_LINGER, socketLinger);
    b.option(ChannelOption.SO_REUSEADDR, reuseAddress);
    b.group(bossGroup, workerGroup)
        .channel(NioServerSocketChannel.class)
        .handler(new LoggingHandler(LogLevel.INFO))
        .childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                p.addLast(new HttpRequestDecoder());
                p.addLast(new HttpObjectAggregator(1024 * 1024 * 5));
                p.addLast(new HttpResponseEncoder());
                p.addLast(new HttpContentCompressor());
                if (corsConfiguration.hasHeader()) {
                    p.addLast(new CorsHandler(
                        CorsConfig
                            .withOrigin(corsConfiguration.getHeader())
                            .allowedRequestHeaders(HttpHeaders.Names.CONTENT_TYPE)
                            .allowedRequestMethods(HttpMethod.POST)
                        .build())
                    );
                }
                p.addLast(jsonRpcWeb3FilterHandler);
                p.addLast(jsonRpcWeb3ServerHandler);
            }
        });
    b.bind(host, port).sync();
}
 
开发者ID:rsksmart,项目名称:rskj,代码行数:31,代码来源:JsonRpcNettyServer.java

示例9: setupHttpChannel

import io.netty.handler.codec.http.cors.CorsConfig; //导入依赖的package包/类
protected ChannelHandler setupHttpChannel(Configuration config, SslContext sslCtx) {

        return new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {

                ch.pipeline().addLast("ssl", new NonSslRedirectHandler(config, sslCtx));
                ch.pipeline().addLast("encoder", new HttpResponseEncoder());
                ch.pipeline().addLast("decoder", new HttpRequestDecoder());
                ch.pipeline().addLast("compressor", new HttpContentCompressor());
                ch.pipeline().addLast("decompressor", new HttpContentDecompressor());
                ch.pipeline().addLast("aggregator", new HttpObjectAggregator(8192));
                ch.pipeline().addLast("chunker", new ChunkedWriteHandler());
                final Configuration.Cors corsCfg = config.getHttp().getCors();
                final CorsConfig.Builder ccb;
                if (corsCfg.isAllowAnyOrigin()) {
                    ccb = new CorsConfig.Builder();
                } else {
                    ccb = new CorsConfig.Builder(corsCfg.getAllowedOrigins().stream().toArray(String[]::new));
                }
                if (corsCfg.isAllowNullOrigin()) {
                    ccb.allowNullOrigin();
                }
                if (corsCfg.isAllowCredentials()) {
                    ccb.allowCredentials();
                }
                corsCfg.getAllowedMethods().stream().map(HttpMethod::valueOf).forEach(ccb::allowedRequestMethods);
                corsCfg.getAllowedHeaders().forEach(ccb::allowedRequestHeaders);
                CorsConfig cors = ccb.build();
                LOG.trace("Cors configuration: {}", cors);
                ch.pipeline().addLast("cors", new CorsHandler(cors));
                ch.pipeline().addLast("queryDecoder", new qonduit.netty.http.HttpRequestDecoder(config));
                ch.pipeline().addLast("strict", new StrictTransportHandler(config));
                ch.pipeline().addLast("login", new X509LoginRequestHandler(config));
                ch.pipeline().addLast("doLogin", new BasicAuthLoginRequestHandler(config));
                ch.pipeline().addLast("error", new HttpExceptionHandler());
            }
        };
    }
 
开发者ID:NationalSecurityAgency,项目名称:qonduit,代码行数:41,代码来源:Server.java

示例10: initChannel

import io.netty.handler.codec.http.cors.CorsConfig; //导入依赖的package包/类
@Override
public void initChannel(SocketChannel ch) {
    CorsConfig corsConfig = CorsConfigBuilder.forAnyOrigin().allowNullOrigin().allowCredentials().build();
    ChannelPipeline pipeline = ch.pipeline();
    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc()));
    }
    pipeline.addLast(new HttpResponseEncoder());
    pipeline.addLast(new HttpRequestDecoder());
    pipeline.addLast(new HttpObjectAggregator(65536));
    pipeline.addLast(new ChunkedWriteHandler());
    pipeline.addLast(new CorsHandler(corsConfig));
    pipeline.addLast(new OkResponseHandler());
}
 
开发者ID:cowthan,项目名称:JavaAyo,代码行数:15,代码来源:HttpCorsServerInitializer.java

示例11: initChannel

import io.netty.handler.codec.http.cors.CorsConfig; //导入依赖的package包/类
@Override
public void initChannel(SocketChannel ch) {
    CorsConfig corsConfig = CorsConfig.withAnyOrigin().build();
    ChannelPipeline pipeline = ch.pipeline();
    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc()));
    }
    pipeline.addLast(new HttpResponseEncoder());
    pipeline.addLast(new HttpRequestDecoder());
    pipeline.addLast(new HttpObjectAggregator(65536));
    pipeline.addLast(new ChunkedWriteHandler());
    pipeline.addLast(new CorsHandler(corsConfig));
    pipeline.addLast(new OkResponseHandler());
}
 
开发者ID:wuyinxian124,项目名称:netty4.0.27Learn,代码行数:15,代码来源:HttpCorsServerInitializer.java

示例12: LaputaServerInitializer

import io.netty.handler.codec.http.cors.CorsConfig; //导入依赖的package包/类
LaputaServerInitializer(SslContext sslContext,
                        CorsConfig corsConfig,
                        String webSocketPath,
                        LaputaRequestProcessor requestProcessor) {
  this.sslContext = sslContext;
  this.corsConfig = corsConfig;
  this.webSocketPath = webSocketPath;
  this.requestProcessor = requestProcessor;
}
 
开发者ID:orctom,项目名称:laputa,代码行数:10,代码来源:LaputaServerInitializer.java

示例13: getCorsConfig

import io.netty.handler.codec.http.cors.CorsConfig; //导入依赖的package包/类
private CorsConfig getCorsConfig(Config config) {
  if (!config.hasPath(CFG_SERVER_CORS_ALLOWS_ORIGINS)) {
    return null;
  }

  List<String> origins = config.getStringList(CFG_SERVER_CORS_ALLOWS_ORIGINS);
  if (null == origins || origins.isEmpty()) {
    return null;
  }

  CorsConfigBuilder builder = null;
  for (String origin : origins) {
    if (SIGN_STAR.equals(origin)) {
      builder = CorsConfigBuilder.forAnyOrigin();
      break;
    }
  }

  if (null == builder) {
    builder = CorsConfigBuilder.forOrigins(origins.toArray(new String[origins.size()]));
  }

  if (config.hasPath(CFG_SERVER_CORS_ALLOWS_CREDENTIALS)) {
    boolean allowCredentials = config.getBoolean(CFG_SERVER_CORS_ALLOWS_CREDENTIALS);
    if (allowCredentials) {
      builder.allowCredentials();
    }
  }

  return builder.build();
}
 
开发者ID:orctom,项目名称:laputa,代码行数:32,代码来源:Bootstrapper.java

示例14: initChannel

import io.netty.handler.codec.http.cors.CorsConfig; //导入依赖的package包/类
@Override
public void initChannel(SocketChannel ch) {
    CorsConfig corsConfig = CorsConfig.withAnyOrigin().build();
    ChannelPipeline p = ch.pipeline();
    p.addLast(new HttpResponseEncoder());
    p.addLast(new HttpRequestDecoder());
    p.addLast(new HttpObjectAggregator(65536));
    p.addLast(new ChunkedWriteHandler());
    p.addLast(new CorsHandler(corsConfig));
    p.addLast(new HttpHandler());
}
 
开发者ID:bupt1987,项目名称:JgFramework,代码行数:12,代码来源:AuthConnector.java

示例15: setupHttpChannel

import io.netty.handler.codec.http.cors.CorsConfig; //导入依赖的package包/类
protected ChannelHandler setupHttpChannel(Configuration config, SslContext sslCtx) {

        return new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {

                ch.pipeline().addLast("ssl", new NonSslRedirectHandler(config, sslCtx));
                ch.pipeline().addLast("encoder", new HttpResponseEncoder());
                ch.pipeline().addLast("decoder", new HttpRequestDecoder());
                ch.pipeline().addLast("compressor", new HttpContentCompressor());
                ch.pipeline().addLast("decompressor", new HttpContentDecompressor());
                ch.pipeline().addLast("aggregator", new HttpObjectAggregator(8192));
                ch.pipeline().addLast("chunker", new ChunkedWriteHandler());
                final Configuration.Cors corsCfg = config.getHttp().getCors();
                final CorsConfig.Builder ccb;
                if (corsCfg.isAllowAnyOrigin()) {
                    ccb = new CorsConfig.Builder();
                } else {
                    ccb = new CorsConfig.Builder(corsCfg.getAllowedOrigins().stream().toArray(String[]::new));
                }
                if (corsCfg.isAllowNullOrigin()) {
                    ccb.allowNullOrigin();
                }
                if (corsCfg.isAllowCredentials()) {
                    ccb.allowCredentials();
                }
                corsCfg.getAllowedMethods().stream().map(HttpMethod::valueOf).forEach(ccb::allowedRequestMethods);
                corsCfg.getAllowedHeaders().forEach(ccb::allowedRequestHeaders);
                CorsConfig cors = ccb.build();
                LOG.trace("Cors configuration: {}", cors);
                ch.pipeline().addLast("cors", new CorsHandler(cors));
                ch.pipeline().addLast("queryDecoder", new timely.netty.http.HttpRequestDecoder(config));
                ch.pipeline().addLast("fileServer", new HttpStaticFileServerHandler());
                ch.pipeline().addLast("strict", new StrictTransportHandler(config));
                ch.pipeline().addLast("login", new X509LoginRequestHandler(config));
                ch.pipeline().addLast("doLogin", new BasicAuthLoginRequestHandler(config));
                ch.pipeline().addLast("aggregators", new HttpAggregatorsRequestHandler());
                ch.pipeline().addLast("metrics", new HttpMetricsRequestHandler(config));
                ch.pipeline().addLast("query", new HttpQueryRequestHandler(dataStore));
                ch.pipeline().addLast("search", new HttpSearchLookupRequestHandler(dataStore));
                ch.pipeline().addLast("suggest", new HttpSuggestRequestHandler(dataStore));
                ch.pipeline().addLast("version", new HttpVersionRequestHandler());
                ch.pipeline().addLast("put", new HttpMetricPutHandler(dataStore));
                ch.pipeline().addLast("error", new TimelyExceptionHandler());
            }
        };
    }
 
开发者ID:NationalSecurityAgency,项目名称:timely,代码行数:49,代码来源:Server.java


注:本文中的io.netty.handler.codec.http.cors.CorsConfig类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。