YiTao/middleware/jwt.go
2024-11-09 14:59:27 +08:00

30 lines
831 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package middleware
import (
"yitao/config"
"github.com/iris-contrib/middleware/jwt"
)
var JwtMiddleware *jwt.Middleware
func init() {
JwtMiddleware = jwt.New(jwt.Config{
// Extractor属性可以选择从什么地方获取jwt进行验证默认从http请求的header中的Authorization字段提取也可指定为请求参数中的某个字段
// 从请求参数token中提取
// Extractor: jwt.FromParameter("token"),
// 从请求头的Authorization字段中提取这个是默认值
Extractor: jwt.FromAuthHeader,
// 设置一个函数返回秘钥关键在于return []byte("这里设置秘钥")
ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
return []byte(config.GetConfig().JwtSecret), nil
},
// 设置一个加密方法
SigningMethod: jwt.SigningMethodHS256,
})
}