时间
2019年9月28日 http->https(tomcat),Nginx
2019年9月29日 http->https(php)
2019年9月29日 http->https(springboot)
效果展示
tomcat nginx 经过
前几天突然想看看心安的博客,打开后发现了让你的网站免费开启 HTTPS -- 从对称加密到非对称加密的文章,于是想把自己的网站也弄成https访问的效果。
在心安的博客里,没有写如何使用tomcat,只有nginx的,我便自己捣腾tomcat的方式。
在server.xml中,需要将
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
改成
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="443" />
目的是为了让默认的80端口转发到443端口(443端口是https默认端口)。
还需要在其中配置
<Connector port="443"
protocol="org.apache.coyote.http11.Http11Protocol"
SSLEnabled="true"
scheme="https"
secure="true"
keystoreFile="cert/2651923_lixingyu.cn.pfx"
keystoreType="PKCS12"
keystorePass="E90UAIzD"
clientAuth="false"
SSLProtocol="TLSv1+TLSv1.1+TLSv1.2"
ciphers="TLS_RSA_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_256_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_256_CBC_SHA256"/>
其中keystoreFile是证书文件,keystorePass是密码,这两个需要在阿里云上申请然后免费下载。
上面的步骤完成后,效果是输入www.lixingyu.cn,不会跳转到https://www.lixingyu.cn。
刚开始问心安的时候,他给我的解决办法是使用tomcat的301重定向。
我用了相应的jar包,配置了web.xml后还是没法自动跳转,就放弃了。
于是我想着去学一点nginx。
经过一阵折腾之后。
其中
return 301 https://$server_name$request_uri;
这段代码主要是用来监听8080端口后,把所有的链接都转发到带有https的链接上面去,也就实现了访问http://www.lixingyu.cn转到了https://www.lixingyu.cn。
坑
在28日下午,我弄好了上面的一切后,想使用nginx来实现https,我就继续搞,后面始终无法转到https,甚至连原本的网页都无法访问。后面我才发现,是因为之前使用tomcat实现301重定向的时候,我在web.xml中加入了
这个jar包里面的代码。导致我tomcat直接不能访问。
php 经过
今天上课的时候我发现原本的网盘不能使用,原因是https://www.lixingyu.cn/login.html是使用的https访问,而网盘使用的是http,https中不能访问http的链接,于是我想着把php的也升级成https。
我看了 CentOs 搭建Apache + PHP + OpenSSL实现https访问 这篇文章后,安装成功。其中
SSLCertificateFile /cert/2651923_lixingyu.cn_public.crt
SSLCertificateKeyFile /cert/2651923_lixingyu.cn.key
SSLCertificateChainFile /cert/2651923_lixingyu.cn_chain.crt
分别对应三个文件。
由于在tomcat中配置了443端口,所以php的端口不能和tomcat的冲突,于是
我把它改成了444端口。
springboot经过
效果
给心安看的时候,他说了一句
我就想把springboot的也弄成https试试
于是在网上找教程
步骤:
第一步:把tomcat证书下载下来放在resources目录
第二步:在properties中配置:
#https
server.ssl.key-store=classpath:证书名
server.ssl.key-store-password=密码
server.ssl.key-store-type=PKCS12
第三步:新建一个java文件,在里面加入
package cn.lixingyu.springmybatisthymeleaf.config;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author lxxxxxxy
* @time 2019/09/29 10:30
*/
@Configuration
public class StartAppHttps {
@Bean
public TomcatServletWebServerFactory 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(initiateHttpConnector());
return tomcat;
}
private Connector initiateHttpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8888);
connector.setSecure(false);
connector.setRedirectPort(8081);//这个端口写properties中配置的端口
return connector;
}
}
于是就实现了访问http://www.lixingyu.cn:8888跳转到https://www.lixingyu.cn:8081。