1 Star 0 Fork 0

han/keyauth-g7

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
restful.go 1.18 KB
一键复制 编辑 原始数据 按行查看 历史
han 提交于 2023-07-23 23:49 . 11
package auth
import (
"gitee.com/hanshengjian/keyauth-g7/apps/token"
"gitee.com/hanshengjian/keyauth-g7/common/utils"
"github.com/emicklei/go-restful/v3"
"github.com/infraboard/mcube/http/label"
"github.com/infraboard/mcube/http/response"
)
// Go-Restful auth Middleware
func (a *KeyauthAuther) RestfulAuthHandlerFunc(
req *restful.Request,
resp *restful.Response,
chain *restful.FilterChain,
) {
meta := req.SelectedRoute().Metadata()
a.log.Debug(meta)
var isAuthEnable bool
if authV, ok := meta[label.Auth]; ok {
switch v := authV.(type) {
case bool:
isAuthEnable = v
case string:
isAuthEnable = v == "true"
}
}
if isAuthEnable {
// 处理Request对象
// *restful.Request
// 1. 认证中间件, 需要获取Token
tkStr := utils.GetToken(req.Request)
// 2. 到用户中心验证token合法性, 依赖用户中心的Grpc Client
validateReq := token.NewValidateTokenRequest(tkStr)
tk, err := a.auth.ValidateToken(req.Request.Context(), validateReq)
if err != nil {
response.Failed(resp.ResponseWriter, err)
return
}
// Set Context
req.SetAttribute("token", tk)
// req.Attribute("token").(*token.Token)
}
chain.ProcessFilter(req, resp)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/hanshengjian/keyauth-g7.git
git@gitee.com:hanshengjian/keyauth-g7.git
hanshengjian
keyauth-g7
keyauth-g7
23cb155209a5

搜索帮助

A270a887 8829481 3d7a4017 8829481