HTTPS-自己生成数字证书
一、获取证书的途径
- 自签名证书,适用于开发者测试HTTPS,最快速的途径就是生成自签名证书,非常方便。
- Let's Encrypt证书,可以使用免费CA机构签发的证书。
- 使用收费CA机构签发的证书,如果对证书安全性、兼容性、功能有特殊需求,可以向CA机构申请证书。
二、自签名证书
自签名证书是我们自己签发的,浏览器不会集成私有的CA机构的根证书,所以打开页面的时候会进行提示,用户选择信任证书之后,后续的通信就会进行加密保护的。
自签名证书的用途还是很广泛的,对于一些企业内部系统,由于购买证书需要成本,可以生成自签名证书,企业内部系统的用户一般运行在同一个局域网下,由防火墙保护,风险相对可控,当浏览器提示用户自签名证书存在风险时,用户可以选择信任自签名证书,等同于访问了一个HTTPS网站。
生成自签名证书的步骤如下
1.生成私钥对和CSR
我们设置密钥的长度为2048bit;
我们最终会得到flask_self_csr.pem和flask_self_key.pem两个文件;
CSR(Certificate Signing Request)表示证书签名请求,里面包含了服务器的密钥对,CA机构接收到请求会验证CSR请求的签名;
flask_self_csr.pem包含了我们的密钥对;
执行命令之后,我们可以在交互式提示中,设置证书包含的一些信息;
mango@mango-ubuntu:~/文档/blogs/web/证书$ openssl req -newkey rsa:2048 -nodes -keyout flask_self_key.pem -out flask_self_csr.pem
Generating a RSA private key
.........+++++
......+++++
writing new private key to 'flask_self_key.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:Beijing
Locality Name (eg, city) []:Beijing
Organization Name (eg, company) [Internet Widgits Pty Ltd]:mango
Organizational Unit Name (eg, section) []:mango
Common Name (e.g. server FQDN or YOUR name) []:cee1-110-251-30-176.ngrok.io
Email Address []:mango@163.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
- 生成自签名证书
接下来通过CSR生成证书,对于自签名证书,我们可以认为自己就是一个CA机构,输入如下命令生成证书:
mango@mango-ubuntu:~/文档/blogs/web/证书$ openssl x509 -signkey flask_self_key.pem -in flask_self_csr.pem -req -days 365 -out flask_self_cert.pem
Signature ok
subject=C = CN, ST = Beijing, L = Beijing, O = mango, OU = mango, CN = cee1-110-251-30-176.ngrok.io, emailAddress = mango@163.com
Getting Private key
- 验证证书
将生成的flask_self_cert.pem和flask_self_key.pem拷贝到站点根目录下,并设置启用ssl
from flask import Flask
app = Flask(__name__)
@app.route("/", methods=["GET"])
def hello():
return 'hello python'
if __name__ == "__main__":
app.run('0.0.0.0', ssl_context=('flask_self_cert.pem', 'flask_self_key.pem'))
# app.run('0.0.0.0', debug=True, ssl_context='adhoc')
三、使用Let's Encrypt证书
Let's Encrypt首先是一个CA机构,得到了很多大公司的支持,兼容性非常不错,同时它定义了ACME协议,将管理证书的流程进行了标准化、自动化,不用人工管理。可以使用基于ACME协议的客户端在Let's Encrypt管理证书,官方推荐Certbot客户端,使用非常方便。
1.安装Certbot客户端
mango@mango-ubuntu:~/文档/blogs/web/证书/certbot$ sudo snap install --classic certbot
certbot 1.21.0 from Certbot Project (certbot-eff✓) installed
2.手动生成证书和密钥文件
mango@mango-ubuntu:~/文档/blogs/web/证书$ sudo certbot certonly --manual -d 565c-110-251-30-176.ngrok.io
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for 565c-110-251-30-176.ngrok.io
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Create a file containing just this data:
csM3J5YGt3V-PQDeRpcDhjlpy7Hdf9tjh-NsIqqoA6A.eRfiNKPaGpDq-g1FefRl52GbfFeSDV_Qg8Gwe1KQP5M
And make it available on your web server at this URL:
http://565c-110-251-30-176.ngrok.io/.well-known/acme-challenge/csM3J5YGt3V-PQDeRpcDhjlpy7Hdf9tjh-NsIqqoA6A
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/565c-110-251-30-176.ngrok.io/fullchain.pem
Key is saved at: /etc/letsencrypt/live/565c-110-251-30-176.ngrok.io/privkey.pem
This certificate expires on 2022-02-10.
These files will be updated when the certificate renews.
NEXT STEPS:
- This certificate will not be renewed automatically. Autorenewal of --manual certificates requires the use of an authentication hook script (--manual-auth-hook) but one was not provided. To renew this certificate, repeat this same certbot command before the certificate's expiry date.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
* Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
* Donating to EFF: https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
为了通过CA对站点的验证,我们需要新增对应的action来响应对应的请求
@app.route("/.well-known/acme-challenge/csM3J5YGt3V-PQDeRpcDhjlpy7Hdf9tjh-NsIqqoA6A")
def challenge():
return 'csM3J5YGt3V-PQDeRpcDhjlpy7Hdf9tjh-NsIqqoA6A.eRfiNKPaGpDq-g1FefRl52GbfFeSDV_Qg8Gwe1KQP5M'
- 验证证书
将生成的证书和密钥文件拷贝到站点根目录,并修改文件权限
mango@mango-ubuntu:~/文档/blogs/webhook$ sudo cp /etc/letsencrypt/live/565c-110-251-30-176.ngrok.io/fullchain.pem fullchain.pem
mango@mango-ubuntu:~/文档/blogs/webhook$ sudo cp /etc/letsencrypt/live/565c-110-251-30-176.ngrok.io/privkey.pem
mango@mango-ubuntu:~/文档/blogs/webhook$ sudo chown mango fullchain.pem
mango@mango-ubuntu:~/文档/blogs/webhook$ sudo chown mango privkey.pem
修改站点启用ssl
from flask import Flask
app = Flask(__name__)
@app.route("/", methods=["GET"])
def hello():
return 'hello python'
@app.route("/.well-known/acme-challenge/csM3J5YGt3V-PQDeRpcDhjlpy7Hdf9tjh-NsIqqoA6A")
def challenge():
return 'csM3J5YGt3V-PQDeRpcDhjlpy7Hdf9tjh-NsIqqoA6A.eRfiNKPaGpDq-g1FefRl52GbfFeSDV_Qg8Gwe1KQP5M'
if __name__ == "__main__":
app.run('0.0.0.0', ssl_context=('fullchain.pem', 'privkey.pem'))
# app.run('0.0.0.0', ssl_context=('flask_self_cert.pem', 'flask_self_key.pem'))
# app.run('0.0.0.0', debug=True, ssl_context='adhoc')
HTTPS-自己生成数字证书的更多相关文章
- 公钥与私钥对HTTPS的理解(数字证书的需要)
本文转自某大牛链接 文中首先解释了加密解密的一些基础知识和概念,然后通过一个加密通信过程的例子说明了加密算法的作用,以及数字证书的出现所起的作用.接着对数字证书做一个详细的解释,并讨论一下window ...
- JDK 生成数字证书
JDK(keytool.exe)生成数字证书 2010-11-21 15:52 QUOTE: keytool JAVA是个密钥和证书管理工具.它使用户能够管理自己的公钥/私钥对及相关证书,用于(通过数 ...
- C#检测并安装https站点的数字证书,CefSharp和HttpWebRequest通过会话Cookie实现自动登录访问https站点
HttpUtil工具类: using System; using System.Collections.Generic; using System.IO; using System.Linq; usi ...
- 利用keytool工具生成数字证书
一.制作数字证书 因测试微信小程序, 腾讯要求使用 https协议,所以需要使用证书.使用jdk工具制作数字证书流程如下: 1.查看JDK是否安装,使用命令java -version 2.切换目录至 ...
- JAVA JDK keytool 生成数字证书
简介: 数字证书作为网络安全数据传输的凭证,web在传输时客户端(浏览器)和 服务端(服务器)先进行会话握手,在握手过程中服务端会验证客户端的是否已经在服务端做了认证,这是单向认证.如果是双向认证的话 ...
- https SSL主流数字证书都有哪些格式?
主流数字证书都有哪些格式? 一般来说,主流的Web服务软件,通常都基于两种基础密码库:OpenSSL和Java. Tomcat.Weblogic.JBoss等,使用Java提供的密码库.通过Java的 ...
- https SSL主流数字证书都有哪些格式(转载)
主流数字证书都有哪些格式? 一般来说,主流的Web服务软件,通常都基于两种基础密码库:OpenSSL和Java. Tomcat.Weblogic.JBoss等,使用Java提供的密码库.通过Java的 ...
- 【传输协议】https SSL主流数字证书都有哪些格式?
一般来说,主流的Web服务软件,通常都基于两种基础密码库:OpenSSL和Java. Tomcat.Weblogic.JBoss等,使用Java提供的密码库.通过Java的Keytool工具,生成Ja ...
- 如何用谷歌浏览器导出一个https网站的数字证书
HTTPS加密是互联网安全建设的基础,百度.淘宝.天猫等越来越多互联网巨头启用全站HTTPS,也带动了更多网站加入HTTPS加密的行列.普通用户也逐渐明白HTTPS比HTTP更安全,访问网银.购物等重 ...
随机推荐
- Maven----将手动下载的jar包以命令行的方式安装到本地MavenRepository中
1.情景再现:准备实现SprintBoot的热部署功能,因没有对应jar时,在Eclipse中mvn install 会报错: 报错信息: [INFO] --- spring-boot-maven-p ...
- FastAPI(58)- 使用 OAuth2PasswordBearer 的简单栗子
背景 假设在某个域中拥有后端 API(127.0.0.1:8080) 并且在另一个域或同一域的不同路径(或移动应用程序)中有一个前端(127.0.0.1:8081) 并且希望有一种方法让前端使用用户名 ...
- 数值计算:四阶龙格-库塔法 for 二阶微分方程
引言 考虑存在以下二阶偏微分方程 \[\begin{align} f_2 \cdot \ddot{X(t)}+f_1 \cdot \dot{X(t)} +f_0 \cdot {X(t)} =F(t) ...
- nsq topic
与Topic相关的代码主要位于nsqd/topic.go中. 上一篇文字我们讲解了下nsq的启动流程.对nsq的整体框架有了一个大概的了解.本篇文章就是由大到小.对于topic这一部分进行详尽的讲解. ...
- 算法——快速排序迭代式和递归式的Java实现
快速排序迭代式和递归式的Java实现 快速排序基于分治法的思想,在待排序表中任选一值作为中枢值 pivot,一趟快排将所有大于该值的元素置于一边,小于该值的元素置于另一边,这样一个元素在排序中的最终位 ...
- Spring Bean装配笔记
Spring Bean装配笔记 Spring中的Bean是一个很重要的概念.Spring作为一个Bean容器,它可以管理对象和对象之间的依赖关系,我们不需要自己建立对象,把这部分工作全部转交给容器完成 ...
- Spring Security OAuth2 微服务认证中心自定义授权模式扩展以及常见登录认证场景下的应用实战
一. 前言 [APP 移动端]Spring Security OAuth2 手机短信验证码模式 [微信小程序]Spring Security OAuth2 微信授权模式 [管理系统]Spring Se ...
- 力扣 - 剑指 Offer 17. 打印从1到最大的n位数
题目 剑指 Offer 17. 打印从1到最大的n位数 思路1 如果有n位,那么最大值就是\(10^n-1\),即如果n是2,那么最大就到输出到99 考虑到大数情况,所以使用字符数组 还要把字符数组转 ...
- selenium去特征
code from selenium import webdriver chrome_options = webdriver.ChromeOptions() chrome_options.add_ex ...
- QG-2019-AAAI-Improving Neural Question Generation using Answer Separation
Improving Neural Question Generation using Answer Separation 本篇是2019年发表在AAAI上的一篇文章.该文章在基础的seq2seq模型的 ...