1,添加 TomcatConfig.java 配置文件
@Configuration public class TomcatConfig { @Bean TomcatServletWebServerFactory tomcatServletWebServerFactory() { TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory() { @Override protected void postProcessContext(Context context) { SecurityConstraint constraint = new SecurityConstraint(); constraint.setUserConstraint("CONFIDENTIAL"); SecurityCollection collection = new SecurityCollection(); collection.addPattern("/*"); constraint.addCollection(collection); context.addConstraint(constraint); } }; factory.addAdditionalTomcatConnectors(createTomcatConnetor()); return factory; } private Connector createTomcatConnetor() { Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setScheme("http"); connector.setPort(8080); connector.setSecure(false); connector.setRedirectPort(8081); return connector; } }
意味着:
http://localhost:8080 请求会自动跳转到 https://localhost:8081 上
同理 undertow 容器配置如下:
1,在 application.properties 中配置证书及端口:
##################################---Undertow服务器支持HTTPS服务---############################################## server.http2.enabled=true server.servlet.context-path=/blog custom.server.http.port=8080 server.port=8443 server.ssl.key-store=classpath:javalsj.jks server.ssl.key-store-password=214533136960974 server.undertow.worker-threads=20 server.undertow.buffer-size=512 server.undertow.io-threads=2 ```2,WebServerConfiguration.java配置文件:
import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory; import org.springframework.boot.web.servlet.server.ServletWebServerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import io.undertow.Undertow; import io.undertow.servlet.api.SecurityConstraint; import io.undertow.servlet.api.SecurityInfo; import io.undertow.servlet.api.TransportGuaranteeType; import io.undertow.servlet.api.WebResourceCollection; /** * @description 采用Undertow作为服务器,支持Https服务配置 * @author WANGJIHONG * @date 2018年3月7日 下午8:34:18 * @Copyright 版权所有 (c) www.javalsj.com * @memo 备注信息 */ @Configuration public class WebServerConfiguration { /** * http服务端口 */ @Value("${custom.server.http.port}") private Integer httpPort; /** * https服务端口 */ @Value("${server.port}") private Integer httpsPort; /** * 采用Undertow作为服务器。 * Undertow是一个用java编写的、灵活的、高性能的Web服务器,提供基于NIO的阻塞和非阻塞API,特点: * 非常轻量级,Undertow核心瓶子在1Mb以下。它在运行时也是轻量级的,有一个简单的嵌入式服务器使用少于4Mb的堆空间。 * 支持HTTP升级,允许多个协议通过HTTP端口进行多路复用。 * 提供对Web套接字的全面支持,包括JSR-356支持。 * 提供对Servlet 3.1的支持,包括对嵌入式servlet的支持。还可以在同一部署中混合Servlet和本机Undertow非阻塞处理程序。 * 可以嵌入在应用程序中或独立运行,只需几行代码。 * 通过将处理程序链接在一起来配置Undertow服务器。它可以对各种功能进行配置,方便灵活。 */ @Bean public ServletWebServerFactory undertowFactory() { UndertowServletWebServerFactory undertowFactory = new UndertowServletWebServerFactory(); undertowFactory.addBuilderCustomizers((Undertow.Builder builder) -> { builder.addHttpListener(httpPort, "0.0.0.0"); // 开启HTTP2 builder.setServerOption(UndertowOptions.ENABLE_HTTP2, true); }); undertowFactory.addDeploymentInfoCustomizers(deploymentInfo -> { // 开启HTTP自动跳转至HTTPS deploymentInfo.addSecurityConstraint(new SecurityConstraint() .addWebResourceCollection(new WebResourceCollection().addUrlPattern("/*")) .setTransportGuaranteeType(TransportGuaranteeType.CONFIDENTIAL) .setEmptyRoleSemantic(SecurityInfo.EmptyRoleSemantic.PERMIT)) .setConfidentialPortManager(exchange -> httpsPort); }); return undertowFactory; } }
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于