好吧,讓我們說實話。安全性是一件大事,如果您正在建立 API,您不能讓任何人闖入並開始弄亂您的資料。這就是 JWT(JSON Web 令牌) 發揮作用的地方。今天,我們透過新增基於 JWT 的身份驗證來升級 Go API。
快速注意?
如果您一直在使用舊的 github.com/dgrijalva/jwt-go 軟體包,那麼是時候升級了。新標準是 github.com/golang-jwt/jwt/v4。
為什麼要切換?
- 原作者移交了控制權,新的維護者一直忙於改進和修復安全問題。
- 從 4.0.0 版本開始,他們添加了 Go 模組支援並改進了令牌驗證。
- 如果您仍在使用舊軟體包,請查看他們的 MIGRATION_GUIDE.md。
現在,讓我們開始使用我們精美的新 JWT 庫!
JWT 又是什麼? ?
對 JWT 新手:
- 它就像一個用於存取您的 API 的簽署授權單。
- API 產生一個令牌,對其進行簽名,然後客戶端(使用者、應用程式等)在每個請求中包含該令牌。
- 伺服器檢查令牌並說:「是的,你是合法的。」
現在您已經熟悉了,讓我們深入研究程式碼!
設定項目
我們將從上一篇文章離開的地方繼續。讓我們更新 Go 模組並安裝必要的軟體包:
- 新增 JWT 套件和 mux 路由器:
go get github.com/golang-jwt/jwt/v4 go get github.com/gorilla/mux
- 打開你的 main.go 文件,讓我們開始編碼!
第 1 步:產生 JWT 令牌
首先,我們將建立一個函數,在使用者登入時產生 JWT 令牌。該令牌將包含使用者名,並將使用金鑰進行簽署。
var jwtKey = []byte("my_secret_key") type Credentials struct { Username string `json:"username"` Password string `json:"password"` } type Claims struct { Username string `json:"username"` jwt.RegisteredClaims } func generateToken(username string) (string, error) { expirationTime := time.Now().Add(5 * time.Minute) claims := &Claims{ Username: username, RegisteredClaims: jwt.RegisteredClaims{ ExpiresAt: jwt.NewNumericDate(expirationTime), }, } token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) tokenString, err := token.SignedString(jwtKey) return tokenString, err }
此函數產生一個令牌,該令牌將在 5 分鐘後過期,並使用 HS256 演算法進行簽署。
第 2 步:建立登入端點
接下來,我們將建立一個登入端點,使用者可以在其中發送憑證。如果登入資訊檢查通過,我們將產生一個 JWT 並將其透過 cookie 發回。
func login(w http.ResponseWriter, r *http.Request) { var creds Credentials err := json.NewDecoder(r.Body).Decode(&creds) if err != nil { w.WriteHeader(http.StatusBadRequest) return } if creds.Username != "admin" || creds.Password != "password" { w.WriteHeader(http.StatusUnauthorized) return } token, err := generateToken(creds.Username) if err != nil { w.WriteHeader(http.StatusInternalServerError) return } http.SetCookie(w, &http.Cookie{ Name: "token", Value: token, Expires: time.Now().Add(5 * time.Minute), }) }
步驟 3:JWT 驗證中介軟體
現在,我們需要一個中間件函數來驗證 JWT 令牌,然後才能允許存取受保護的路由。
func authenticate(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { c, err := r.Cookie("token") if err != nil { if err == http.ErrNoCookie { w.WriteHeader(http.StatusUnauthorized) return } w.WriteHeader(http.StatusBadRequest) return } tokenStr := c.Value claims := &Claims{} tkn, err := jwt.ParseWithClaims(tokenStr, claims, func(token *jwt.Token) (interface{}, error) { return jwtKey, nil }) if err != nil || !tkn.Valid { w.WriteHeader(http.StatusUnauthorized) return } next.ServeHTTP(w, r) }) }
此中間件檢查請求是否具有有效的 JWT 令牌。如果不是,它會傳回未經授權的回應。
步驟 4:保護路由
現在,讓我們應用我們的身份驗證中間件來保護 /books 路由:
func main() { r := mux.NewRouter() r.HandleFunc("/login", login).Methods("POST") r.Handle("/books", authenticate(http.HandlerFunc(getBooks))).Methods("GET") fmt.Println("Server started on port :8000") log.Fatal(http.ListenAndServe(":8000", r)) }
測試 API
- 登入產生令牌:
curl -X POST http://localhost:8000/login -d '{"username":"admin", "password":"password"}' -H "Content-Type: application/json"
- 存取受保護的 /books 端點:
curl --cookie "token=<your_token>" http://localhost:8000/books </your_token>
如果令牌有效,您將獲得存取權限。如果沒有,您將收到“401 Unauthorized”。
接下來是什麼?
下次,我們將把 API 連接到資料庫來管理使用者憑證和儲存資料。更多精彩敬請期待!
以上是使用 JWT 身份驗證保護您的 Go API的詳細內容。更多資訊請關注PHP中文網其他相關文章!

