You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # spring-boot-demo-https
  2. > 此 demo 主要演示了 Spring Boot 如何集成 https
  3. ## 1. 生成证书
  4. 首先使用 jdk 自带的 keytool 命令生成证书复制到项目的 `resources` 目录下(生成的证书一般在用户目录下 C:\Users\Administrator\server.keystore)
  5. > 自己生成的证书浏览器会有危险提示,去ssl网站上使用金钱申请则不会
  6. ![ssl 命令截图](ssl.png)
  7. ## 2. 添加配置
  8. 1. 在配置文件配置生成的证书
  9. ```yaml
  10. server:
  11. ssl:
  12. # 证书路径
  13. key-store: classpath:server.keystore
  14. key-alias: tomcat
  15. enabled: true
  16. key-store-type: JKS
  17. #与申请时输入一致
  18. key-store-password: 123456
  19. # 浏览器默认端口 和 80 类似
  20. port: 443
  21. ```
  22. 2. 配置 Tomcat
  23. ```java
  24. /**
  25. * <p>
  26. * HTTPS 配置类
  27. * </p>
  28. *
  29. * @author yangkai.shen
  30. * @date Created in 2020-01-19 10:31
  31. */
  32. @Configuration
  33. public class HttpsConfig {
  34. /**
  35. * 配置 http(80) -> 强制跳转到 https(443)
  36. */
  37. @Bean
  38. public Connector connector() {
  39. Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
  40. connector.setScheme("http");
  41. connector.setPort(80);
  42. connector.setSecure(false);
  43. connector.setRedirectPort(443);
  44. return connector;
  45. }
  46. @Bean
  47. public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector) {
  48. TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
  49. @Override
  50. protected void postProcessContext(Context context) {
  51. SecurityConstraint securityConstraint = new SecurityConstraint();
  52. securityConstraint.setUserConstraint("CONFIDENTIAL");
  53. SecurityCollection collection = new SecurityCollection();
  54. collection.addPattern("/*");
  55. securityConstraint.addCollection(collection);
  56. context.addConstraint(securityConstraint);
  57. }
  58. };
  59. tomcat.addAdditionalTomcatConnectors(connector);
  60. return tomcat;
  61. }
  62. }
  63. ```
  64. ## 3. 测试
  65. 启动项目,浏览器访问 http://localhost 将自动跳转到 https://localhost
  66. ## 4. 参考
  67. - `keytool`命令参考
  68. ```bash
  69. $ keytool --help
  70. 密钥和证书管理工具
  71. 命令:
  72. -certreq 生成证书请求
  73. -changealias 更改条目的别名
  74. -delete 删除条目
  75. -exportcert 导出证书
  76. -genkeypair 生成密钥对
  77. -genseckey 生成密钥
  78. -gencert 根据证书请求生成证书
  79. -importcert 导入证书或证书链
  80. -importpass 导入口令
  81. -importkeystore 从其他密钥库导入一个或所有条目
  82. -keypasswd 更改条目的密钥口令
  83. -list 列出密钥库中的条目
  84. -printcert 打印证书内容
  85. -printcertreq 打印证书请求的内容
  86. -printcrl 打印 CRL 文件的内容
  87. -storepasswd 更改密钥库的存储口令
  88. 使用 "keytool -command_name -help" 获取 command_name 的用法
  89. ```
  90. - [Java Keytool工具简介](https://blog.csdn.net/liumiaocn/article/details/61921014)