个人学习系列 - Spring Boot 集成 https

最近都在用https了,可是我们创建一个springboot的项目还是用http,今天我们来看一下怎么将http变成https。。。

1. 生成证书

这里我们需要使用jdk自带的keytool命令生成证书并复制到我们项目的
图片.png
目录下。

1.1 打开CMD

打开我们安装的jdk的bin目录:
图片.png

1.2 使用keytool命令生成证书

使用keytool命令生成证书:

keytool 
-genkey 
-alias server(别名) 
-keypass 123456(别名密码) 
-keyalg RSA(算法) 
-keysize 1024(密钥长度) 
-validity 365(有效期,天单位) 
-keystore D:/keys/server.keystore(指定生成证书的位置和证书名称) 
-storepass 123456(获取keystore信息的密码)

根据自己的实际情况按照此格式进行生成即可:
在这里插入图片描述

查看目标文件夹发现证书已经生成完毕!
图片.png

2. 新建springboot项目

2.1 pom.xml

这里我们只是引入web即可

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.2 application.yml
server:
  ssl:
    # 证书路径
    key-store: classpath:server.keystore
    # 与申请时输入一致
    key-alias: server
    enabled: true
    key-store-type: JKS
    #与申请时输入一致
    key-store-password: 123456
    # 浏览器默认端口 和 80 类似,https默认的端口号为443
  port: 443
2.3 HttpsConfig配置文件
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p>
 * HTTPS 配置类
 * </p>
 *
 * @author www.zhouzhaodong.xyz
 * @date Created in 2020/9/17 14:30
 */
@Configuration
public class HttpsConfig {
    /**
     * 这里需要查看application.yml里面的端口号配置
     * 配置 http(80) -> 强制跳转到 https(443)
     */
    @Bean
    public Connector connector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(80);
        connector.setSecure(false);
        connector.setRedirectPort(443);
        return connector;
    }

    @Bean
    public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector) {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(connector);
        return tomcat;
    }
}
2.3 创建一个HttpsController
/**
 * 控制器
 * @author www.zhouzhaodong.xyz
 * @date Created in 2020/9/17 14:30
 */
@RestController
public class HttpsController {

    @RequestMapping("/")
    public String https(){
        return "success";
    }

}

3. 进行测试

启动项目,浏览器访问 http://localhost 将自动跳转到 https://localhost并显示内容:

图片.png

个人博客地址:

http://www.zhouzhaodong.xyz

项目GitHub地址为:

https://github.com/zhouzhaodong/springboot/tree/master/spring-boot-https

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值