1 进入windows命令行输入以下内容:
keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650
①.-storetype 指定密钥仓库类型
②.-keyalg 生证书的算法名称,RSA是一种非对称加密算法
③.-keysize 证书大小
④.-keystore 生成的证书文件的存储路径
⑤.-validity 证书的有效期
2 填写信息
你的姓名、公司名称,地址等,最后输入: 是 + enter
3 配置application.properties文件:
server.ssl.key-store=keystore.p12
server.ssl.key-store-password=123456 #你自己的密码
server.ssl.keyStoreType=PKCS12
server.ssl.keyAlias:tomcat
第一行指定签名文件,第二行指定签名密码,第三行指定密钥仓库类型,第四个是别名。
4
@Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() { @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(8081); return connector; }
这个时候当我们访问http://localhost:8080的时候系统会自动重定向到https://localhost:8081这个地址上。