你應該關心Go語言中的"strings"包,因為它提供了處理文本數據的工具,從基本的字符串拼接到高級的正則表達式匹配。 1)"strings"包提供了高效的字符串操作,如Join函數用於拼接字符串,避免性能問題。 2)它包含高級功能,如ContainsAny函數,用於檢查字符串是否包含特定字符集。 3)Replace函數用於替換字符串中的子串,需注意替換順序和大小寫敏感性。 4)Split函數可以根據分隔符拆分字符串,常用於正則表達式處理。 5)使用時需考慮性能,如

“編碼/二進制”軟件包interingoisentialForHandlingBinaryData,oferingToolSforreDingingAndWritingBinaryDataEfficely.1)Itsupportsbothlittle-endianandBig-endianBig-endianbyteorders,CompialforOss-System-System-System-compatibility.2)

掌握Go語言中的bytes包有助於提高代碼的效率和優雅性。 1)bytes包對於解析二進制數據、處理網絡協議和內存管理至關重要。 2)使用bytes.Buffer可以逐步構建字節切片。 3)bytes包提供了搜索、替換和分割字節切片的功能。 4)bytes.Reader類型適用於從字節切片讀取數據,特別是在I/O操作中。 5)bytes包與Go的垃圾回收器協同工作,提高了大數據處理的效率。

你可以使用Go語言中的"strings"包來操縱字符串。 1)使用strings.TrimSpace去除字符串兩端的空白字符。 2)用strings.Split將字符串按指定分隔符拆分成切片。 3)通過strings.Join將字符串切片合併成一個字符串。 4)用strings.Contains檢查字符串是否包含特定子串。 5)利用strings.ReplaceAll進行全局替換。注意使用時要考慮性能和潛在的陷阱。

ThebytespackageinGoishighlyeffectiveforbyteslicemanipulation,offeringfunctionsforsearching,splitting,joining,andbuffering.1)Usebytes.Containstosearchforbytesequences.2)bytes.Splithelpsbreakdownbyteslicesusingdelimiters.3)bytes.Joinreconstructsbytesli

thealternativestogo'sbytespackageincageincludethestringspackage,bufiopackage和customstructs.1)thestringspackagecanbeusedforbytemanipulationforbytemanipulationbybyconvertingbytestostostostostostrings.2))

“字節”包裝封裝forefforeflyManipulatingByteslices,CocialforbinaryData,網絡交易和andfilei/o.itoffersfunctionslikeIndexForsearching,BufferForhandLinglaRgedLargedLargedAtaTasets,ReaderForsimulatingStreamReadReadImreAmreadReamReadinging,以及Joineffiter和Joineffiter和Joineffore

go'sstringspackageIscialforficientficientsTringManipulation,uperingToolSlikestrings.split(),strings.join(),strings.replaceall(),andStrings.contains.contains.contains.contains.contains.contains.split.split(split()strings.split()dividesStringoSubSubStrings; 2)strings.joins.joins.joinsillise.joinsinelline joinsiline joinsinelline; 3);


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

Dreamweaver CS6
視覺化網頁開發工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中