2 Star 1 Fork 0

法马智慧/fmgo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
jwt.go 3.27 KB
一键复制 编辑 原始数据 按行查看 历史
零海 提交于 2022-08-12 09:24 . jwt添加解析string方法
package jwtx
import (
"fmt"
"github.com/dgrijalva/jwt-go"
"net/http"
"strings"
"time"
)
const (
DefaultAccessSecret = "jwtx"
DefaultAccessExpire = 3 * 24 * 60 * 60 // 三天
)
type (
JwtMould struct {
SecretKey string
AccessExpire int64
}
JwtClaims struct {
UserID int `json:"user_id"`
OrgID int `json:"dept_id"`
RoleID int `json:"role_id"`
Username string `json:"username"`
Account string `json:"account"`
ProjectID int `json:"projectID,omitempty"` // 项目ID
jwt.StandardClaims
}
)
func DefaultJwt() *JwtMould {
return &JwtMould{
SecretKey: DefaultAccessSecret,
AccessExpire: DefaultAccessExpire,
}
}
func Jwt(AccessSecret string, AccessExpire int64) *JwtMould {
return &JwtMould{
SecretKey: AccessSecret,
AccessExpire: AccessExpire,
}
}
func (j *JwtMould) Token(userid int, username, account string, roleID int, orgID int) (string, error) {
jwtClaims := JwtClaims{
UserID: userid,
OrgID: orgID,
RoleID: roleID,
Username: username,
Account: account,
StandardClaims: jwt.StandardClaims{
ExpiresAt: time.Now().Add(time.Duration(j.AccessExpire) * time.Second).Unix(),
},
}
tokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, jwtClaims)
return tokenClaims.SignedString([]byte(j.SecretKey))
}
func (j *JwtMould) UpdateToken(token *JwtClaims) (string, error) {
token.StandardClaims = jwt.StandardClaims{
ExpiresAt: time.Now().Add(time.Duration(j.AccessExpire) * time.Second).Unix(),
}
tokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, token)
return tokenClaims.SignedString([]byte(j.SecretKey))
}
func (j *JwtMould) GetClaims(authorization string) *JwtClaims {
tokenStr := strings.ReplaceAll(authorization, "Bearer ", "")
token, err := jwt.ParseWithClaims(tokenStr, &JwtClaims{}, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected login method %v", token.Header["alg"])
}
return []byte(j.SecretKey), nil
})
if err != nil {
return nil
}
claims, ok := token.Claims.(*JwtClaims)
if ok && token.Valid {
return claims
}
return nil
}
func (j *JwtMould) Claims(r *http.Request) *JwtClaims {
authorization := r.Header.Get("Authorization")
if authorization == "" {
return nil
}
token := strings.Split(authorization, " ")
if len(token) != 2 {
return nil
}
Token, err := jwt.ParseWithClaims(token[1], &JwtClaims{}, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected login method %v", token.Header["alg"])
}
return []byte(j.SecretKey), nil
})
if err != nil {
return nil
}
claims, ok := Token.Claims.(*JwtClaims)
if ok && Token.Valid {
return claims
}
return nil
}
func (j *JwtMould) ClaimsToken(auth string) *JwtClaims {
token := strings.Split(auth, " ")
if len(token) != 2 {
return nil
}
Token, err := jwt.ParseWithClaims(token[1], &JwtClaims{}, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected login method %v", token.Header["alg"])
}
return []byte(j.SecretKey), nil
})
if err != nil {
return nil
}
claims, ok := Token.Claims.(*JwtClaims)
if ok && Token.Valid {
return claims
}
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/fmpt/fmgo.git
git@gitee.com:fmpt/fmgo.git
fmpt
fmgo
fmgo
v1.2.45

搜索帮助