org.springframework.boot.web.server.WebServerFactoryCustomizer Java Examples

The following examples show how to use org.springframework.boot.web.server.WebServerFactoryCustomizer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MvcAutoConfiguration.java    From MaxKey with Apache License 2.0 6 votes vote down vote up
/**
 * 配置默认错误页面(仅用于内嵌tomcat启动时) 使用这种方式,在打包为war后不起作用.
 *
 * @return webServerFactoryCustomizer
 */
@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
    return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
        @Override
        public void customize(ConfigurableWebServerFactory factory) {
            _logger.debug("WebServerFactoryCustomizer ... ");
            ErrorPage errorPage400 = 
                    new ErrorPage(HttpStatus.BAD_REQUEST, "/exception/error/400");
            ErrorPage errorPage404 = 
                    new ErrorPage(HttpStatus.NOT_FOUND, "/exception/error/404");
            ErrorPage errorPage500 = 
                    new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/exception/error/500");
            factory.addErrorPages(errorPage400, errorPage404, errorPage500);
        }
    };
}
 
Example #2
Source File: JettyAutoConfiguration.java    From joinfaces with Apache License 2.0 6 votes vote down vote up
@Bean
public WebServerFactoryCustomizer<JettyServletWebServerFactory> jsfJettyFactoryCustomizer() {
	return factory -> factory.addServerCustomizers(new JettyServerCustomizer() {
		@Override
		@SneakyThrows(IOException.class)
		public void customize(Server server) {
			Handler[] childHandlersByClass = server.getChildHandlersByClass(WebAppContext.class);
			final WebAppContext webAppContext = (WebAppContext) childHandlersByClass[0];

			String classPathResourceString = JettyAutoConfiguration.this.jettyProperties.getClassPathResource();

			webAppContext.setBaseResource(new ResourceCollection(
				Resource.newResource(new ClassPathResource(classPathResourceString).getURI()),
				webAppContext.getBaseResource()));

			log.info("Setting Jetty classLoader to {} directory", classPathResourceString);
		}
	});
}
 
Example #3
Source File: WebContainerAccessLogAutoConfiguration.java    From spring-cloud-formula with Apache License 2.0 6 votes vote down vote up
@Bean
public WebServerFactoryCustomizer accessWebServerFactoryCustomizer() {
    return factory -> {
        if (factory instanceof JettyServletWebServerFactory) {
            ((JettyServletWebServerFactory) factory).addServerCustomizers((JettyServerCustomizer) server -> {
                HandlerCollection handlers = new HandlerCollection();
                for (Handler handler : server.getHandlers()) {
                    handlers.addHandler(handler);
                }
                RequestLogHandler reqLogs = new RequestLogHandler();
                Slf4jRequestLog requestLog = new Slf4jRequestLog();
                requestLog.setLoggerName("access-log");
                requestLog.setLogLatency(false);

                reqLogs.setRequestLog(requestLog);
                handlers.addHandler(reqLogs);
                server.setHandler(handlers);
            });
        }
    };
}
 
Example #4
Source File: DSSSpringApplication.java    From DataSphereStudio with Apache License 2.0 6 votes vote down vote up
@Bean
public WebServerFactoryCustomizer<JettyServletWebServerFactory> jettyFactoryCustomizer() {
    return new WebServerFactoryCustomizer<JettyServletWebServerFactory>() {
        public void customize(JettyServletWebServerFactory jettyServletWebServerFactory) {
            jettyServletWebServerFactory.addServerCustomizers(new JettyServerCustomizer() {
                public void customize(Server server) {
                    Handler[] childHandlersByClass = server.getChildHandlersByClass(WebAppContext.class);
                    final WebAppContext webApp = (WebAppContext) childHandlersByClass[0];
                    FilterHolder filterHolder = new FilterHolder(CharacterEncodingFilter.class);
                    filterHolder.setInitParameter("encoding", Configuration.BDP_ENCODING().getValue());
                    filterHolder.setInitParameter("forceEncoding", "true");
                    webApp.addFilter(filterHolder, "/*", EnumSet.allOf(DispatcherType.class));
                    BDPJettyServerHelper.setupRestApiContextHandler(webApp);
                    if(ServerConfiguration.BDP_SERVER_SOCKET_MODE().getValue()) {
                        BDPJettyServerHelper.setupControllerServer(webApp);
                    }
                    if(!ServerConfiguration.BDP_SERVER_DISTINCT_MODE().getValue()) {
                        BDPJettyServerHelper.setupWebAppContext(webApp);
                    }
                }
            });
        }
    };
}
 
