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