jwt 认证初步完成

This commit is contained in:
eson
2023-06-12 15:17:42 +08:00
parent 7fd80315b2
commit 427140f140
33 changed files with 610 additions and 85 deletions

View File

@@ -12,9 +12,37 @@ type ServiceContext struct {
}
func NewServiceContext(c {{.config}}) *ServiceContext {
return &ServiceContext{
Config: c,
MysqlConn: sqlx.NewMysql(c.SourceMysql),
{{.middlewareAssignment}}
}
}
func (svcCxt *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) {
AuthKey := r.Header.Get("Authorization")
if len(AuthKey) <= 50 {
return nil, errors.New(fmt.Sprint("Error parsing token, len:", len(AuthKey)))
}
token, err := jwt.Parse(AuthKey, func(token *jwt.Token) (interface{}, error) {
// HS256
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
//
return svcCxt.Config.Auth.AccessSecret, nil
})
if err != nil {
return nil, errors.New(fmt.Sprint("Error parsing token:", err))
}
//
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
return claims, nil
}
return nil, errors.New(fmt.Sprint("Invalid token", err))
}

View File

@@ -7,12 +7,37 @@ import (
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
{{.ImportPackages}}
)
func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// jwtToken
claims, err := svcCtx.ParseJwtToken(r)
// JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
// Token里获取对应的信息
userinfo, err := auth.GetUserInfoFormMapClaims(claims)
// JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
{{if .HasRequest}}var req types.{{.RequestType}}
// 使httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 510,
@@ -21,9 +46,11 @@ func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
logx.Info(err)
return
}
//
{{end}}l := {{.LogicName}}.New{{.LogicType}}(r.Context(), svcCtx)
{{if .HasResp}}resp{{end}} := l.{{.Call}}({{if .HasRequest}}&req{{end}})
{{if .HasResp}}resp{{end}} := l.{{.Call}}({{if .HasRequest}}&req, userinfo{{end}})
// nil使httpx.OkJsonCtx方法返回JSON响应;
// 500JSON响应并记录错误消息logx.Error
if resp != nil {
{{if .HasResp}}httpx.OkJsonCtx(r.Context(), w, resp){{else}}httpx.Ok(w){{end}}
} else {

View File

@@ -1,6 +1,9 @@
package {{.pkgName}}
import (
"fusenapi/utils/auth"
"fusenapi/utils/basic"
{{.imports}}
)
@@ -18,8 +21,9 @@ func New{{.logic}}(ctx context.Context, svcCtx *svc.ServiceContext) *{{.logic}}
}
}
func (l *{{.logic}}) {{.function}}({{.request}}) (resp *types.Response) {
func (l *{{.logic}}) {{.function}}({{.request}}, userinfo *auth.UserInfo) (resp *types.Response) {
// Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo , null
{{.returnString}} resp.SetStatus(basic.CodeOK)
}

View File

@@ -8,7 +8,6 @@ import (
{{.types}}
// Set 设置Response的Code和Message值
func (resp *Response) Set(Code int, Message string) *Response {
return &Response{