当前位置:网站首页>Golang JWT cross domain authentication
Golang JWT cross domain authentication
2022-07-27 04:05:00 【Crisp_ LF】
Golang jwt Cross domain authentication
JWT Full name JSON Web Token Is a cross domain authentication solution , It belongs to an open standard , It stipulates a kind of Token Realization way , At present, it is mostly used for front and rear end separation projects and OAuth2.0
install jwt
go get github.com/dgrijalva/jwt-go
Generate jwt
package main
import (
"errors"
"fmt"
jwt "github.com/dgrijalva/jwt-go"
"time"
)
type MyClaims struct {
// Except under the feet Claims, You also need user information
Username string `json:"username"`
Password string `json:"password"`
//jwt Medium standard Claims
jwt.StandardClaims
}
var key = []byte("secret")
//GenToken Generate token Methods
func GenToken(username string, password string) (string, error) {
// Create our own statement
c := MyClaims{
username, // Custom field
password,
jwt.StandardClaims{
ExpiresAt: time.Now().Add(time.Hour * 2).Unix(), // Expiration time
Issuer: "Crisp", // Issued by people
},
}
// Creates a signature object using the specified signature method
token := jwt.NewWithClaims(jwt.SigningMethodHS256, c) // Use here HS256 encryption algorithm
// Use specified secret Sign and get the complete encoded string token
// Note that this place must be a byte slice, not a string
return token.SignedString(key)
}
analysis jwt
//ParseToken analysis token Methods
func ParseToken(tokenString string) (*MyClaims, error) {
// analysis token
token, err := jwt.ParseWithClaims(tokenString, &MyClaims{
},
func(token *jwt.Token) (i interface{
}, err error) {
return key, nil
})
if err != nil {
return nil, err
}
if claims, ok := token.Claims.(*MyClaims); ok && token.Valid {
// check token
return claims, nil
}
return nil, errors.New("invalid token")
}
Call to generate token And analyze token
func main() {
// Generate token
token, err := GenToken("Crisp", "12345678")
if err != nil {
panic(err)
}
// analysis token
parseToken, err := ParseToken(token)
if err != nil {
panic(err)
}
fmt.Printf("parseToken.UserName: %v\n", parseToken.Username)
fmt.Printf("parseToken.Password: %v\n", parseToken.Password)
}
Code run results
Generated token:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IkNyaXNwIiwicGFzc3dvcmQiOiIxMjM0NTY3OCIsImV4cCI6MTY1ODcyNzQxNXNzIjoiQ3Jpc3AifQ.wXUoWlv8VeMJo_ezWSlsEO_XOfcwh3aD4Y_q0YoZfDg
analysis token:
analysis token get :parseToken.UserName: Crisp
analysis token get :parseToken.Password: 12345678
边栏推荐
- VR全景人淘金“小心机”(上)
- 注释有点好玩哦
- 分享当下人生——一个高中毕业生在中央电视台的六星期实习经历
- 【SemiDrive源码分析】【驱动BringUp】41 - LCM 驱动 backlight 背光控制原理分析
- Cocos game practice-05-npc and character attack logic
- A. YES or YES?
- Regression testing: meaning, challenges, best practices and tools
- 真正意义上的数字零售应当具有更加丰富的内涵和意义
- C语言学习笔记 —— 内存管理
- Share the current life -- a six week internship experience of a high school graduate in CCTV
猜你喜欢

Kettle reads file split by line

【愚公系列】2022年7月 Go教学课程 018-分支结构之switch

零基础小白也能懂的 Redis 数据库,手把手教你易学易用!

3381. 手机键盘(清华大学考研机试题)

Feitengtengrui d2000 won the "top ten hard core technologies" award of Digital China
![[Yugong series] July 2022 go teaching course 018 switch of branch structure](/img/50/171a083713597f1b5643835377a12d.png)
[Yugong series] July 2022 go teaching course 018 switch of branch structure

手动从0搭建ABP框架-ABP官方完整解决方案和手动搭建简化解决方案实践

Parallel desktop startup virtual machine "operation failed" problem solution

PSINS工具箱中轨迹生成工具详细解析

Lixia action | Yuanqi Digitalization: existing mode or open source innovation?
随机推荐
Threads and processes
小于等于K的最大子数组累加和
A. Round Down the Price
Plato farm is expected to further expand its ecosystem through elephant swap
Application, addition and deletion of B-tree
On the first day of Shenzhen furniture exhibition, the three highlights of Jin Ke'er booth were unlocked!
H.265网页播放器EasyPlayer对外开放录像的方法
Will this flinkcdc monitor all tables in the database? Or the designated table? I look at the background log. It monitors all tables. If it monitors
leetcode:433. 最小基因变化
VR全景人淘金“小心机”(上)
Bean Validation原理篇--07
零基础小白也能懂的 Redis 数据库,手把手教你易学易用!
Lixia action | Yuanqi Digitalization: existing mode or open source innovation?
Realization of regular hexagon map with two-dimensional array of unity
函数指针与回调函数
明汯投资裘慧明:长期优异超额的背后考验的是团队的投研能力和策略的完整性
科目三: 济南章丘五号线
Connman introduction
Chapter 5 决策树和随机森林实践
Machine learning [Matplotlib]