代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。