jwt 认证初步完成
This commit is contained in:
@@ -26,15 +26,17 @@ func NewUserAddressListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *U
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UserAddressListLogic) UserAddressList(req *types.Request) (resp *types.Response) {
|
||||
func (l *UserAddressListLogic) UserAddressList(req *types.Request, userinfo *auth.UserInfo) (resp *types.Response) {
|
||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||
m := model.NewFsAddressModel(l.svcCtx.MysqlConn)
|
||||
user := auth.GetUserInfoFormCtx(l.ctx)
|
||||
if user.UserId == 0 {
|
||||
return resp.SetStatus(basic.CodeUnAuth)
|
||||
}
|
||||
|
||||
// user := auth.GetUserInfoFormCtx(l.ctx)
|
||||
// if user.UserId == 0 {
|
||||
// return resp.SetStatus(basic.CodeUnAuth)
|
||||
// }
|
||||
// user.UserId = 22
|
||||
// model里设置了 返回值. 和 types 里的一至可以直接返回
|
||||
data, err := m.FindDataAddressList(l.ctx, user.UserId)
|
||||
data, err := m.FindDataAddressList(l.ctx, 22)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error())
|
||||
|
||||
@@ -1,28 +1,34 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
"github.com/474420502/requests"
|
||||
)
|
||||
|
||||
func TestCaseAddressList(t *testing.T) {
|
||||
|
||||
// http.NewRequest("POST", "http://localhost:8888/user/login", body io.Reader)
|
||||
|
||||
// ses := requests.NewSession()
|
||||
// tp := ses.Post("http://localhost:8888/user/login")
|
||||
// resp, err := tp.SetBodyAuto(map[string]interface{}{
|
||||
// "name": "devenv@sina.cn",
|
||||
// "pwd": "$2y$13$6UFDMZQMEfqFYiNLpiUCi.B3fpvGEamPAjIgzUqv/u7jT05nB3pOC",
|
||||
// }, requests.TypeFormData).Execute()
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
// log.Println(string(resp.Content()))
|
||||
ses := requests.NewSession()
|
||||
tp := ses.Post("http://localhost:8888/user/login")
|
||||
resp, err := tp.SetBodyJson(map[string]interface{}{
|
||||
"name": "devenv@sina.cn",
|
||||
"pwd": "$2y$13$6UFDMZQMEfqFYiNLpiUCi.B3fpvGEamPAjIgzUqv/u7jT05nB3pOC",
|
||||
}).Execute()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
result := resp.Json()
|
||||
token := result.Get("data.token")
|
||||
ses.Header.Add("Authorization", token.String())
|
||||
log.Println(string(resp.Content()))
|
||||
|
||||
// resp, err = ses.Get("http://localhost:8888/user/address-list").Execute()
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
// log.Println(string(resp.Content()))
|
||||
resp, err = ses.Get("http://localhost:8888/user/address-list").Execute()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.Println(string(resp.Content()))
|
||||
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ func NewUserBasicInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Use
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UserBasicInfoLogic) UserBasicInfo(req *types.Request) (resp *types.Response) {
|
||||
func (l *UserBasicInfoLogic) UserBasicInfo(req *types.Request, userinfo *auth.UserInfo) (resp *types.Response) {
|
||||
loginInfo := auth.GetUserInfoFormCtx(l.ctx)
|
||||
if loginInfo.UserId == 0 {
|
||||
return resp.SetStatus(basic.CodeOK, "parse login info err ")
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fusenapi/model"
|
||||
"fusenapi/server/home-user-auth/internal/svc"
|
||||
"fusenapi/server/home-user-auth/internal/types"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
@@ -25,7 +26,7 @@ func NewUserFontsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserFon
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UserFontsLogic) UserFonts(req *types.Request) (resp *types.Response) {
|
||||
func (l *UserFontsLogic) UserFonts(req *types.Request, userinfo *auth.UserInfo) (resp *types.Response) {
|
||||
data, err := model.NewFsFontModel(l.svcCtx.MysqlConn).FindAllOrderSortByDesc(l.ctx)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fusenapi/model"
|
||||
"fusenapi/server/home-user-auth/internal/svc"
|
||||
"fusenapi/server/home-user-auth/internal/types"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
@@ -25,7 +26,8 @@ func NewUserGetTypeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserG
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UserGetTypeLogic) UserGetType(req *types.Request) (resp *types.Response) {
|
||||
func (l *UserGetTypeLogic) UserGetType(req *types.Request, userinfo *auth.UserInfo) (resp *types.Response) {
|
||||
|
||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||
data, err := model.NewFsCanteenTypeModel(l.svcCtx.MysqlConn).FindGetType(l.ctx)
|
||||
if err != nil {
|
||||
|
||||
@@ -7,9 +7,9 @@ import (
|
||||
"fusenapi/model"
|
||||
"fusenapi/server/home-user-auth/internal/svc"
|
||||
"fusenapi/server/home-user-auth/internal/types"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"github.com/golang-jwt/jwt"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
@@ -27,46 +27,48 @@ func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLog
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UserLoginLogic) genJwtToken(accessSecret string, accessExpire, nowSec, userid int64) (string, error) {
|
||||
claims := make(jwt.MapClaims)
|
||||
claims["exp"] = nowSec + accessExpire
|
||||
claims["iat"] = nowSec
|
||||
claims["userid"] = userid
|
||||
token := jwt.New(jwt.SigningMethodHS256)
|
||||
token.Claims = claims
|
||||
return token.SignedString([]byte(accessSecret))
|
||||
}
|
||||
|
||||
func (l *UserLoginLogic) UserLogin(req *types.RequestUserLogin) (resp *types.Response, jwtToken string) {
|
||||
// 创建一个 FsUserModel 对象 m 并实例化之,该对象用于操作 MySQL 数据库中的用户数据表。
|
||||
m := model.NewFsUserModel(l.svcCtx.MysqlConn)
|
||||
|
||||
// 在用户数据表中根据登录名查找用户记录,并返回 UserModel 类型的结构体对象 userModel。
|
||||
userModel, err := m.FindOneByEmail(l.ctx, req.Name)
|
||||
|
||||
// 如果在用户数据表中没有找到登录名匹配的用户记录,则抛出 ErrNotFound 错误并返回未认证的状态码以及错误信息。
|
||||
if err == model.ErrNotFound {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error()), jwtToken
|
||||
}
|
||||
|
||||
// 如果在用户数据表中找到了登录名匹配的用户记录,则判断密码是否匹配。
|
||||
if userModel.PasswordHash != req.Password {
|
||||
logx.Info("密码错误")
|
||||
return resp.SetStatusWithMessage(basic.CodeUnAuth, "密码错误"), jwtToken
|
||||
}
|
||||
|
||||
// jwt 生成
|
||||
// 如果密码匹配,则生成 JWT Token。
|
||||
nowSec := time.Now().Unix()
|
||||
jwtToken, err = l.genJwtToken(l.svcCtx.Config.Auth.AccessSecret, l.svcCtx.Config.Auth.AccessExpire, nowSec, userModel.Id)
|
||||
jwtToken, err = auth.GenerateJwtToken(l.svcCtx.Config.Auth.AccessSecret, l.svcCtx.Config.Auth.AccessExpire, nowSec, int(userModel.Id))
|
||||
|
||||
// 如果生成 JWT Token 失败,则抛出错误并返回未认证的状态码。
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatus(basic.CodeUnAuth), jwtToken
|
||||
}
|
||||
|
||||
// 更新 MySQL 数据库中用户记录对应的 VerificationToken 字段值为生成的 JWT Token。
|
||||
err = m.UpdateVerificationToken(l.ctx, userModel.Id, jwtToken)
|
||||
|
||||
// 如果更新 VerificationToken 字段失败,则返回未认证的状态码。
|
||||
if err != nil {
|
||||
return resp.SetStatus(basic.CodeUnAuth), jwtToken
|
||||
}
|
||||
|
||||
// 构造 DataUserLogin 类型的数据对象 data 并设置其属性值为生成的 JWT Token。
|
||||
data := &types.DataUserLogin{
|
||||
Token: jwtToken,
|
||||
}
|
||||
|
||||
// 返回认证成功的状态码以及数据对象 data 和 JWT Token。
|
||||
return resp.SetStatus(basic.CodeOK, data), jwtToken
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ func NewUserSaveBasicInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UserSaveBasicInfoLogic) UserSaveBasicInfo(req *types.RequestBasicInfoForm) (resp *types.Response) {
|
||||
func (l *UserSaveBasicInfoLogic) UserSaveBasicInfo(req *types.RequestBasicInfoForm, userinfo *auth.UserInfo) (resp *types.Response) {
|
||||
loginInfo := auth.GetUserInfoFormCtx(l.ctx)
|
||||
if loginInfo.UserId == 0 {
|
||||
return resp.SetStatus(basic.CodeOK, "parse login info err ")
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"fusenapi/server/home-user-auth/internal/svc"
|
||||
"fusenapi/server/home-user-auth/internal/types"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
@@ -24,7 +25,7 @@ func NewUserStatusConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UserStatusConfigLogic) UserStatusConfig(req *types.Request) (resp *types.Response) {
|
||||
func (l *UserStatusConfigLogic) UserStatusConfig(req *types.Request, userinfo *auth.UserInfo) (resp *types.Response) {
|
||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||
|
||||
data := &types.DataStatusConfig{
|
||||
|
||||
Reference in New Issue
Block a user