proxy 不支持token加驗(yàn)證,只能再封裝了一層代理,進(jìn)行加驗(yàn)證。
為柳林等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及柳林網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、柳林網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
kubectl proxy --port=8089 --address=127.0.0.1 --accept-hosts='^*$' # 后面這個(gè)可以去掉,不用允許所有
mkdir go-proxy
cd go-proxy
go mod init go-proxy
vim main.go
package main
import (
"bytes"
"fmt"
"github.com/ouqiang/goproxy"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"strings"
"time"
)
type EventHandler struct{}
func (e *EventHandler) Connect(ctx *goproxy.Context, rw http.ResponseWriter) {
}
func (e *EventHandler) Auth(ctx *goproxy.Context, rw http.ResponseWriter) {
}
func (e *EventHandler) BeforeRequest(ctx *goproxy.Context) {
if clientIP, _, err := net.SplitHostPort(ctx.Req.RemoteAddr); err == nil {
if prior, ok := ctx.Req.Header["X-Forwarded-For"]; ok {
clientIP = strings.Join(prior, ", ") + ", " + clientIP
}
ctx.Req.Header.Set("X-Forwarded-For", clientIP)
}
// 讀取Body
body, err := ioutil.ReadAll(ctx.Req.Body)
if err != nil {
// 錯(cuò)誤處理
return
}
if ctx.Req.Header.Get("token") != "1234" {
fmt.Println("沒有權(quán)限,禁止登錄")
ctx.Abort()
return
}
// Request.Body只能讀取一次, 讀取后必須再放回去
// Response.Body同理
ctx.Req.Body = ioutil.NopCloser(bytes.NewReader(body))
}
func (e *EventHandler) BeforeResponse(ctx *goproxy.Context, resp *http.Response, err error) {
if err != nil {
return
}
// 修改response
}
// 設(shè)置上級(jí)代理
func (e *EventHandler) ParentProxy(req *http.Request) (*url.URL, error) {
return url.Parse("http://127.0.0.1:8089")
}
func (e *EventHandler) Finish(ctx *goproxy.Context) {
log.Printf("請求結(jié)束 URL:%s\n", ctx.Req.URL)
}
// 記錄錯(cuò)誤日志
func (e *EventHandler) ErrorLog(err error) {
log.Println(err)
}
func main() {
proxy := goproxy.New(goproxy.WithDelegate(&EventHandler{}))
server := &http.Server{
Addr: ":8000",
Handler: proxy,
ReadTimeout: 1 * time.Minute,
WriteTimeout: 1 * time.Minute,
}
err := server.ListenAndServe()
if err != nil {
panic(err)
}
}
go run main.go
主要修改的地方有2個(gè)
if ctx.Req.Header.Get("token") != "1234" { 這里是輸入你的token密碼
return url.Parse("http://127.0.0.1:8089") 這里是你本地的 proxy 接口
請求頭 添加 token : 1234
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
分享名稱:kubectlproxy加token驗(yàn)證-創(chuàng)新互聯(lián)
網(wǎng)頁地址:http://aaarwkj.com/article34/pjppe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、用戶體驗(yàn)、網(wǎng)站策劃、品牌網(wǎng)站制作、建站公司、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容