Springboot内嵌tomcat如何发布阿里云SSL签证

1.Springboot进行注入Bean在tomcat中(此功能是为了http->https)

import org.springframework.context.annotation.Configuration;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
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.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;

@Configuration
public class ConnectorConfig {


    @Bean
    public ServletWebServerFactory servletWebServerFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection securityCollection = new SecurityCollection();
                securityCollection.addPattern("/*");
                securityConstraint.addCollection(securityCollection);
                context.addConstraint(securityConstraint);
            }
        };
        factory.addAdditionalTomcatConnectors(redirectConnector());
        return factory;
    }

    private Connector redirectConnector() {
        Connector connector = new Connector(Http11NioProtocol.class.getName());
        connector.setScheme("http");
        connector.setPort(80);
        connector.setSecure(false);
        connector.setRedirectPort(8090);//这个端口号是你yml配置的server.port
        return connector;
    }

}

2.阿里云去进行下载签名选择下载
在这里插入图片描述
3.yml中配置
(有两种签名类型如果你是jdk8,则进行使用pfx转化jks)

#pfx版本
server:
   port: 8090
   ssl:
     key-store: classpath:xxx.pfx(在阿里云下载的pfx文件)
     key-store-type: PKCS12
     key-store-password: xxx(在阿里云下载的pfx-password里面的内容)
     enabled: true
#JKS版本(该JKS的文件需要用到jdk8的内置工具)
keytool -importkeystore -srckeystore  xxx.pfx -srcstoretype pkcs12 -destkeystore xxx.jks  -deststoretype JKS
server:
   port: 8090
   ssl:
     key-store: classpath:xxx.jks(该JKS的文件需要用到jdk8的内置工具)
     key-store-type: JKS
     key-store-password: xxx(在阿里云下载的pfx-password里面的内容)
     enabled: true     

4.服务器中如何配置ssl(重点防止采坑)
这里你需要把这个文件放进跟jar的同级目录中
由于打入的jar中不包含对应pfx文件
yml的写法需要去掉 classpath 再进行部署

#pfx/jks
server:
   port: 8090
   ssl:
     key-store: xxx.pfx(在阿里云下载的pfx文件)
     key-store-type: PKCS12
     key-store-password: xxx(在阿里云下载的pfx-password里面的内容)
     enabled: true

在这里插入图片描述
5.如何校验是否成功
直接在浏览器中输入你的对应的域名以及https的页面
我是用swagger的界面直接打开,即可
https://www.dapengzx.com:8090/swagger-ui.html#/

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值