本文共 1471 字,大约阅读时间需要 4 分钟。
springboot内部已经集成了,引入密钥文件,修改相关配置文件就可以使用https了,
server: port: 8443 ssl: key-store: classpath:www.aaa.cn.jks key-store-password: aaaaaa key-store-type: JKS
监听一个http端口,请求这个跳转后请求会转发到对应的https端口
@Configurationpublic class CommonConfig { @Value("${http.port}") private Integer httpPort; @Value("${server.port}") private Integer serverPort; @Bean public TomcatServletWebServerFactory servletContainer() { TomcatServletWebServerFactory tomcat = 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); } }; tomcat.addAdditionalTomcatConnectors(httpConnector()); return tomcat; } @Bean public Connector httpConnector() { Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setScheme("http"); //Connector监听的http的端口号 connector.setPort(8080); connector.setSecure(false); //监听到http的端口号后转向到的https的端口号 connector.setRedirectPort(8443); return connector; }}
就这么简单就完成了。
转载地址:http://dhjqi.baihongyu.com/