SpringBoot 实现 https 访问, 并实现 http 访问自动转 https 访问

SpringBoot 实现 https 访问, 并实现 http 访问自动转 https 访问

1. 使用 jdk 自带的 keytools 创建证书

# -alias 产生别名 每个keystore都关联这一个独一无二的alias, 这个alias通常不区分大小写
# -keystore 指定密钥库的名称(产生的各类信息将不在.keystore文件中)
# -validity 指定创建的证书有效期多少天(默认: 90)
# -keysize 指定密钥长度(默认: 1024)
keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore /home/ubuntu/answer/fabric/tomcat.keystore -validity 3650


Enter keystore password:  					# 123456
Re-enter new password: 						# 123456
What is your first and last name?
  [Unknown]:  l
What is the name of your organizational unit?
  [Unknown]:  l
What is the name of your organization?
  [Unknown]:  l
What is the name of your City or Locality?
  [Unknown]:  l
What is the name of your State or Province?
  [Unknown]:  l
What is the two-letter country code for this unit?
  [Unknown]:  l
Is CN=l, OU=l, O=l, L=l, ST=l, C=l correct?
  [no]:  yes

2. 将 生成的 tomcat.keystore 文件复制到项目的 resources 目录下

3. 在 application.properties 文件中添加如下配置

server.port=8888
server.http.port=8080

# 生成的证书文件
server.ssl.key-store=classpath:tomcat.keystore
server.ssl.key-store-type=PKCS12

server.ssl.enabled=true
# 密钥库密码
server.ssl.key-store-password=123456
server.ssl.key-alias=tomcat

4. http 访问自动转 https 访问

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;

public class SpringbootMybatisWebApplication {
    
    @Value("${server.port}")
    private Integer httpsPort;

    @Value("${server.http.port}")
    private Integer httpPort;
    
    
    public static void main(String[] args) {
		SpringApplication.run(SpringbootMybatisWebApplication.class, args);
	}
    
    
    @Bean
    public TomcatServletWebServerFactory servletContainer() {
    //		TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() { ... }

        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;
    }

    @Bean
    public Connector connector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(httpPort);
        connector.setSecure(false);
        connector.setRedirectPort(httpsPort);
        return connector;
    }
}

5. 访问验证

https://localhost:8888/smw/user/findUsers

http://localhost:8080/smw/user/findUsers

6. 报错分析

# 启动报错: java.security.NoSuchProviderException: no such provider: PKCS12

# 注释掉 application.properties 中 如下配置
server.ssl.key-store-provider=PKCS12
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jaemon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值