mirror of
https://gitee.com/shikong-sk/gofiber-study
synced 2025-05-10 03:58:05 +08:00
添加 jwt && 配置
models 结构调整
This commit is contained in:
parent
28f4198697
commit
b2972673ae
@ -26,6 +26,7 @@ func Run() {
|
||||
|
||||
reloadRedis(global.Config)
|
||||
reloadDataSources(global.Config)
|
||||
reloadJwt(global.Config)
|
||||
|
||||
App = reloadApp(global.Config)
|
||||
|
||||
|
10
app/jwt.go
Normal file
10
app/jwt.go
Normal file
@ -0,0 +1,10 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"gofiber.study.skcks.cn/common/config"
|
||||
"gofiber.study.skcks.cn/global"
|
||||
)
|
||||
|
||||
func reloadJwt(c *config.BasicConfig) {
|
||||
global.JwtConfig = &c.Jwt
|
||||
}
|
@ -4,6 +4,12 @@ type BasicConfig struct {
|
||||
Server ServerConfig `yaml:"server"`
|
||||
Mysql MysqlConfig `yaml:"mysql"`
|
||||
Redis RedisConfig `yaml:"redis"`
|
||||
Jwt JwtConfig `yaml:"jwt"`
|
||||
}
|
||||
|
||||
type JwtConfig struct {
|
||||
Secret string `yaml:"secret"`
|
||||
Expire int64 `yaml:"expire"`
|
||||
}
|
||||
|
||||
type MysqlConfig struct {
|
||||
|
@ -12,6 +12,12 @@ server:
|
||||
# 启用路由信息 /routes
|
||||
enableRoutesMsg: true
|
||||
|
||||
jwt:
|
||||
# token 加密
|
||||
secret: 80ca7934863638942db04cec68e57bb4
|
||||
# token 过期时间
|
||||
expire: 7200
|
||||
|
||||
mysql:
|
||||
# 自动同步数据库/表结构
|
||||
autoSync: true
|
||||
|
@ -3,7 +3,7 @@ package global
|
||||
import (
|
||||
"gofiber.study.skcks.cn/common/logger"
|
||||
models2 "gofiber.study.skcks.cn/model/casbin_model/models"
|
||||
"gofiber.study.skcks.cn/model/user/models"
|
||||
"gofiber.study.skcks.cn/model/generic/models"
|
||||
"xorm.io/xorm"
|
||||
"xorm.io/xorm/names"
|
||||
)
|
||||
|
44
global/jwt.go
Normal file
44
global/jwt.go
Normal file
@ -0,0 +1,44 @@
|
||||
package global
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
"gofiber.study.skcks.cn/common/config"
|
||||
"time"
|
||||
)
|
||||
|
||||
var JwtConfig *config.JwtConfig
|
||||
|
||||
type UserClaims struct {
|
||||
Id string `json:"id"`
|
||||
Account string `json:"account"`
|
||||
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
func GetToken(claims UserClaims) (string, error) {
|
||||
iat := time.Now().Unix()
|
||||
claims.IssuedAt = jwt.NewNumericDate(time.Unix(iat, 0))
|
||||
|
||||
expire := iat + JwtConfig.Expire
|
||||
claims.ExpiresAt = jwt.NewNumericDate(time.Unix(expire, 0))
|
||||
|
||||
token := jwt.New(jwt.SigningMethodHS256)
|
||||
return token.SignedString([]byte(JwtConfig.Secret))
|
||||
}
|
||||
|
||||
func ParseToken(tokenStr string) (*UserClaims, error) {
|
||||
token, err := jwt.ParseWithClaims(tokenStr, &UserClaims{}, func(token *jwt.Token) (interface{}, error) {
|
||||
return JwtConfig.Secret, nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if claims, ok := token.Claims.(*UserClaims); ok && token.Valid {
|
||||
return claims, nil
|
||||
}
|
||||
|
||||
return nil, errors.New("无效的令牌")
|
||||
}
|
1
go.mod
1
go.mod
@ -30,6 +30,7 @@ require (
|
||||
github.com/go-openapi/jsonreference v0.19.6 // indirect
|
||||
github.com/go-openapi/spec v0.20.4 // indirect
|
||||
github.com/go-openapi/swag v0.19.15 // indirect
|
||||
github.com/golang-jwt/jwt/v4 v4.4.2 // indirect
|
||||
github.com/golang/snappy v0.0.4 // indirect
|
||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||
github.com/josharian/intern v1.0.0 // indirect
|
||||
|
2
go.sum
2
go.sum
@ -173,6 +173,8 @@ github.com/gogo/googleapis v1.1.0/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFG
|
||||
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
|
||||
github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
|
||||
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
|
||||
github.com/golang-jwt/jwt/v4 v4.4.2 h1:rcc4lwaZgFMCZ5jxF9ABolDcIHdBytAFgqFPbSJQAYs=
|
||||
github.com/golang-jwt/jwt/v4 v4.4.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
|
||||
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
|
||||
|
@ -3,7 +3,7 @@ package generate
|
||||
import (
|
||||
"fmt"
|
||||
"gofiber.study.skcks.cn/common/utils"
|
||||
"gofiber.study.skcks.cn/model/user/models"
|
||||
"gofiber.study.skcks.cn/model/generic/models"
|
||||
"testing"
|
||||
)
|
||||
|
@ -3,7 +3,7 @@ package user
|
||||
import (
|
||||
"errors"
|
||||
"gofiber.study.skcks.cn/global"
|
||||
"gofiber.study.skcks.cn/model/user/models"
|
||||
"gofiber.study.skcks.cn/model/generic/models"
|
||||
)
|
||||
|
||||
var (
|
||||
|
Loading…
Reference in New Issue
Block a user