SpringBoot 启用 HTTPS 全流程

本文介绍如何在Spring Boot应用中配置SSL证书,并实现从HTTP到HTTPS的重定向。通过详细步骤说明证书生成及配置过程,同时提供代码示例展示如何配置HTTP重定向。

配置证书

切换到 jdk 的 bin 目录下,输入命令:

keytool -genkey -alias michaelSpica -keyalg RSA -keysize 2048 -storetype PKCS12 -keystore ebininfosoft-ssl-key.p12 -validity 3650

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vPhx35tb-1670403111426)(assets/image-20221206093840-cyhztdm.png)]

证书会下载到 jdk 的 bin 目录下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YBukqnSC-1670403111427)(assets/image-20221206092947-skqocp6.png)]

放入 SpringBoot 的资源目录,配置 application.properties

# 证书的路径,可用绝对路径,如果放到项目资源文件路径需要添加 classpath:
server.ssl.key-store=classpath:ebininfosoft-ssl-key.p12
# 证书的密码
server.ssl.key-store-password=w50029804
# 证书的类型
server.ssl.key-store-type=PKCS12

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5IVbpD5T-1670403111428)(assets/image-20221206101730-rd4vndi.png)]

pom.xml 把这个文件加入编译,如果不加会报错 Could not load key store 'classpath:ebininfosoft-ssl-key.p12'

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BiWMVtxL-1670403111429)(assets/image-20221206101843-lmp4yjh.png)]···

刷新 Maven,clean 后重新 compile。

此时若用 http 访问,则会提示:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mx4sS5aD-1670403111429)(assets/image-20221206103237-8fs3xny.png)]

http 重定向至 https

若要把对 http 端口访问的全部重定向到 https,则需配置转换器,例如将 9002 端口访问都定向到 9000:

package com.huawei.oss.telcloudsimulationuiserver.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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HttpConnectorConfig {
    /**
     * 获取Http连接器
     * @return Connector
     */
    public Connector getHttpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http"); // 使用http协议
        connector.setSecure(false); // 非安全传输
        connector.setPort(9002); // HTTP监听端口
        connector.setRedirectPort(9000); // 重定向端口
        return connector;
    }
    @Bean
    public TomcatServletWebServerFactory tomcatServletWebServerFactory() {
        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(getHttpConnector()); // 添加连接器
        return tomcat;
    }
}

同时启用 http 和 https

如果要在两个端口分别启用 http 和 https,则可做以下配置,例如将 9002 端口作为 http 访问端口:

package com.huawei.oss.telcloudsimulationuiserver.config;
import org.apache.catalina.connector.Connector;
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;

@Configuration
public class HttpConnectorConfig {
    private int httpPort = 9002;
    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addAdditionalTomcatConnectors(createStandardConnector());
        return tomcat;
    }
    private Connector createStandardConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setPort(httpPort);
        return connector;
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jIl45uMz-1670403111430)(assets/image-20221206104121-tl7w242.png)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值