Example #5
Source File: WebServerSecurityConfig.java    From api-layer with Eclipse Public License 2.0 6 votes vote down vote up
@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> servletContainerCustomizer() {
    return factory -> factory.addConnectorCustomizers(connector -> {
        AbstractHttp11Protocol abstractProtocol = (AbstractHttp11Protocol<?>) connector.getProtocolHandler();
        abstractProtocol.setUseServerCipherSuitesOrder(true);
    });
}
 
Example #6
Source File: MossApplication.java    From Moss with Apache License 2.0 5 votes vote down vote up
@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
	return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
		@Override
		public void customize(ConfigurableWebServerFactory factory) {
			factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/index.html"));
			//factory.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/index.html"));
		}
	};
}
 
Example #7
Source File: UndertowAutoConfiguration.java    From joinfaces with Apache License 2.0 5 votes vote down vote up
@Bean
public WebServerFactoryCustomizer<UndertowServletWebServerFactory> jsfUndertowFactoryCustomizer(UndertowProperties undertowProperties) {
	return factory -> factory.addDeploymentInfoCustomizers(deploymentInfo -> {
		AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
			deploymentInfo.setResourceManager(new CompositeResourceManager(
				new ClassPathResourceManager(deploymentInfo.getClassLoader(), undertowProperties.getClassPathResource()),
				deploymentInfo.getResourceManager()));

			return null;
		});

		log.info("Setting Undertow classLoader to {} directory", undertowProperties.getClassPathResource());
	});
}
 
Example #8
Source File: TomcatGracefulShutdownAutoConfiguration.java    From loc-framework with MIT License 5 votes vote down vote up
@Bean
public WebServerFactoryCustomizer tomcatFactoryCustomizer() {
  return server -> {
    if (server instanceof TomcatServletWebServerFactory) {
      ((TomcatServletWebServerFactory) server).addConnectorCustomizers(tomcatGracefulShutdown());
    }
  };
}
 
Example #9
Source File: ErrorPagesConfig.java    From OneBlog with GNU General Public License v3.0 5 votes vote down vote up
/**
   * 自定义异常处理路径
   *
   * @return
   */
  @Bean
  public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> containerCustomizer() {
      return factory -> {
	factory.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/error/400"));
	factory.addErrorPages(new ErrorPage(HttpStatus.UNAUTHORIZED, "/error/401"));
	factory.addErrorPages(new ErrorPage(HttpStatus.FORBIDDEN, "/error/403"));
	factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/error/404"));
	factory.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500"));
	factory.addErrorPages(new ErrorPage(Throwable.class, "/error/500"));
};
  }
 
Example #10
Source File: TarocoAuthenticationApplication.java    From Taroco with Apache License 2.0 5 votes vote down vote up
/**
 * 解决vue-router history 模式下刷新页面的404问题
 */
@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
    return factory -> {
        ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/index.html");
        factory.addErrorPages(error404Page);
    };
}
 
Example #11
Source File: ExternalizedConfigurationWebApplicationBootstrap.java    From thinking-in-spring-boot-samples with Apache License 2.0 5 votes vote down vote up
@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> webServerFactoryCustomizer() {
    return factory ->
            factory.addContextCustomizers(
                    context -> {
                        // 设置 JNDI 信息
                        ContextEnvironment environment = new ContextEnvironment();
                        environment.setName("jndi-name");
                        environment.setValue("My JNDI");
                        environment.setType(String.class.getName());
                        // 配置 Environment,等同于 <Environment/> 元素
                        context.getNamingResources().addEnvironment(environment);
                    }
            );
}
 
Example #12
Source File: ErrorPagesConfig.java    From springboot-shiro with MIT License 5 votes vote down vote up
/**
 * 自定义异常处理路径
 *
 * @return
 */
@Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> containerCustomizer() {
    return factory -> {
        factory.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/error/400"));
        factory.addErrorPages(new ErrorPage(HttpStatus.UNAUTHORIZED, "/error/401"));
        factory.addErrorPages(new ErrorPage(HttpStatus.FORBIDDEN, "/error/403"));
        factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/error/404"));
        factory.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500"));
        factory.addErrorPages(new ErrorPage(Throwable.class, "/error/500"));
    };
}
 
Example #13
Source File: TomcatGracefulShutdownAutoConfiguration.java    From fast-family-master with Apache License 2.0 5 votes vote down vote up
@Bean
public WebServerFactoryCustomizer tomcatServletWebServerFactory() {
    return server -> {
        if (server instanceof TomcatServletWebServerFactory) {
            ((TomcatServletWebServerFactory) server).addConnectorCustomizers(tomcatGracefulShutdown());
        }
    };
}
 
Example #14
Source File: WebContainerAccessLogAutoConfiguration.java    From spring-cloud-formula with Apache License 2.0 5 votes vote down vote up
@Bean
public WebServerFactoryCustomizer accesslogWebServerFactoryCustomizer() {
    return factory -> {
        SimpleLogbackValve valve = new SimpleLogbackValve(LoggerFactory.getLogger("accesslog"));

        ((TomcatServletWebServerFactory) factory).addEngineValves(valve);
    };
}
 
Example #15
Source File: WebContainerAccessLogAutoConfiguration.java    From spring-cloud-formula with Apache License 2.0 5 votes vote down vote up
@Bean
public WebServerFactoryCustomizer accesslogWebServerFactoryCustomizer() {
    return factory -> {
        if (factory instanceof UndertowServletWebServerFactory) {

            HandlerWrapper wrapper = this::createAccessLogHandler;
            ((UndertowServletWebServerFactory) factory).addBuilderCustomizers(builder ->
                    builder.setServerOption(UndertowOptions.RECORD_REQUEST_START_TIME, true));
            ((UndertowServletWebServerFactory) factory).addDeploymentInfoCustomizers(deploymentInfo ->
                    deploymentInfo.addInitialHandlerChainWrapper(wrapper));
        }
    };
}
 
Example #16
Source File: ErrorPagesConfig.java    From springboot-learn with MIT License 5 votes vote down vote up
/**
 * 自定义异常处理路径
 *
 * @return
 */
@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
    return (container -> {
        ErrorPage error400Page = new ErrorPage(HttpStatus.BAD_REQUEST, "/error/400");
        ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/error/401");
        ErrorPage error403Page = new ErrorPage(HttpStatus.FORBIDDEN, "/error/403");
        ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/error/404");
        ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500");
        ErrorPage errorExPage = new ErrorPage(Throwable.class, "/error/500");
        container.addErrorPages(error400Page, error401Page, error403Page, error404Page, error500Page, errorExPage);
    });
}
 
Example #17
Source File: JetLinksConfiguration.java    From jetlinks-community with Apache License 2.0 5 votes vote down vote up
@Bean
public WebServerFactoryCustomizer<NettyReactiveWebServerFactory> webServerFactoryWebServerFactoryCustomizer() {
    //解决请求参数最大长度问题
    return factory -> factory.addServerCustomizers(httpServer ->
        httpServer.httpRequestDecoder(spec -> {
            spec.maxInitialLineLength(10240);
            return spec;
        }));
}
 
Example #18
Source File: TomcatAutoConfiguration.java    From joinfaces with Apache License 2.0 4 votes vote down vote up
@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> jsfTomcatFactoryCustomizer() {
	return factory -> factory.addContextCustomizers(this.customizer);
}
 
Example #19
Source File: CredHubApp.java    From credhub with Apache License 2.0 4 votes vote down vote up
@Bean
public WebServerFactoryCustomizer servletContainerCustomizer() {
  return (factory) -> ((TomcatServletWebServerFactory) factory)
    .addConnectorCustomizers((connector) -> ((AbstractHttp11Protocol<?>) connector.getProtocolHandler())
      .setUseServerCipherSuitesOrder(true));
}
 
Example #20
Source File: TestApplication.java    From recaptcha-spring-boot-starter with Apache License 2.0 4 votes vote down vote up
@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> cookieProcessorCustomizer() {
    return factory -> factory.addContextCustomizers(context -> context.setCookieProcessor(new LegacyCookieProcessor()));
}