NOde JS Mac 下创建HTTPS 服务以及Android 端OKGO 访问HTTPS 小结
来源:互联网 发布:网络就是计算机的集合 编辑:程序博客网 时间:2024/06/06 09:11
1. mac 下创建NODE JS HTTPS
实现https,需要有证书。在mac下,你可以进行如下操作。
首先,打开终端,创建一个文件夹
mkdir certificate
进入该文件夹
cd certificate
然后输入openssl (mac下openssl是系统自带的,可以直接在终端使用。windows 电脑需要另行下载)
openssl
下面需要依次生成三个文件
首先是私钥
genrsa -out private.pem 2048
生成证书签名
req -new -key private.pem -out csr.pem
生成证书签名需要填写一些信息,依次按照提示填写就行。以下是我随便填写的信息:
Country Name (2 letter code) [AU]:CH
State or Province Name (full name) [Some-State]:CH
Locality Name (eg, city) []:CH
Organization Name (eg, company) [Internet Widgits Pty Ltd]:CH
Organizational Unit Name (eg, section) []:CH
Common Name (e.g. server FQDN or YOUR name) []:CH
Email Address []:CH
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:123123123
An optional company name []:CH
最后一步,生成证书文件
x509 -req -in csr.pem -signkey private.pem -out csr.crt
完成,此时certificate文件夹中就存在三个文件csr.crt,csr.pem,private.pem。将这三个文件,放入node项目的根目录。
现在要写node项目的启动js文件,index.js
let express = require(‘express’);
let app = express();
let fs = require(‘fs’);
let https = require(‘https’);
let http = require(‘http’);
let privateKey = fs.readFileSync(‘private.pem’, ‘utf8’);
let certificate = fs.readFileSync(‘csr.crt’, ‘utf8’);
let cert = {key: privateKey, cert: certificate};
let httpServer = http.createServer(app);
let httpsServer = https.createServer(cert, app);
app.get(‘/’, function(req, res) {
if(req.protocol === ‘https’) {
res.send(‘https require’);
} else {
res.send(‘http require’);
}
});
httpServer.listen(3000, function() {
console.log(‘HTTP Server is running’);
});
httpsServer.listen(3001, function() {
console.log(‘HTTPS Server is running’);
});
启动node 服务器
node index.js
此时控制台显示
HTTP Server is running
HTTPS Server is running
然后去浏览器输入 https://localhost:3001/
此时浏览器会拦截你访问,因为生成的证书是无效的。不过你可以按继续访问(safari浏览器)或者高级选项继续访问(chrome浏览器)。
参考了http://www.jianshu.com/p/9072dce6eb2e
Android 端 拷贝 csr.crt证书文件采用OKGO 访问HTTPS
##APP:
OkGo.getInstance().init(this);
try {
OkGo.getInstance().setCertificates(getAssets().open(“csr.crt”));
} catch (IOException e) {
e.printStackTrace();
}
act
public void gohttps(View view){
OkGo.get(“https://192.168.1.101:3001“).tag(this).execute(new StringCallback() {
@Override
public void onSuccess(String s, Call call, Response response) {
System.out.println(“——”+s);
}
});
}
- NOde JS Mac 下创建HTTPS 服务以及Android 端OKGO 访问HTTPS 小结
- Android访问https服务
- node.js搭建https服务
- node.js 创建 https 服务器
- node.js 创建 https 服务器
- Node.js构建HTTPS+服务集群
- node.js在本地启动https服务
- Android 端Https 访问
- Mac下firfox访问https限制问题
- mac下Apache服务配置Https
- Tomcat创建HTTPS访问,java访问https
- nodejs创建https服务
- NodeJs创建https 服务
- 使用Node 搭建HTTPS服务
- 用Node.js创建自签名的HTTPS服务器
- 用Node.js创建自签名的HTTPS服务器
- 用Node.js创建自签名的HTTPS服务器
- 用Node.js创建自签名的HTTPS服务器
- 4Sum
- [leetcode]17. Letter Combinations of a Phone Number(Java)
- android studio 控制台输出个性设置
- 深度学习-使用mxnet做线性回归问题
- Ueditor1.4.3.3+springMvc+maven 实现图片上传
- NOde JS Mac 下创建HTTPS 服务以及Android 端OKGO 访问HTTPS 小结
- const修饰变量、指针详解
- 关于webstorm的若干小问题
- jvm浅析
- CodeForces
- svm和LSSVM区别
- 1031. Hello World for U (20)
- 正则表达式学习
- 17_7_17:复杂链表的复制