本文以msh.com
域名为例
一、修改页面内的 HTTP 协议为HTTPS协议
HTTPS 下不允许 HTTP 请求,如果有此情况,则会在浏览器控制台报如下错误
(index):42 Mixed Content: The page at 'https://b.test.iupiao.cn/' was loaded over HTTPS, but requested an insecure stylesheet 'http://at.alicdn.com/t/font_203680_of0yc4ur0ie.css'. This request has been blocked; the content must be served over HTTPS.
或者
Mixed Content: The page at 'https://b.test.iupiao.cn/' was loaded over HTTPS, but requested an
错误原因可能如下:
- 当前的HTTPS所承载的页面,但是在页面内引用了通过HTTP 协议访问的静态资源
- 当前的HTTPS所承载的页面,发送了请求路径为HTTP 协议的Ajax 异步请求
解决方案
将所有的HTTP 协议的URL全局替换为HTTPS协议
二、 修改页面内的ws协议为wss协议
Nginx做全站HTTPS升级的时候,是不涉及到WebSocket相关的配置的
唯一需要做的,就是升级成功后,在前端代码里面把ws://
协议替换为wss://
即可
想深入了解的话,请参考这篇文章: WebSocket 和socket、HTTP的区别和联系
三、后端改造
本项目用Java
作为后端语言。在后端,通过Spring
的RestTemplate
来做各个后端服务直接的数据请求
全站升级HTTPS后,RestTemplate 请求https路径下的接口时会报错,解决方式参见下方代码
原理:
- 把RestTemplate 的实例bean的生成方法重写一遍,使之信任所有的安全证书
- spring在实例化RestTemplate 的时候,会调用此方法,
把下面代码放到任意一个spring管理下的service或者controller即可,之后spring里面的所有的RestTemplate 实例就会通过此方法生成
@Bean
public RestTemplate restTemplate()
throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
.loadTrustMaterial(null, acceptingTrustStrategy)
.build();
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(csf)
.build();
HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory);
return restTemplate;
}
系列文章