springboot添加https
Posted zincredible
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot添加https相关的知识,希望对你有一定的参考价值。
一、使用JDK工具keytool生成证书
keytool命令详解 https://blog.csdn.net/zlfing/article/details/77648430
keytool -genkey -alias tomcat -keypass password -storetype PKCS12 -keyalg RSA -keysize 2048 -validity 3650 -keystore keystore.p12 -storepass password -dname "CN=(AMS), OU=(National Innovation Institute of Additive Manufacturing), O=(National Innovation Institute of Additive Manufacturing), L=(Xian), ST=(Shanxi), C=(CN)"
二、将生成的文件copy到src下
三、配置yml文件
server:
#https 端口 port: 8443 ssl: key-store: classpath:keystore.p12 key-store-password: password keyStoreType: PKCS12 keyAlias: tomcat
#http端口 http: port: 7000
四、重定向http到https
在配置类中加入
/** * http auto to https * * @return */ @Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() { @Override protected void postProcessContext(Context context) { SecurityConstraint securityConstraint = new SecurityConstraint(); securityConstraint.setUserConstraint("CONFIDENTIAL"); SecurityCollection collection = new SecurityCollection(); collection.addPattern("/*"); securityConstraint.addCollection(collection); context.addConstraint(securityConstraint); } }; tomcat.addAdditionalTomcatConnectors(httpConnector()); return tomcat; } public Connector httpConnector() { Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setScheme("http"); connector.setPort(7000);//http端口重定向到https端口 connector.setSecure(false); connector.setRedirectPort(8443); return connector; }
以上是关于springboot添加https的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot启动报错“Consider defining a bean of type ‘xxx.mapper.UserMapper‘ in your configuration.“(代码片段
全栈编程系列SpringBoot整合Shiro(含KickoutSessionControlFilter并发在线人数控制以及不生效问题配置启动异常No SecurityManager...)(代码片段
springboot启动流程构造SpringApplication实例对象
SpringBoot中表单提交报错“Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported“(代码片段