友情关注:2021年底即将推出我们墨飞鱼团队历时大半年研发的灵感写作平台,将会为微信读书的用户提供免费的笔记素材整理工具,借助全球领先的NLP AI模型与百万金句素材数据库,助力您的写作灵感火花不断, 敬请收藏关注这个地址,12月底见哦~
自建网站,有时候在微信页面打开,或浏览器中打开,非https的网页会呈现不安全链接等提醒,就需要把网站转化为安全链接的形式,即通过ssl通讯,转换方法如下.
申请SSL证书并下载
以阿里云网站上申请的域名为例,你可以有20个免费证书,可以在阿里云网站通过搜索'SSL证书',找到证书资源包信息,然后申请免费证书,个人或企业均可获得20张免费证书的额度.
申请成功后,可以选择相应的证书类型,比如对springboot来说,其已经内置了Tomcat,所以可以下载Tomcat对应的证书
申请完证书后,即可在springboot项目中安装证书
一. 先将我们生成的证书移到springboot项目的resource目录下
这里我们的证书名称为'5173192_res.limuqiao.com.pfx',将其复制到resource目录下
二. 在application.yml中加入以下配置
server:
port: 443
ssl:
key-store: classpath:5173192_res.limuqiao.com.pfx
key-store-password: yx9HHT5G ---证书密码
key-store-type: PKCS12
enabled: true
重定向http到https
因为我们原来的请求方式都是http, 现在我们想使用https,就需要做一下重定向,不能跟之前的冲突。
第一种方式 在java代码的启动类里直接变动
比如以下代码中原来9035端口的请求全都转向443端口处理
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.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
/**
* @author Binary Wang(https://github.com/binarywang)
*/
@SpringBootApplication
public class WxCpApplication {
public static void main(String[] args) {
SpringApplication.run(WxCpApplication.class, args);
// SHA1.gen("1jKZSbU47pn23GwYDQfh32zJvGS4hSH2wSWJB27Zz2b", timestamp, nonce, data)
}
/**
* 配置一个 TomcatServletWebServerFactory bean
* 将http 重定向到 https
* @return
*/
@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;
}
/**
* 让我们的应用支持HTTP是个好想法,但是需要重定向到HTTPS,
* 但是不能同时在application.properties中同时配置两个connector,
* 所以要以编程的方式配置HTTP connector,然后重定向到HTTPS connector
* @return Connector
*/
private Connector initiateHttpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(9035); // http端口
connector.setSecure(false);
connector.setRedirectPort(443); // application.properties中配置的https端口
return connector;
}
}
此处重定向之后,考虑到有的使用nginx对80端口的访问转向了localhost的某一端口地址,可能会导致重定向失败,还有个方法就是可以考虑在页面的header处加入<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests"/>来使得所有http请求替换为https请求
如果系统通过了nginx做代理,则可以考虑第二种方式
即在nginx中对所有http请求重定向到https请求,如下变动
server{
listen 80;
server_name XXXXX.com; //你的域名
return 301 https://$server_name$request_uri;
location ~ / {
index index.html index.php index.htm;
}
}
Comments | NOTHING