源码时代Java干货分享|手把手教你SpringBoot配置ssl证书

图说明
在这里插入图片描述
第一步首先去阿里云弄一个免费的SSL证书
在这里插入图片描述
下载然后 放到项目里面的resource路径下
这里一定要注意 是 key-store 和 key-store-password 我在配置时写出了 key-password 弄了很久没找到原因 换成了
nginx 去配置,最近还是嫌弃服务启动太多 改了回来
在这里插入图片描述
现在如果直接方法服务器上那么现在 就可以https 访问我的项目了 但是如果你用http 就不行 因为http是默认80端口 而https 是默认的 443 端口 所以我在我的启动类里面写一段代码 。防护http 是自动跳转到https
EmbeddedServletContainerFactory 可以因为你的spring boot 版本 过高没有这个class 那么你需要去你对应spring boot版本的class

public class HuahaiApplication {

   public static void main(String[] args)
   {
      SpringApplication.run(HuahaiApplication.class, args);
   }

   /**
    * 配置一个 TomcatServletWebServerFactory bean
    * 将http 重定向到 https
    * @return
    */
    /**
     * it's for set http url auto change to https
     */
    @Bean
    public EmbeddedServletContainerFactory servletContainer(){

        TomcatEmbeddedServletContainerFactory tomcat=new TomcatEmbeddedServletContainerFactory(){
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint=new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");//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("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(80);
        connector.setSecure(false);
        connector.setRedirectPort(443);
        return connector;
    }

}

最近我可以把我本地的服务映射到外网去访问借助一个 sunny-ngrok 免费的
在这里插入图片描述
去域名管理中心解析你的域名

在这里插入图片描述
本地启动ngrok 的服务
在这里插入图片描述
效果
输入你的域名 不大https 也会自动跳转到https 这里是因为我刚刚在启动类里面配置了 重定向
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值