java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Springboot配置阿里云SSL

Springboot项目配置阿里云SSL证书的实现步骤

作者:ノBye~

本文详细介绍了SpringBoot项目中配置HTTPS,包括证书申请、下载及安装过程,及设置HTTP自动重定向至HTTPS,确保网站数据传输的安全,感兴趣的可以了解一下

1.证书申请(自行选购)

2.下载证书

3.解压下载压缩包

4.将.pfx文件放至springboot项目resources目录下

applicatio.yml中添加配置

5.设置http自动重定向https(8082端口->443端口):

在SpringApplication启动类中加入以下代码:(注意网上有些代码中的EmbeddedServletContainerFactory找不到是因为springboot2.X之后改了写法)

    //拦截所有请求
    @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(httpConnector());
        return tomcat;
    }
 
    //配置http转https
    @Bean
    public Connector httpConnector() {
        Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
        connector.setScheme("http");
        //Connector监听的http的端口号
        connector.setPort(8082);
        connector.setSecure(false);
        //监听到http的端口号后转向到的https的端口号
        connector.setRedirectPort(443);
        return connector;
    }

6.已测

到此这篇关于Springboot项目配置阿里云SSL证书的实现步骤的文章就介绍到这了,更多相关Springboot配置阿里云SSL内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

您可能感兴趣的文章:
阅读全文