起因:

在使用facebook三方登陆时,有效 OAuth 跳转 URI这一项目前看来只支持https访问,本地测试就要实现https

mkcert 简介
mkcert 是一个使用go语言编写的生成本地自签证书的小工具,具有跨平台,使用简单,支持多域名,自动信任CA等一系列方便的特性,可供本地开发时快速创建 https 环境使用。
github介绍:https://github.com/FiloSottil...

macOS
On macOS, use Homebrew

brew install mkcert
brew install nss # if you use Firefox
$ mkcert -install
Created a new local CA 💥
The local CA is now installed in the system trust store! ⚡️
The local CA is now installed in the Firefox trust store (requires browser restart)! 🦊

$ mkcert example.com "*.example.com" example.test localhost 127.0.0.1 ::1

Created a new certificate valid for the following names 📜
 - "example.com"
 - "*.example.com"
 - "example.test"
 - "localhost"
 - "127.0.0.1"
 - "::1"

The certificate is at "./example.com+5.pem" and the key at "./example.com+5-key.pem" ✅

代码开发:加一个中间件

package main

import (
    "github.com/gin-gonic/gin"
    "github.com/unrolled/secure"
)

func main() {
    router := gin.Default()
    router.Use(LoadTls())
    // 开启端口监听
    router.RunTLS(":8080", "localhost.pem", "localhost-key.pem")
}

func LoadTls() gin.HandlerFunc {
    return func(c *gin.Context) {
        middleware := secure.New(secure.Options{
            SSLRedirect: true,
            SSLHost:     "localhost:8000",
        })
        err := middleware.Process(c.Writer, c.Request)
        if err != nil {
            //如果出现错误,请不要继续。
            fmt.Println(err)
            return
        }
        // 继续往下处理
        c.Next()
    }
}

net/http实现:

log.Fatal(http.ListenAndServeTLS(":8000", "conf/localhost.pem", "conf/localhost-key.pem", nil))

zwalker
1 声望0 粉丝