经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring Boot » 查看文章
spring boot 配置HTTPS代码实例
来源:jb51  时间:2019/11/4 12:47:21  对本文有异议

这篇文章主要介绍了spring boot 配置HTTPS代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

spring boot 版本是<version>1.5.8.RELEASE</version>

1.配置文件里,看下不要有空格=[不要有空格]

2.别名

================

  1. server.port=8095
  2. server.ssl.key-store=*.pfx
  3. server.ssl.key-store-password=**
  4. server.ssl.key-store-type=PKCS12
  5. server.ssl.key-alias=alias//别名

代码

  1. import org.apache.catalina.Context;
  2. import org.apache.catalina.connector.Connector;
  3. import org.apache.tomcat.util.descriptor.web.SecurityCollection;
  4. import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
  5. import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
  6. import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. /**
  10. * 扩展: 并将 http 自动转向 https
  11. * @Description:类说明:
  12. * @author: gzh
  13. * @date: 2019年11月1日上午11:08:20
  14. */
  15. @Configuration
  16. public class HttpsConfiguration {
  17. @Bean
  18. public EmbeddedServletContainerFactory servletContainer() {
  19. TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(){
  20. protected void postProcessContext(Context context) {
  21. SecurityConstraint securityConstraint = new SecurityConstraint();
  22. securityConstraint.setUserConstraint("CONFIDENTIAL");
  23. SecurityCollection collection = new SecurityCollection();
  24. collection.addPattern("/*");
  25. securityConstraint.addCollection(collection);
  26. context.addConstraint(securityConstraint);
  27. }
  28. }
  29. ;
  30. tomcat.addAdditionalTomcatConnectors(httpConnector());
  31. return tomcat;
  32. }
  33. @Bean
  34. public Connector httpConnector(){
  35. Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
  36. connector.setScheme("http");
  37. connector.setPort(8096);
  38. //表示用8080端口来供http访问(PB,kingdee)
  39. connector.setSecure(false);
  40. //输入:my.com,跳到: http:// www.my.com
  41. connector.setRedirectPort(8095);
  42. //自动重定向到8095,443端口
  43. return connector;
  44. }
  45. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号