博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[SSL]——如何使用SpringBoot内置的tomcat配置SSL——>从而实现HTTPS访问(基于阿里云云服务器)
阅读量:4047 次
发布时间:2019-05-25

本文共 2459 字,大约阅读时间需要 8 分钟。

最近一直在搞微信小程序,然后我是负责后端接口的设计,功能的实现。

因为小程序中请求接口需要https格式。

第一次研究这个,搞了今天一天,虽然走了很多弯路! 不过! 现在发现! 很简单的!

​​​​​​​大概介绍

  • 下载SSL证书,因为我用的是阿里云服务器,所以就直接从阿里云申请了免费的证书

  • 将证书复制到项目中并进行配置

一:SSL证书 

 因为我是用了springboot内置的tomcat,所以下载的就是tomcat版本的证书;

一个pfx文件,一个pfx密码文件

二:项目配置

打开项目目录,在src->main->resources中,把我们上面的两个文件放进去

接下来就是修改application配置文件,application配置文件有两种格式properties和yml ;

内容都相同,但就是格式不同;并且千万不要混用!

我项目文件使用的yml,我将properties的也写在下面。

application.yml添加的内容如下:

server:    port: 443   #https的默认端口就是433 不能修改    ssl:        key-store: classpath:3516608_www.shiwanfute.cn.pfx   #证书pfx的名字        key-store-type: PKCS12                               #证书的类型        key-store-password: ********                         #pfx-password.txt中的密码

application.properties添加的内容如下: 

三:如果也要使用http,可以加入以下代码,将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;import org.springframework.boot.web.servlet.server.ServletWebServerFactory;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * http重定向到https * @return */@Configurationpublic class SecurityConfig{    @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;    }    @Bean    public Connector httpConnector() {        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");        connector.setScheme("http");        //Connector监听的http的端口号        connector.setPort(8080);        connector.setSecure(false);        //监听到http的端口号后转向到的https的端口号        connector.setRedirectPort(443);        return connector;    }}

四:将项目打包成jar包

打开项目文件夹-->targer文件夹,就可以看到jar包啦

 

 

 

转载地址:http://gnzci.baihongyu.com/

你可能感兴趣的文章
简单理解Socket及TCP/IP、Http、Socket的区别
查看>>
利用HTTP Cache来优化网站
查看>>
利用负载均衡优化和加速HTTP应用
查看>>
消息队列设计精要
查看>>
分布式缓存负载均衡负载均衡的缓存处理:虚拟节点对一致性hash的改进
查看>>
分布式存储系统设计(1)—— 系统架构
查看>>
MySQL数据库的高可用方案总结
查看>>
将数据直接上传到分区目录(hdfs)上,让Hive分区表和数据产生关联有哪些方式?
查看>>
Hive 中分区是否越多越好?
查看>>
Hive 的分桶表是什么?有什么作用?
查看>>
Hive 桶表是否可以通过直接 load 将数据导入?
查看>>
Hive 分区和分桶的区别?
查看>>
order by,sort by,distribute by,cluster by的区别是什么?
查看>>
聚合函数是否可以写在order by后面,为什么?
查看>>
什么情况下 Hive 可以避免进行 MapReduce?
查看>>
Hive 的文件存储格式怎么选择?
查看>>
Hive 的数据压缩格式怎么选择?
查看>>
Hive 的 SerDe 是什么?
查看>>
Hive 中如何解决多字符分割场景?
查看>>
一篇文章搞懂 Hive 的调优思路
查看>>