• 加载SSL证书


    使用JDK1.8 开发工具包bin目录下的keytool.exe工具生成ssl密钥:

    keytool -genkey -alias mykey -keyalg RSA -keysize 2048 -validity 365 -keystore mykeystore.p 

    • -genkey: 表示创建密钥。
    • -alias: 保存时的别名。
    • -keyalg:加密算法选择,这里使用RSA。
    • -keystore:密钥的存放位置。
    • -validity:有效时间,单位是天。

    配置项 

    application.properties

    1. server.port=8081
    2. server.error.path=/log
    3. server.servlet.session.timeout=30s
    4. #设置应用程序的上下文路径为 /testc002。这意味着所有以 /testc002 开始的 URL 都将被认为属于这个应用程序。
    5. server.servlet.context-path=/testc002
    6. server.tomcat.uri-encoding=UTF-8
    7. server.tomcat.max-threads=500
    8. #表示 SSL 密钥存储库的名称为 safehttp.p。
    9. server.sll.key-store=safehttp.p
    10. #表示 SSL 密钥别名为 tomcathttpstest2。
    11. server.sll.key-alias=tomcathttpstest2
    12. #这行设置的是 SSL 密钥存储库的密码为 12345678
    13. server.sll.key-store-password=12345678

    pom.xml 

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0modelVersion>
    6. <groupId>org.examplegroupId>
    7. <artifactId>spring_BackartifactId>
    8. <version>1.0-SNAPSHOTversion>
    9. <parent>
    10. <groupId>org.springframework.bootgroupId>
    11. <artifactId>spring-boot-starter-parentartifactId>
    12. <version>2.3.9.RELEASEversion>
    13. <relativePath/>
    14. parent>
    15. <properties>
    16. <maven.compiler.source>8maven.compiler.source>
    17. <maven.compiler.target>8maven.compiler.target>
    18. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    19. properties>
    20. <dependencies>
    21. <dependency>
    22. <groupId>org.springframework.bootgroupId>
    23. <artifactId>spring-boot-starter-webartifactId>
    24. dependency>
    25. dependencies>
    26. project>

    配置类

    1. package org.example.config;
    2. import org.apache.catalina.Context;
    3. import org.apache.catalina.connector.Connector;
    4. import org.apache.tomcat.util.descriptor.web.SecurityCollection;
    5. import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
    6. import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
    7. import org.springframework.context.annotation.Bean;
    8. import org.springframework.context.annotation.Configuration;
    9. import org.springframework.web.bind.annotation.RequestMapping;
    10. import sun.security.util.SecurityConstants;
    11. import java.sql.Connection;
    12. @Configuration
    13. public class TomcatConfig {
    14. /**
    15. * 设置 Tomcat 的Server配置
    16. * @return
    17. */
    18. @Bean
    19. TomcatServletWebServerFactory tomcatServletWebServerFactory(){
    20. TomcatServletWebServerFactory myFactory = new TomcatServletWebServerFactory(){
    21. //创建一个安全约束对象
    22. @Override
    23. protected void postProcessContext(Context context) {
    24. SecurityConstraint constraint = new SecurityConstraint();
    25. constraint.setUserConstraint("CONFIDENTIAL");//设置为机密级别
    26. SecurityCollection connection = new SecurityCollection();//设置一个安全连接对象
    27. //作用到所有路由上
    28. connection.addPattern("/*");
    29. //加入 connection 对象到安全路由中去
    30. constraint.addCollection(connection);
    31. context.addConstraint(constraint);
    32. }
    33. };
    34. myFactory.addAdditionalTomcatConnectors(createConnector());
    35. return myFactory;
    36. }
    37. /**
    38. * 创建一个连接兼容Https请求
    39. * @return
    40. */
    41. private Connector createConnector(){
    42. //tomcat 9 中
    43. //tomcat/conf/server.xml中不要使用org.apache.coyote.http11.Http11AprProtocol
    44. //要用HTTP/1.1
    45. Connector connector = new Connector("HTTP/1.1");
    46. connector.setScheme("http");
    47. connector.setPort(8080);
    48. connector.setSecure(true);//关闭ssl检查
    49. //设置跳转到8081 的端口
    50. connector.setRedirectPort(8081);
    51. return connector;
    52. }
    53. }

    控制类

    1. package org.example.controller;
    2. import org.springframework.stereotype.Component;
    3. import org.springframework.web.bind.annotation.GetMapping;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import org.springframework.web.bind.annotation.RestController;
    6. @RestController
    7. public class FirstController {
    8. @GetMapping("/hey")
    9. public String hey(){
    10. return "hey main";
    11. }
    12. }

    启动类

    1. package org.example;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    4. import org.springframework.context.annotation.ComponentScan;
    5. @EnableAutoConfiguration
    6. @ComponentScan
    7. public class MyApp {
    8. public static void main(String[] args) {
    9. SpringApplication.run(MyApp.class,args);
    10. }
    11. }

    此时访问将只能使用 http 协议 以及通过8080端口跳转到 8081.

  • 相关阅读:
    Unity与IOS⭐一、百度语音IOS版Demo调试方法
    外包干了3天,技术退步明显.......
    Linux 的情况下实现贪吃蛇 -- 第二十八天
    CS224W1.2——图机器学习应用
    记一次生产问题的排查,让我领略了算法的重要性
    2. 如何给在 SAP Business Application Studio 里开发的 OData 服务准备测试数据
    BERT tokenizer 增加全角标点符号
    数组的最小不可组成和问题
    一文彻底讲透@Async注解的原理和使用方法
    【Android知识笔记】性能优化专题(三)
  • 原文地址:https://blog.csdn.net/m0_62943934/article/details/134505503