SpringBoot配置HTTPS

1.生成HTTPS证书

打开cmd执行命令

-alias设置别名
-storetype 设置证书格式
-keyalg设置加密算法
-keysize设置证书大小
-keystore设置证书文件地址
-validity设置有效天数。

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

执行后根据提示输入相关内容,输入完毕后会得到一个是否确认的提示,输入y 按回车确认就可以了

最后会得到一个keystore.p12文件,文件位置一般在 C://users/当前用户名 这个目录下,找不到的可以在users下进行文件检索

把它复制到项目resources目录下。

2.在application.yml中配置HTTPS相关信息

我把我的文件都粘出来,大家查漏补缺吧

server:
  port: 8443   <!-- https链接端口 -->
  ssl:
    key-store: classpath:keystore.p12  <!-- 刚才生成的https证书地址 -->
    key-store-password: wangyongqi     <!-- 刚才在命令行中输入的密码 -->
    key-store-type: PKCS12             <!-- 协议类型 -->
    key-alias: tomcat
    enabled: true

spring:
  datasource:
    username: <!-- mysql用户名 -->
    password: <!-- mysql密码 -->
    url: <!-- mysql链接地址 -->
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
  application:
    name: <!-- 这个是当前访问的用户名,可以使用mysql用户名或者随意设置一个 -->
  http:
    encoding:
      charset: UTF-8
      force: true
  jackson:
    default-property-inclusion: non_null

mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml  <!-- mybatis文件位置扫描 -->
  type-aliases-package: com.xxx.entity    <!-- mybatis文件实体类扫描 -->

3.配置https请求映射

package com.xxx.config;

import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * https 请求配置类
 */
@Configuration
public class ConnectorConfig {

    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");//设置所有路径都配置https
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(getHttpConnector());
        return tomcat;
    }

    private Connector getHttpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(8088);
        connector.setSecure(false);
        connector.setRedirectPort(8443);
        return connector;
    }
}

配置好这些就可以根据  https://localhost:8443/hello 进行访问了

发布了110 篇原创文章 · 获赞 475 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/yongqi_wang/article/details/104063641
今日推荐