佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > 詳解Golang實現(xiàn)http重定向https的方式

詳解Golang實現(xiàn)http重定向https的方式

熱門標(biāo)簽:地方門戶網(wǎng)站 服務(wù)外包 網(wǎng)站排名優(yōu)化 呼叫中心市場需求 AI電銷 鐵路電話系統(tǒng) 百度競價排名 Linux服務(wù)器

以前寫代碼時,都是直接將程序綁定到唯一端口提供http/https服務(wù),在外層通過反向代理(nginx/caddy)來實現(xiàn)http和https的切換。隨著上線后的服務(wù)越來越多,有一些服務(wù)無法直接通過反向代理來提供這種重定向,只能依靠代碼自己實現(xiàn)。所以簡要記錄一下如何在代碼中實現(xiàn)http到https的重定向。

分析

無論是反向代理還是代碼自己實現(xiàn),問題的本質(zhì)都是判斷請求是否是https請求。 如果是則直接處理,如果不是,則修改請求中的url地址,同時返回客戶端一個重定向狀態(tài)碼(301/302/303/307)。但如果仔細分析的話,會衍生出另外的問題,返回哪個重定向碼是合理的?

這個問題展開討論,估計要寫滿滿一大頁,可能還得不出結(jié)論。 因此這里就不糾結(jié)到底返回哪個了,我使用的是307.

實現(xiàn)

如何我們從問題出現(xiàn)的場景開始分析,基本可以得出一個結(jié)論: 在需要轉(zhuǎn)換的場景中,都是用戶習(xí)慣性的首先發(fā)出了http請求,然后服務(wù)器才需要返回一個https的重定向。 因此實現(xiàn)的第一步就是創(chuàng)建一個監(jiān)聽http請求的端口:

go http.ListenAndServe(":8000", http.HandlerFunc(redirect))

8000端口專門用來監(jiān)聽http請求,不能阻塞https主流程,因此單獨扔給一個協(xié)程來處理。 redirect用來實現(xiàn)重定向:

func redirect(w http.ResponseWriter, req *http.Request) { 
  _host := strings.Split(req.Host, ":")
  _host[1] = "8443"

  target := "https://" + strings.Join(_host, ":") + req.URL.Path
  if len(req.URL.RawQuery) > 0 {
    target += "?" + req.URL.RawQuery
  }

  http.Redirect(w, req, target, http.StatusTemporaryRedirect)
}

8443是https監(jiān)聽的端口。 如果監(jiān)聽默認端口443,那么就可加可不加。 最后調(diào)用sdk中的Redirect函數(shù)封裝Response。

處理完重定向之后,再處理https就變得很容易了:

router := mux.NewRouter() 
  router.Path("/").HandlerFunc(handleHttps)
  c := cors.New(cors.Options{
    AllowedOrigins:  []string{"*.devexp.cn"},
    AllowedMethods:  []string{"HEAD", "GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
    AllowedHeaders:  []string{"*"},
    AllowCredentials: true,
    Debug:      false,
    AllowOriginFunc: func(origin string) bool {
      return true
    },
  })

  handler := c.Handler(router)
  logrus.Fatal(http.ListenAndServeTLS(":8443", "cert.crt", "cert.key", handler))

完整代碼如下:

package main

import ( 
  "github.com/gorilla/mux"
  "github.com/rs/cors"
  "github.com/sirupsen/logrus"
  "net/http"
  "encoding/json"
  "log"
  "strings"
)

func main() { 
  go http.ListenAndServe(":8000", http.HandlerFunc(redirect))

  router := mux.NewRouter()
  router.Path("/").HandlerFunc(handleHttps)
  c := cors.New(cors.Options{
    AllowedOrigins:  []string{"*.devexp.cn"},
    AllowedMethods:  []string{"HEAD", "GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
    AllowedHeaders:  []string{"*"},
    AllowCredentials: true,
    Debug:      false,
    AllowOriginFunc: func(origin string) bool {
      return true
    },
  })

  handler := c.Handler(router)
  logrus.Fatal(http.ListenAndServeTLS(":8443", "cert.crt", "cert.key", handler))
}

func redirect(w http.ResponseWriter, req *http.Request) { 
  _host := strings.Split(req.Host, ":")
  _host[1] = "8443"

  // remove/add not default ports from req.Host
  target := "https://" + strings.Join(_host, ":") + req.URL.Path
  if len(req.URL.RawQuery) > 0 {
    target += "?" + req.URL.RawQuery
  }
  log.Printf("redirect to: %s", target)
  http.Redirect(w, req, target,
    // see @andreiavrammsd comment: often 307 > 301
    http.StatusTemporaryRedirect)
}

func handleHttps(w http.ResponseWriter, r *http.Request) { 
  json.NewEncoder(w).Encode(struct {
    Name string
    Age  int
    Https bool
  }{
    "lala",
    11,
    true,
  })
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • golang flag簡單用法
  • Golang中定時器的陷阱詳解
  • 在golang中操作mysql數(shù)據(jù)庫的實現(xiàn)代碼
  • golang設(shè)置http response響應(yīng)頭與填坑記錄
  • Golang學(xué)習(xí)之平滑重啟
  • Golang編譯器介紹

標(biāo)簽:黃山 湘潭 衡水 崇左 仙桃 銅川 蘭州 湖南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《詳解Golang實現(xiàn)http重定向https的方式》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    宽城| 南郑县| 邮箱| 绵阳市| 仙游县| 临洮县| 左权县| 手游| 灵山县| 潮安县| 龙里县| 南乐县| 郎溪县| 石屏县| 章丘市| 九台市| 东港市| 民勤县| 桓台县| 镇巴县| 车致| 罗定市| 南漳县| 诸城市| 凤翔县| 涟水县| 舞阳县| 库车县| 万全县| 温州市| 盐津县| 环江| 富顺县| 呼和浩特市| 隆安县| 和平区| 吉首市| 佛学| 连南| 白银市| 阳曲县|