0 实验准备
准备一个可以正常使用的域名,这里使用的是 test.duqingquan.com
准备一个可以正常使用服务器,并正确设置域名解析。 这里是 debian9.6
准备一个web服务器, 这里用的是tomcat 9.0.41
准备一个安装JDK的计算机。这里安装的是Open JDK 11.
1 生成必须的证书文件
有条件的可以使用CA认证的,我这里使用的自签名的文件。
将下面的脚本写入一个文本文件,重命名为 auto.bat (windows平台)
```
echo "auto create key"
# 生成服务端keystore -> server.jks
keytool -genkeypair -v -alias tomcatKey -keyalg RSA -validity 3650 -keystore ./server.jks -storepass 886688 -dname "CN=duqingquan.com" -ext SAN=dns:test.duqingquan.com
# 生成服务器端公钥至 server.cer
keytool -exportcert -alias tomcatKey -keystore ./server.jks -file ./server.cer -storepass 886688
# 生成客户端keystore -> client.p12
keytool -genkeypair -v -alias client -dname "CN=duqingquan.com" -keyalg RSA -validity 3650 -keystore ./client.p12 -storepass 336633 -storetype PKCS12
# 生成客户端公钥 -> client.cer
keytool -exportcert -alias client -file ./client.cer -keystore ./client.p12 -storepass 336633 -storetype PKCS12
# 让服务器keystore信任客户端公钥
keytool -importcert -alias clientca -keystore ./server.jks -file ./client.cer -storepass 886688
# 让客户端keystore信任服务器公钥
keytool -importcert -alias serverca -keystore ./client.p12 -file ./server.cer -storepass 336633
# 打印服务器keystore
keytool -list -keystore ./server.jks -storepass 886688
# 打印服务器公钥
keytool -rfc -printcert -file .\server.cer -v
pause
```
注意替换其中的秘钥,域名和机构信息。
双击执行后,我们得到了我们需要的所有证书文件。
所有需要的证书物料2 配置tomcat服务器
2.1 tomcat 安装配置
略
2.2 配置server.xml
上传server.jks文件至 tomcat安装目录下,conf文件夹下
进入tomcat服务器,conf文件夹,编辑server.xml,对connector字段添加如下配置
文本内容
<Connector
protocol="org.apache.coyote.http11.Http11NioProtocol"
sslImplementationName="org.apache.tomcat.util.net.jsse.JSSEImplementation"
port="13888" maxThreads="200"
scheme="https" secure="true" SSLEnabled="true"
truststoreFile="conf/server.jks" truststorePass="886688"
keystoreFile="conf/server.jks" keystorePass="886688"
clientAuth="true" sslProtocol="TLS"/>
保存server.xml 重启tomcat.
打开tomcat根目录下的logs目录,
使用 tail -f catalina.out 查看tomcat实时日志,如果server.xml配置有误,在这里可以看到报错信息。
tomcat成功重启后,访问
https://test.duqingquan.com:13888/examples/servlets/servlet/HelloWorldExample
提示如下信息,说明服务器端配置成功。
2.3 配置浏览器证书
双击 client1.cer文件,进行安装
安装成功后,此时再次访问,报错信息变成了
再次安装,客户端keystore : client.p12
安装成功后,重新访问。会提示选择客户端证书。
选择证书后,可以正常访问。
3 uni-app 配置访问
3.1 前提条件
使用HX 3.2.0以上版本
3.2 调用代码示例
定义androidTlsConfig3.3 测试步骤
使用下面的测试用例代码,运行后界面
运行后的界面示例在不动态安装DCloud CA的前提下,依次访问三个网站:
接下来,动态下载DCloud官网证书。
再次测试三个网站
测试用例代码:
```
<template>
<view>
<button @click="downloadCAFile"> downloadCAFile</button>
<button @click="taobaoTlsRequest"> taobaoTlsRequest</button>
<button @click="dcloudTlsRequest"> dcloudTlsRequest</button>
<button @click="goTlsRequest"> goTlsRequest</button>
<text>{{requestResult}}</text>
</view>
</template>
<script>
export default {
data() {
return {
androidConfig : {
keystore:'data:keystore/p12;base64,MIIMywIBAzCCDIQGCSqGSIb3DQEHAaCCDHUEggxxMIIMbTCCBWkGCSqGSIb3DQEHAaCCBVoEggVWMIIFUjCCBU4GCyqGSIb3DQEMCgECoIIE+zCCBPcwKQYKKoZIhvcNAQwBAzAbBBRsUupaTHglPzY1OVEL3zpVQ1BeQAIDAMNQBIIEyOgi16viECt7wPU5KkWhSr1oiN7OtlVkLEAAkg0Mub+lwGtqVv1ggmgrKipXdzYACjbMS6atW+VgfYsOeIfajmZgkx668jeHtaLkg3Ejm5ha2iA1DppPtiAc+5lDMMbdyM3ZkNmS2ZCUNMGLJvJqnOf+rKbfxeJs0FYa4b7pj6HXv1RWp37DOvhuwUQubF6xms5+JO8Zc+ak+JYdWSWetP9ThBPXhuDtqPByDberKpSIK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W6dq4PkcFqrmS7ZwL6nFJ/YH\nZqXMK9NrbDZEVTpdmlt/pu/l+/Rex99Rkc6ws4Z9YI/p0LkMgjqL9LcAag1fJZK/\n2YLCN2hk+ujwGuz2qmJxAoq2oKefWSghjGkzb47y9g7h6acXlIWA42+7lyeh8iPD\nUkkGHj+kJOsF3VykDhV3FMpzcMfMIx7HMWJt4NVYjgourVhXKQ6JXACDBzD2vfki\n/M79h7o3PHiNE8ZGe8tNuexDnR1eR2ftywIDAQABo0EwPzAdBgNVHQ4EFgQUhAfB\nEwQZzkblt9LnGm+mANYPtRswHgYDVR0RBBcwFYITdGVzdC5kdXFpbmdxdWFuLmNv\nbTANBgkqhkiG9w0BAQsFAAOCAQEAGhB6wR8oASt+MMQy9zFq0p4fnoE2UzrmJ2uo\nCUVJZo+qnrutA1gzI/b4jOEFP/x4iVwfHRqycz9D0RWWheVYGyvFCXeuxcFTynnR\nOG8UtbUCuO+a7KDBc8RfbKHopyvBAIjnNzP/Xd4qNaCA/wQDDbpIwyS8qDcOb4YR\nArtPeiOnWWt3cLZ0Sw9mmCp3h8VEtgqyukuF8M36cPneE3LXtDtfL+YTZWIHUo1e\nZIV/pFtunbJ7TDm9RrSpSB9fmP0eflVSYCpOPvuhFdrf3IJLzhJcf06Ve58TcvLg\nUfMzW7OCLhOke6Sq3VM/5HCok70q+iRPkBYifjDqBzAqidbTiw==\n-----END CERTIFICATE-----"],
},
requestResult:""
}
},
methods: {
downloadCAFile:function() {
var that = this;
if(that.androidConfig.ca.length > 1){
console.log("远程公钥文件已装载");
return ;
}
uni.downloadFile({
//仅为示例,这里的证书公钥可以从浏览器导出
url: 'https://dcloud.oss-cn-hangzhou.aliyuncs.com/UniADSDK/res/DCloud.pem',
success: (res) => {
// 这里的mime协议头是固定格式
let downloadCAFilePath = "data:keystore/p12;filepath," + res.tempFilePath;
console.log('downloadCAFilePath === ' + downloadCAFilePath);
that.androidConfig.ca.push(downloadCAFilePath);
console.log('that.androidConfig.ca === ' + JSON.stringify(that.androidConfig.ca));
}
});
},
goTlsRequest:function(){
var that = this;
uni.request({
url:"https://test.duqingquan.com:13888/examples/servlets/servlet/HelloWorldExample",
method:"get",
androidTLSConfig:this.androidConfig,
sslVerify:true,
success: (res) => {
console.log("success === " + JSON.stringify(res));
that.requestResult = res;
},
fail: (res) => {
console.log("fail === " + JSON.stringify(res));
that.requestResult = res;
}
});
},
dcloudTlsRequest:function(){
var that = this;
uni.request({
url:"https://ask.dcloud.net.cn/people/i7788",
method:"get",
androidTLSConfig: this.androidConfig,
success: (res) => {
console.log("success2 === " + JSON.stringify(res));
that.requestResult = res;
},
fail: (res) => {
console.log("fail2 === " + JSON.stringify(res));
that.requestResult = res;
}
});
},
taobaoTlsRequest:function(){
var that = this;
uni.request({
url:"https://www.taobao.com/",
method:"get",
androidTLSConfig: this.androidConfig,
success: (res) => {
console.log("success3 === " + JSON.stringify(res));
that.requestResult = res;
},
fail: (res) => {
console.log("fail3 === " + JSON.stringify(res));
that.requestResult = res;
}
});
},
}
}
</script>
<style>
.container {
padding: 20px;
font-size: 14px;
line-height: 24px;
}
.swiper-item {
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #999;
color: #fff;
}
.image {
width: 750rpx;
}
//css
.button-icon {
background-image: linear-gradient(to right, rgb(0, 238, 255), rgb(0, 193, 252));
border-radius: 50rpx;
height: 65rpx;
padding-left: 20rpx;
padding-right: 20rpx;
overflow: hidden;
}
.myMenu-icon {
font-size: 50rpx;
padding-top: 8rpx;
border-radius: 50rpx;
overflow: hidden;
}
</style>
```
网友评论