gRPC 認(rèn)證 和 同時提供http服務(wù)

gogrpc源碼中grpc.ServeHTTP可以同時提供https,但需要TLS認(rèn)證

// ServeHTTP implements the Go standard library's http.Handler
// interface by responding to the gRPC request r, by looking up
// the requested gRPC method in the gRPC server s.
//
// The provided HTTP request must have arrived on an HTTP/2
// connection. When using the Go standard library's server,
// practically this means that the Request must also have arrived
// over TLS.
//
// To share one port (such as 443 for https) between gRPC and an
// existing http.Handler, use a root http.Handler such as:
//
//   if r.ProtoMajor == 2 && strings.HasPrefix(
//      r.Header.Get("Content-Type"), "application/grpc") {
//      grpcServer.ServeHTTP(w, r)
//   } else {
//      yourMux.ServeHTTP(w, r)
//   }
//
// Note that ServeHTTP uses Go's HTTP/2 server implementation which is totally
// separate from grpc-go's HTTP/2 server. Performance and features may vary
// between the two paths. ServeHTTP does not support some gRPC features
// available through grpc-go's HTTP/2 server.
//

好吧溪北,接著首先用openssl創(chuàng)建EC參數(shù)和私鑰文件

 #客戶端
 openssl genrsa -out ca.key 2048
 openssl req -x509 -new -nodes -key ca.key -subj "/CN=tim" -days 5000 -out ca.crt

  #服務(wù)端
  openssl genrsa -out server.key 2048   
  openssl req -new -key server.key -subj "/CN=tim" -out server.csr
  openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 5000
openssl .png

這里需要記錄一下Common Name輸入的內(nèi)容top

ls.png

#server 端
certFile: ./server.crt
keyFile: ./server.key
#client端
ca.crt

開始修改代碼,服務(wù)端

//創(chuàng)建tls認(rèn)證。YamlConfig.CertFile指生成的server.pem路徑市框,YamlConfig.KeyFile指生成的server.key路徑
cred, err := credentials.NewServerTLSFromFile(YamlConfig.CertFile, YamlConfig.KeyFile)
    if err != nil {
        log.Fatalln(err)
    }

    grpcServer := grpc.NewServer(
        grpc.Creds(cred), //這里加載了tls認(rèn)證
        grpc.UnaryInterceptor(grpcMetrics.UnaryServerInterceptor()),
    )
      ··········
    log.Println("Listen ",YamlConfig.Port)
    //http也加載tls
    if err = http.ListenAndServeTLS(
        fmt.Sprintf("%s", YamlConfig.Port),
        YamlConfig.CertFile, 
        YamlConfig.KeyFile, 
        http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            if r.ProtoMajor == 2 && strings.HasPrefix(
                r.Header.Get("Content-Type"), "application/grpc") {
                grpcServer.ServeHTTP(w, r)
            } else {
                mux.ServeHTTP(w, r)
            }

            return
        }),
    );err != nil{
        log.Fatalln(err)
    }

然后是客戶端

//CertFile指server.pem路徑
cred,err := credentials.NewClientTLSFromFile(CertFile,"top")
    if err != nil {
        log.Fatalln("Failed to create TLS credentials %v", err)
    }
conn, err := grpc.DialContext(ctx, v.(string),
            grpc.WithBlock(),
            grpc.WithTransportCredentials(cred),//使用tls
            grpc.WithKeepaliveParams(keepalive.ClientParameters{
                Time:                10 * time.Second,
                Timeout:             100 * time.Millisecond,
                PermitWithoutStream: true}),
        )

瀏覽器訪問https://xxx 沒問題

grpc&&https.png

grpc_client客戶端調(diào)grpc_server沒問題


grpc_client->grpc_server
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末疗隶,一起剝皮案震驚了整個濱河市佑笋,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌斑鼻,老刑警劉巖蒋纬,帶你破解...
    沈念sama閱讀 219,539評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異坚弱,居然都是意外死亡蜀备,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評論 3 396
  • 文/潘曉璐 我一進(jìn)店門荒叶,熙熙樓的掌柜王于貴愁眉苦臉地迎上來碾阁,“玉大人,你說我怎么就攤上這事些楣≈祝” “怎么了?”我有些...
    開封第一講書人閱讀 165,871評論 0 356
  • 文/不壞的土叔 我叫張陵愁茁,是天一觀的道長蚕钦。 經(jīng)常有香客問我,道長鹅很,這世上最難降的妖魔是什么冠桃? 我笑而不...
    開封第一講書人閱讀 58,963評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮道宅,結(jié)果婚禮上食听,老公的妹妹穿的比我還像新娘胸蛛。我一直安慰自己,他們只是感情好樱报,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,984評論 6 393
  • 文/花漫 我一把揭開白布葬项。 她就那樣靜靜地躺著,像睡著了一般迹蛤。 火紅的嫁衣襯著肌膚如雪民珍。 梳的紋絲不亂的頭發(fā)上捞附,一...
    開封第一講書人閱讀 51,763評論 1 307
  • 那天云矫,我揣著相機(jī)與錄音苟蹈,去河邊找鬼香浩。 笑死,一個胖子當(dāng)著我的面吹牛得运,可吹牛的內(nèi)容都是我干的储藐。 我是一名探鬼主播赊时,決...
    沈念sama閱讀 40,468評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼宣渗,長吁一口氣:“原來是場噩夢啊……” “哼抖所!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起痕囱,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤田轧,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后鞍恢,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體傻粘,經(jīng)...
    沈念sama閱讀 45,850評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,002評論 3 338
  • 正文 我和宋清朗相戀三年帮掉,在試婚紗的時候發(fā)現(xiàn)自己被綠了抹腿。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,144評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡旭寿,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出崇败,到底是詐尸還是另有隱情盅称,我是刑警寧澤,帶...
    沈念sama閱讀 35,823評論 5 346
  • 正文 年R本政府宣布后室,位于F島的核電站缩膝,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏岸霹。R本人自食惡果不足惜疾层,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,483評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望贡避。 院中可真熱鬧痛黎,春花似錦予弧、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,026評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至井厌,卻和暖如春蚓庭,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背仅仆。 一陣腳步聲響...
    開封第一講書人閱讀 33,150評論 1 272
  • 我被黑心中介騙來泰國打工器赞, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人墓拜。 一個月前我還...
    沈念sama閱讀 48,415評論 3 373
  • 正文 我出身青樓港柜,卻偏偏與公主長得像,于是被迫代替她去往敵國和親撮弧。 傳聞我的和親對象是個殘疾皇子潘懊,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,092評論 2 355

推薦閱讀更多精彩內(nèi)容