-
为什么要配置https
引用wikipedia的说明Hypertext Transfer Protocol Secure (HTTPS) is an extension of the Hypertext Transfer Protocol (HTTP). It is used for secure communication over a computer network, and is widely used on the Internet. In HTTPS, the communication protocol is encrypted using Transport Layer Security (TLS) or, formerly, Secure Sockets Layer (SSL). The protocol is therefore also referred to as HTTP over TLS, or HTTP over SSL.
大概意思就是HTTPS是一个增强版的HTTP,加强点是安全性(Security),实现方式有两种,分别是TLS,SSL;这是因为HTTP协议传输的数据都是未加密的,是不安全的,是可以被篡改的,针对这些问题,于是提出了https
-
怎么配置https
这里以阿里云为例,可以申请免费证书和收费证书,个人的话可以申请免费版的,企业建议申请收费的,下面以免费版为例,地址:https://yundun.console.aliyun.com/?spm=5176.12818093.ProductAndService–ali–widget-home-product-recent.dre3.5adc16d0A2FsyJ&p=cas#/certExtend
购买成功后,还需要创建证书
证书申请(没有域名需要先申请域名,ECS及域名申请:https://blog.csdn.net/jornada_/article/details/116400554)
验证信息、提交审核
审核通过后,按需下载对应的证书
-
证书安装
java应用以springboot为例,将上一步申请的证书下载下来,下载——服务器类型选择Tomcat,下载下来有两个文件,一个是证书文件,一个是密码;将证书文件拷贝到应用resources目录,添加配置http.port=80 server.port=443 server.ssl.key-store=classpath:证书目录 server.ssl.key-store-password=密码 server.ssl.keyStoreType=PKCS12
/** * https配置 */ @Component public class HttpsConfig { /** * bean注入 */ @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(redirectConnector()); return tomcat; } /** * http转https */ private Connector redirectConnector() { Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL); connector.setScheme("http"); connector.setPort(80); connector.setSecure(false); connector.setRedirectPort(443); return connector; } }
配置好后,打包、部署应用,访问查看是否生效
个人网站配置https
最新推荐文章于 2021-07-26 09:53:54 发布