1. keytool 生成证书

windows下的生成:

  keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650

linux下的命令:

keytool -genkey -alias tomcat -keyalg RSA -validity 20000 -keystore keystore.p12 

 

 

 

windows下生成.png

执行完后会生成一个文件:keystore.p12

关于keytool的说明:
keytool -genkey -alias 你的证书别名 -keyalg 密钥算法 -keystore 证书库文件保存的位置和文件名 -keysize 密钥长度 -validity 证书有效期天数

springboot编码

配置application.yml

server:
  port: 8081 ssl: key-store: keystore.p12 key-store-password: test08 key-store-type: PKCS12 key-alias: tomcat 

将上面生成的keystore.p12文件移动到classpath下。

此时你的项目就可以使用https协议访问。

与http同时访问

如果你希望你的项目既可以http访问,也可以https访问,而且两个互不干扰:
在启动类下,public class KindoApplication extends SpringBootServletInitializer(继承SpringBootServletInitializer类):

  • springboot版本1.x:

@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(initiateHttpConnector()); return tomcat; } private Connector initiateHttpConnector() { Connector connector = new Connector( "org.apache.coyote.http11.Http11NioProtocol"); connector.setScheme("http"); connector.setPort(8082); connector.setSecure(false); connector.setRedirectPort(8443); return connector; } 

  • springboot版本2.x:

@Bean
    public ServletWebServerFactory servletContainer() { TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory(); tomcat.addAdditionalTomcatConnectors(createHTTPConnector()); return tomcat; } private Connector createHTTPConnector() { Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setScheme("http"); connector.setSecure(false); connector.setPort(8082); connector.setRedirectPort(8443); return connector; } 

ps:注意:http的端口不要https相同,即上文的port设置,否则会出现端口占用错误。

http强制转换https访问

在用户用http访问的时候,强制转换为https。
基于上述模块与http同时访问,添加过滤器,强制重定向。


import org.springframework.context.annotation.Configuration; import org.springframework.web.filter.OncePerRequestFilter; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * Created by lingbao on 2018/1/23. * * @author lingbao * @Description * @Modify */ @Configuration @WebFilter public class KindoFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { String requestURL = request.getRequestURL().toString(); String protocol = requestURL.split("://")[0]; if ("http".equals(protocol)) { requestURL = requestURL.replace("http", "https").replace("8082", "8081"); response.sendRedirect(requestURL); } filterChain.doFilter(request, response); } } 

方法有点蠢,如果有好的方法,欢迎留言
有BUG也可以交流交流!谢谢!

参考文献:
https://www.jianshu.com/p/68d723431596
https://www.jianshu.com/p/05c8be17c80a

作者:靈08_1024

链接:https://www.jianshu.com/p/b6549f086b21

来源:简书

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

原文地址:http://www.cnblogs.com/telwanggs/p/16899856.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性