新聞中心
HTTPS服務
建立?HTTPS?服務非常簡單,使用框架?WebServer?提供的??EnableHTTPS?(certFile, keyFile string) error?方法即可。很顯然,該方法中需要提供兩個參數(shù),即兩個用于?HTTPS?非對稱加密的證書文件以及對應的秘鑰文件。

準備工作
在本地演示的需要,我們可以使用?openssl?命令生成本地用于測試的證書和對應的秘鑰文件。命令如下:
- 使用常用的?
RSA?算法生成秘鑰文件
openssl genrsa -out server.key 2048
- 此外,我們也可以使用?
ECDSA?算法來生成秘鑰文件:
openssl ecparam -genkey -name secp384r1 -out server.key
- 根據(jù)秘鑰文件生成證書文件
openssl req -new -x509 -key server.key -out server.crt -days 365
- (可選)根據(jù)秘鑰生成公鑰文件,該文件用于客戶端與服務端通信
openssl rsa -in server.key -out server.key.public
?openssl?支持的算法以及命令參數(shù)比較多,如果想要深入了解請使用?man openssl?命令進行查看。本次示例中,本地環(huán)境(?Ubuntu?)使用命令生成相關秘鑰、公鑰、證書文件的流程如下:
$ openssl genrsa -out server.key 2048
Generating RSA private key, 2048 bit long modulus
.........................+++
.....................................................................+++
unable to write 'random state'
e is 65537 (0x10001)
$ openssl req -new -x509 -key server.key -out server.crt -days 365
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]:CH
State or Province Name (full name) [Some-State]:SiChuan
Locality Name (eg, city) []:Chengdu
Organization Name (eg, company) [Internet Widgits Pty Ltd]:John.cn
Organizational Unit Name (eg, section) []:Dev
Common Name (e.g. server FQDN or YOUR name) []:John
Email Address []:john@johng.cn
$ openssl rsa -in server.key -out server.key.public
writing RSA key
$ ll
total 20
drwxrwxr-x 2 john john 4096 Apr 23 21:26 ./
drwxr-xr-x 90 john john 4096 Apr 23 20:55 ../
-rw-rw-r-- 1 john john 1383 Apr 23 21:26 server.crt
-rw-rw-r-- 1 john john 1675 Apr 23 21:25 server.key
-rw-rw-r-- 1 john john 1675 Apr 23 21:26 server.key.public
其中,生成證書的命令提示需要錄入一些信息,可以直接回車留空即可,我們這里隨便填寫了一些。
示例代碼
根據(jù)以上生成的秘鑰和證書文件,我們來演示如果使用?ghttp.Server?實現(xiàn)一個?HTTPS?服務。示例代碼如下:
package main
import (
"github.com/GOgf/gf/v2/net/ghttp"
)
func main() {
s := ghttp.GetServer()
s.BindHandler("/", func(r *ghttp.Request){
r.Response.Writeln("來自于HTTPS的:哈嘍世界!")
})
s.EnableHTTPS("/home/john/https/server.crt", "/home/john/https/server.key")
s.SetPort(8199)
s.Run()
}
可以看到,我們直接將之前生成的證書和秘鑰文件地址傳遞給?EnableHTTPS?即可,通過?s.SetPort(8199)?設置?HTTPS?的服務端口,當然我們也可以通過?s.SetHTTPSPort(8199)?來實現(xiàn),在單一服務下兩者沒有區(qū)別,當?WebServer?需要同時支持?HTTP?和?HTTPS?服務的時候,兩者的作用就不同了,這個特性我們會在后面介紹。隨后我們訪問頁面 ?https://127.0.0.1:8199/? 來看一下效果:
可以看到瀏覽器有提示信息,主要是因為我們生成的證書為私有的,非第三方授信企業(yè)提供的。瀏覽器大多會自帶一些第三方授信的?HTTPS?證書機構(gòu),這些機構(gòu)提供的?HTTPS?證書被瀏覽器認為是權(quán)威的、可信的,才不會出現(xiàn)該提示信息。一般這種第三方權(quán)威機構(gòu)授信證書價格在每年幾千到幾萬人民幣不等,感興趣的朋友可在搜索引擎上了解下。
我們這里直接點擊?Advanced?,然后點擊?Proceed to 127.0.0.1 (unsafe)?,最終可以看到頁面輸出預期的結(jié)果:
HTTPS與HTTP支持
我們經(jīng)常會遇到需要通過?HTTP?和?HTTPS?來提供同一個服務的情況,即除了端口和訪問協(xié)議不一樣,其他都是相同的。如果按照傳統(tǒng)的使用多?WebServer?的方式來運行的話會比較繁瑣,為輕松地解決開發(fā)者的煩惱,?ghttp?提供了非常方便的特性:支持 “同一個”?WebServer?同時支持?HTTPS?及?HTTP?訪問協(xié)議。我們先來看一個例子:
package main
import (
"github.com/gogf/gf/v2/net/ghttp"
)
func main() {
s := ghttp.GetServer()
s.BindHandler("/", func(r *ghttp.Request){
r.Response.Writeln("您可以同時通過HTTP和HTTPS方式看到該內(nèi)容!")
})
s.EnableHTTPS("/home/john/https/server.crt", "/home/john/https/server.key")
s.SetHTTPSPort(443)
s.SetPort(80)
s.Run()
}
執(zhí)行后,通過本地瀏覽器訪問這兩個地址 http://127.0.0.1/ 和 https://127.0.0.1/ 都會看到同樣的內(nèi)容(需要注意的是,由于部分系統(tǒng)對于權(quán)限的限制,?WebServer?綁定?80?和?443?端口需要?root/?管理員權(quán)限,如果啟動報錯,可以更改端口號后重新執(zhí)行即可)。
在本示例中,我們使用了兩個方法來開啟?HTTPS?特性:
func (s *Server) EnableHTTPS(certFile, keyFile string) error
func (s *Server) SetHTTPSPort(port ...int) error
一個是添加證書及密鑰文件,一個是設置?HTTPS?協(xié)議的監(jiān)聽端口,一旦這兩個屬性被設置了,那么?WebServer?就會啟用?HTTPS?特性。并且,在示例中也通過?SetPort?方法來設置了?HTTP?服務的監(jiān)聽端口,因此該?WebServer?將會同時監(jiān)聽指定的?HTTPS?和?HTTP?服務端口。
使用Let’s Encrypt免費證書
?SSL?免費證書機構(gòu)比較多,如:
- ?
騰訊云DV SSL 證書? : https://cloud.tencent.com/product/ssl - ?
Let’s Encrypt? : https://letsencrypt.org/ - ?
CloudFlare SSL? : https://www.cloudflare.com/ - ?
StartSSL? : https://www.startcomca.com/ - ?
Wosign沃通SSL? : https://www.wosign.com/ - ?
loovit.net AlphaSSL? : https://www.lowendtalk.com/entry/register?Target=discussion%2Fcomment%2F2306096
以下以?Let's Encrypt?為例,介紹如何申請、使用、續(xù)期免費證書。
?Let’s Encrypt?官網(wǎng)地址:https://letsencrypt.org/
以下以?Ubuntu?系統(tǒng)為例,如何申請?Let's Encrypt?免費證書及在?gf?框架下對證書的使用。
安裝Certbot
?Certbot?官網(wǎng)地址:?https://certbot.eff.org/ ?
申請?Let’s Encrypt?免費證書需要使用到?certbot?工具:
sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install certbot
申請證書
使用以下命令:
certbot certonly --standalone -d 申請域名 --staple-ocsp -m 郵箱地址 --agree-tos
例如:
root@ip-172-31-41-204:~# certbot certonly --standalone -d GoFrame.org --staple-ocsp -m john@goframe.org --agree-tos
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator standalone, Installer None
Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for goframe.org
Waiting for verification...
Cleaning up challenges
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/goframe.org/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/goframe.org/privkey.pem
Your cert will expire on 2019-01-25. To obtain a new or tweaked
version of this certificate in the future, simply run certbot
again. To non-interactively renew *all* of your certificates, run
"certbot renew"
- 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
默認情況下,證書會被安裝到?/etc/letsencrypt/?,證書和私鑰文件分別為:
/etc/letsencrypt/live/goframe.org/fullchain.pem
/etc/letsencrypt/live/goframe.org/privkey.pem
使用證書
package main
import (
"github.com/gogf/gf/v2/net/ghttp"
)
func main() {
s := ghttp.GetServer()
s.BindHandler("/", func(r *ghttp.Request){
r.Response.Writeln("來自于HTTPS的:哈嘍世界!")
})
s.EnableHTTPS("/etc/letsencrypt/live/goframe.org/fullchain.pem", "/etc/letsencrypt/live/goframe.org/privkey.pem")
s.Run()
}
證書續(xù)期
證書默認有效期為3個月,到期后需要手動續(xù)期,使用以下命令:
certbot renew
示例1,我們可以使用?crontab?定時任務來實現(xiàn)自動續(xù)期:
# 每天嘗試續(xù)期一次,成功后重啟`gf`框架運行的WebServer
0 3 * * * certbot renew --quiet --renew-hook "kill -SIGUSR1 $(pidof 進程名稱)"
示例2,如果我們通過?nginx?管理證書,那么我們可以這樣來設置定時任務:
# 每天嘗試續(xù)期一次,證書續(xù)期需要先關閉80端口的WebServer監(jiān)聽
0 3 * * * service nginx stop && certbot renew --quiet --renew-hook "service nginx start"
為了防止?certbot renew?命令可能的失敗導致?nginx?無法重新啟動,為保證穩(wěn)定性,可以這樣:
# 每天嘗試續(xù)期一次,證書續(xù)期需要先關閉80端口的WebServer監(jiān)聽
0 3 * * * service nginx stop && certbot renew --quiet --renew-hook "service nginx start"
5 3 * * * service nginx start
網(wǎng)頁題目:創(chuàng)新互聯(lián)GoFrame教程:GoFrame高級特性-HTTPS&TLS
當前鏈接:http://www.dlmjj.cn/article/djddddi.html


咨詢
建站咨詢
