zoukankan      html  css  js  c++  java
  • Springboot模拟https安全访问(使用Java提供的keytool命令生成证书)

    1、SpringBoot启动时默认采用http进行通信协议定义,但是为了访问安全性,我们有时候会选择使用https进行访问。正常来讲,https的访问是需要证书的,并且为了保证这个证书的安全,一定要在项目中使用CA进行认证,需要收费的哦,证书真是一个挣钱的生意。这里只是利用Java提供的keytool命令实现证书的生成。

    2、如果想要使用keytool命令生成一个证书,这里先简单学习一下如何使用此命令,如下所示:

    参考:https://www.cnblogs.com/zhi-leaf/p/10418222.html

    1 keytool -genkey -alias tomcat -keyalg RSA -keystore D:/tomcat.keystore -keypass 123456 -storepass 123456 -dname "CN=xingming,OU=danwei,O=zuzhi,L=shi,ST=sheng,C=CN"
    2 keytool -genkey -alias tomcat -keyalg RSA -keysize 1024 -validity 365 -keystore D:/tomcat.keystore -keypass 123456 -storepass 123456 -dname "CN=xingming,OU=danwei,O=zuzhi,L=shi,ST=sheng,C=CN"

    1)、-genkey 生成秘钥。
    2)、-alias 别名。
    3)、-keyalg 秘钥算法。
    4)、-keysize 秘钥长度。
    5)、-validity 有效期。
    6)、-keystore 生成秘钥库的存储路径和名称。
    7)、-keypass 秘钥口令。
    8)、-storepass 秘钥库口令。
    9)、-dname 拥有者信息,CN:姓名;OU:组织单位名称;O:组织名称;L:省/市/自治区名称;C:国家/地区代码。

    如果想要,更多参数说明,执行命令:keytool -genkey -?。

    3、查看证书的命令,如下所示:

    1 keytool -list -keystore d:/tomcat.keystore -storepass 123456
    2 keytool -list -v -keystore d:/tomcat.keystore -storepass 123456
    3 keytool -list -rfc -keystore d:/tomcat.keystore -storepass 123456

    1)、-list 列出秘钥库中的条目。
    2)、-v 详细输出。
    3)、-rfc 以RFC样式输出。

    查看更多参数说明执行命令:keytool -list -?。

    使用该命令生成证书,如下所示:

    1 keytool -genkey -alias mytomcat -storetype PKCS12  -keyalg RSA -keysize 2048 -keystore D:/keystore.p12 -validity 3650 -keypass 123456 -dname "CN=Web Server,OU=Unit,O=Organization,L=City,S=State,C=US" -storepass 123456 

    执行完成后,会生成一个名称为keystore.p12的证书文件,该证书的别名为mytomcat,访问密码为123456。 将生成的keystore.p12复制到src/main/resources目录中。

    4、修改application.yml文件,配置ssl安全访问,如下所示:

     1 # https的端口号设置为4433,由于我的443端口被占用了,这里使用4433端口号。
     2 server.port=4433 
     3 
     4 # keystore配置文件路径
     5 server.ssl.key-store=classpath:keystore.p12
     6 # keystore的类型
     7 server.ssl.key-store-type=PKCS12
     8 # 设置的别名
     9 server.ssl.key-alias=mytomcat
    10 # 访问密码,秘钥口令
    11 server.ssl.key-password=123456
    12 # 访问密码,秘钥库口令
    13 server.ssl.key-store-password=123456

    修改pom.xml配置文件,资源目录中增加了*.p12文件,要想让其正常执行,还需要修改resource配置,追加输出文件类型配置。

     1 <?xml version="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 
     5     https://maven.apache.org/xsd/maven-4.0.0.xsd">
     6     <modelVersion>4.0.0</modelVersion>
     7     <parent>
     8         <groupId>org.springframework.boot</groupId>
     9         <artifactId>spring-boot-starter-parent</artifactId>
    10         <version>2.3.5.RELEASE</version>
    11         <relativePath /> <!-- lookup parent from repository -->
    12     </parent>
    13     <groupId>com.example</groupId>
    14     <artifactId>demo</artifactId>
    15     <version>0.0.1-SNAPSHOT</version>
    16     <name>demo</name>
    17     <description>Demo project for Spring Boot</description>
    18 
    19     <properties>
    20         <java.version>1.8</java.version>
    21         <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
    22     </properties>
    23 
    24     <dependencies>
    25         <dependency>
    26             <groupId>org.springframework.boot</groupId>
    27             <artifactId>spring-boot-starter-web</artifactId>
    28         </dependency>
    29 
    30         <dependency>
    31             <groupId>org.springframework.boot</groupId>
    32             <artifactId>spring-boot-starter-test</artifactId>
    33             <scope>test</scope>
    34             <exclusions>
    35                 <exclusion>
    36                     <groupId>org.junit.vintage</groupId>
    37                     <artifactId>junit-vintage-engine</artifactId>
    38                 </exclusion>
    39             </exclusions>
    40         </dependency>
    41     </dependencies>
    42 
    43     <build>
    44         <plugins>
    45             <plugin>
    46                 <groupId>org.springframework.boot</groupId>
    47                 <artifactId>spring-boot-maven-plugin</artifactId>
    48             </plugin>
    49         </plugins>
    50         <resources>
    51             <resource>
    52                 <directory>src/main/resources</directory>
    53                 <includes>
    54                     <include>**/*.properties</include>
    55                     <include>**/*.yml</include>
    56                     <include>**/*.xml</include>
    57                     <include>**/*.p12</include>
    58                 </includes>
    59             </resource>
    60         </resources>
    61     </build>
    62 
    63 </project>

    此时,现在的程序中配置了https支持,但考虑到用户访问时如果使用http访问,所以需要做一个Web配置,使得通过http的80端口访问的请求直接映射到https的443端口上。Spring Boot2.x 中使用TomcatServletWebServerFactory进行接口访问转发。

     1 package com.demo.config;
     2 
     3 import org.apache.catalina.Context;
     4 import org.apache.catalina.connector.Connector;
     5 import org.apache.tomcat.util.descriptor.web.SecurityCollection;
     6 import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
     7 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
     8 import org.springframework.context.annotation.Bean;
     9 import org.springframework.context.annotation.Configuration;
    10 
    11 /**
    12  * 此类专门用来负责http连接的相关配置
    13  * 
    14  * @author
    15  *
    16  */
    17 @Configuration
    18 public class HttpConnectorConfig {
    19 
    20     /**
    21      * 
    22      * @return
    23      */
    24     public Connector initConnector() {
    25         Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
    26         // 如果现在用户使用普通的http方式进行访问
    27         connector.setScheme("http");
    28         // 用户访问的端口号是8080
    29         connector.setPort(8080);
    30         // 如果该连接为跳转,则表示不是一个新的连接对象
    31         connector.setSecure(false);
    32         // 设置转发操作端口号
    33         connector.setRedirectPort(4433);
    34         return connector;
    35     }
    36 
    37     @Bean
    38     public TomcatServletWebServerFactory servletContainerFactory() {
    39         TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory() {
    40 
    41             /**
    42              * 该方法主要进行请求处理的上下文配置, 定义新的安全访问策略。
    43              */
    44             @Override
    45             protected void postProcessContext(Context context) {
    46                 // 该方法主要进行请求处理的上下文配置, 定义新的安全访问策略。
    47                 SecurityConstraint securityConstraint = new SecurityConstraint();
    48                 // 定义用户访问约束要求
    49                 securityConstraint.setUserConstraint("CONFIDENTIAL");
    50                 SecurityCollection collection = new SecurityCollection();
    51                 // 匹配所有的访问映射路径
    52                 collection.addPattern("/*");
    53                 // 最佳路径映射访问配置
    54                 securityConstraint.addCollection(collection);
    55                 context.addConstraint(securityConstraint);
    56             }
    57         };
    58 
    59         // 连接初始化配置
    60         factory.addAdditionalTomcatConnectors(this.initConnector());
    61         return factory;
    62     }
    63 
    64 }

    访问http://localhost:8080/hello/world,发现会自动跳转到https://localhost:4433/hello/world,发现已经进行了请求地址转发了。

  • 相关阅读:
    [Bzoj2152]聪聪可可
    [2019杭电多校第七场][hdu6655]Just Repeat
    [2019杭电多校第七场][hdu6651]Final Exam
    [2019杭电多校第七场][hdu6646]A + B = C(hash)
    [2019杭电多校第六场][hdu6641]TDL
    [2019杭电多校第六场][hdu6638]Snowy Smile(维护区间最大子段和)
    abc179f
    Codeforces Round #680A
    Codeforces Round #680B
    Codeforces Round #681 D
  • 原文地址:https://www.cnblogs.com/biehongli/p/13954186.html
Copyright © 2011-2022 走看看