Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement login verification with go gin+token verification

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you go gin+token verification is how to achieve login verification, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.

1. Prepare go get github.com/dgrijalva/jwt-gogo get github.com/gin-gonic/gin2. Code package main import ("errors"fmt"github.com/dgrijalva/jwt-go"github.com/gin-gonic/gin"net/http"time") func main () {r: = gin.Default () r.GET ("/ login/:username/:password", login) r.GET ("/ verify/:token") Verify) r.GET ("/ refresh/:token", refresh) r.GET ("/ sayHello/:token" SayHello) r.Run (": 9090") / / http://localhost:9090/login/dong/123456 / / http://localhost:9090/verify/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NjA1MTIyMTAsImlhdCI6MTU2MDUwODYxMCwidXNlcl9pZCI6MSwicGFzc3dvcmQiOiIxMjM0NTYiLCJ1c2VybmFtZSI6ImRvbmciLCJmdWxsX25hbWUiOiJkb25nIiwicGVybWlzc2lvbnMiOltdfQ.Esh2Zge0vO1BAW1GeR5wurWP3H1jUIaMf3tcSaUwkzA / / http://localhost:9090/refresh/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NjA1MTIyNDMsImlhdCI6MTU2MDUwODYxMCwidXNlcl9pZCI6MSwicGFzc3dvcmQiOiIxMjM0NTYiLCJ1c2VybmFtZSI6ImRvbmciLCJmdWxsX25hbWUiOiJkb25nIiwicGVybWlzc2lvbnMiOltdfQ.Xkb_J8MWXkwGUcBF9bpp2Ccxp8nFPtRzFzOBeboHmg0} const (ErrorReason_ServerBusy = "Server busy" ErrorReason_ReLogin = "Please login again") func sayHello (c * gin. Context) {strToken: = c.Param ("token") claim Err: = verifyAction (strToken) if err! = nil {c.String (http.StatusNotFound, err.Error ()) return} c.String (http.StatusOK, "hello,", claim.Username)} type JWTClaims struct {/ / token add user information User information jwt.StandardClaims UserID int `json: "user_id" `Password string `json: "password" `Username string `json: "username" `FullName string `json: "full_name" `Permissions [] string `json: "permissions" `} var (Secret = "dong_tech" / / ExpireTime = 3600) may be used after verification of the user / / token validity period) func login (c * gin.Context) {username: = c.Param ("username") password: = c.Param ("password") claims: = & JWTClaims {UserID: 1 Username: username, Password: password, FullName: username, Permissions: [] string {},} claims.IssuedAt = time.Now (). Unix () claims.ExpiresAt = time.Now () .Add (time.Second * time.Duration (ExpireTime)) .Unix () signedToken Err:=getToken (claims) if erratic roomnil {c.String (http.StatusNotFound, err.Error ()) return} c.String (http.StatusOK, signedToken)} func verify (c * gin.Context) {strToken: = c.Param ("token") claim,err: = verifyAction (strToken) if erratic roomnil {c.String (http.StatusNotFound) Err.Error () return} c.String (http.StatusOK, "verify,", claim.Username)} func refresh (c * gin.Context) {strToken: = c.Param ("token") claims,err: = verifyAction (strToken) if err! = nil {c.String (http.StatusNotFound) Err.Error () return} claims.ExpiresAt = time.Now () .Unix () + (claims.ExpiresAt-claims.IssuedAt) signedToken,err:=getToken (claims) if erratic roomnil {c.String (http.StatusNotFound, err.Error () return} c.String (http.StatusOK, signedToken)} func verifyAction (strToken string) (* JWTClaims Error) {token, err: = jwt.ParseWithClaims (strToken, & JWTClaims {}, func (token * jwt.Token) (interface {}, error) {return [] byte (Secret), nil}) if err! = nil {return nil, errors.New (ErrorReason_ServerBusy)} claims Ok: = token.Claims. (* JWTClaims) if! ok {return nil, errors.New (ErrorReason_ReLogin)} if err: = token.Claims.Valid () Err! = nil {return nil, errors.New (ErrorReason_ReLogin)} fmt.Println ("verify") return claims, nil} func getToken (claims * JWTClaims) (string,error) {token: = jwt.NewWithClaims (jwt.SigningMethodHS256, claims) signedToken, err: = token.SignedString ([] byte (Secret) if err! = nil {return "" Errors.New (ErrorReason_ServerBusy)} return signedToken,nil}

The running result is shown in the figure:

Access interface

Landing

Verification

Refresh

Http://localhost:9090/login/

Http://localhost:9090/verify/

Http://localhost:9090/refresh/

The above is how go gin+token verification implements login verification. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report