将SpringBoot项目的HTTP站点改为HTTPS的全过程
作者:麦芽糖0219
本文介绍了如何在Spring Boot项目中实现HTTPS请求,首先,创建一个Spring Boot项目并编写一个BasicController.java文件,然后,配置application.yaml文件以支持HTTPS,接着,使用Java自带的keytool工具生成一个免费的HTTPS证书,需要的朋友可以参考下
一 新建springboot项目
我们先新建一个springboot项目,写一个BasicController.java,内容如下
package com.https.httpsdemo.demos.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
public class BasicController {
// http://127.0.0.1:8081/hello
@GetMapping("/hello")
@ResponseBody
public String hello() {
return "Hello ";
}
}
application.yaml内容如下
server: port: 8081
浏览器访问http://127.0.0.1:8081/hello如下
接下来开始把http请求改为https
二 https证书
首先需要有一个 https 证书,可以从各个云服务厂商处申请一个免费的(我申请的为阿里云SSL),也可以直接借助 Java 自带的 JDK 管理工具 keytool 来生成一个免费的 https 证书。
找到命令窗口,右键选择管理员打开

执行如下命令
keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650
命令含义如下:
- genkey 表示要创建一个新的密钥。
- alias 表示 keystore 的别名。
- keyalg 表示使用的加密算法是 RSA ,一种非对称加密算法。
- keysize 表示密钥的长度。
- keystore 表示生成的密钥存放位置。
- validity 表示密钥的有效时间,单位为天。

如上图,以此输入相关信息即可。
在C:\Windows\System32目录可以看到生成了文件

把上边这个文件复制到项目根目录下

application.yml文件如下
# 应用服务 WEB 访问端口
server:
port: 8081
ssl:
# 证书路径,秘钥文件路径,也可以配置绝对路径
key-store: keystore.p12
# 证书密码,秘钥生成时在终端输入的秘钥口令
key-store-password: 123456
# 秘钥类型,与秘钥生成命令一样
key-store-type: PKCS12
# 证书别名,与秘钥生成命令一样
key-alias: tomcat
启动类添加如下内容
package com.https.httpsdemo;
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.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class HttpsDemoApplication {
public static void main(String[] args) {
SpringApplication.run(HttpsDemoApplication.class, args);
}
@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("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(redirectConnector());
return tomcat;
}
/**
* 浏览器访问http://127.0.0.1:8080/hello时,自动跳转到https://127.0.0.1:8081/hello
* @return
*/
private Connector redirectConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8080);
connector.setSecure(false);
connector.setRedirectPort(8081);
return connector;
}
}
此时,我们在浏览器就可以通过https来访问https://127.0.0.1:8081/hello了
因为我用的是自己生成的ssl证书,所以https请求是不被客户端识别的,如下

点击后,内容如下

注意:生产环境中,则需要购买合法的 SSL 证书。
由于在启动类已经进行了配置,所以我们也可以通过访问http://127.0.0.1:8080/hello来自动跳转到https://127.0.0.1:8081/hello

以上就是将SpringBoot项目的HTTP站点改为HTTPS的全过程的详细内容,更多关于SpringBoot HTTP站点改为HTTPS的资料请关注脚本之家其它相关文章!
