```
feat(login): 添加Bcrypt密码哈希功能并集成用户认证 - 引入golang.org/x/crypto/bcrypt包用于密码哈希处理 - 实现HashPassword函数对密码进行Bcrypt哈希 - 实现CheckPasswordHash函数验证密码与哈希匹配 - 添加示例代码演示密码哈希和验证功能 feat(login): 集成外部用户信息服务 - 实现GetUserInfo方法调用外部服务获取用户信息 - 添加用户信息展示的示例代码 - 集成用户登录验证流程 fix
This commit is contained in:
@@ -2,6 +2,8 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
@@ -47,13 +49,54 @@ func (s *BaseSysLoginService) Login(ctx context.Context, req *v1.BaseOpenLoginRe
|
||||
err = gerror.New("验证码错误")
|
||||
return
|
||||
}
|
||||
md5password, _ := gmd5.Encrypt(password)
|
||||
// 调用方法
|
||||
userInfo, err := GetUserInfo(username, password)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// // 输出结果
|
||||
// fmt.Printf("用户名: %s\n", userInfo.Data.Attributes.Username)
|
||||
// fmt.Printf("显示名: %s\n", userInfo.Data.Attributes.DisplayName)
|
||||
// fmt.Printf("头像: %s\n", userInfo.Data.Attributes.AvatarUrl)
|
||||
// fmt.Printf("加入时间: %s\n", userInfo.Data.Attributes.JoinTime)
|
||||
// fmt.Printf("金钱: %.2f\n", userInfo.Data.Attributes.Money)
|
||||
// fmt.Printf("用户组: %s\n", userInfo.Included[0].Attributes.NameSingular)
|
||||
md5password, _ := gmd5.Encrypt(password)
|
||||
var user *model.BaseSysUser
|
||||
cool.DBM(baseSysUser).Where("username=?", username).Where("password=?", md5password).Where("status=?", 1).Scan(&user)
|
||||
if user == nil {
|
||||
err = gerror.New("账户或密码不正确~")
|
||||
return
|
||||
|
||||
if userInfo.Data.Attributes.Username != username { //说明没查找到用户
|
||||
//后端添加的账户
|
||||
cool.DBM(baseSysUser).Where("username=?", username).Where("password=?", md5password).Where("status=?", 1).Scan(&user)
|
||||
if user == nil {
|
||||
|
||||
err = gerror.New("账户或密码不正确~")
|
||||
return
|
||||
}
|
||||
|
||||
} else {
|
||||
m := cool.DBM(baseSysUser).Where("username=?", username).Where("status=?", 1)
|
||||
m.Scan(&user)
|
||||
if user == nil {
|
||||
//这里实现注册用户
|
||||
//err = gerror.New("账户或密码不正确~")
|
||||
// return
|
||||
|
||||
NewBaseSysUserService().Gen(userInfo.Data.Attributes)
|
||||
m := cool.DBM(baseSysUser).Where("username=?", username).Where("status=?", 1)
|
||||
m.Scan(&user)
|
||||
} else {
|
||||
user.HeadImg = &userInfo.Data.Attributes.AvatarUrl
|
||||
var ttt = *user.PasswordV + 1
|
||||
user.PasswordV = &ttt
|
||||
_, err := m.Save(user)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
cool.CacheManager.Set(ctx, fmt.Sprintf("admin:passwordVersion:%d", user.ID), user.PasswordV, 0)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
result, err = s.generateTokenByUser(ctx, user)
|
||||
@@ -90,6 +133,7 @@ func (*BaseSysLoginService) Captcha(req *v1.BaseOpenCaptchaReq) (interface{}, er
|
||||
// Logout 退出登录
|
||||
func (*BaseSysLoginService) Logout(ctx context.Context) (err error) {
|
||||
userId := cool.GetAdmin(ctx).UserId
|
||||
|
||||
cool.CacheManager.Remove(ctx, "admin:department:"+gconv.String(userId))
|
||||
cool.CacheManager.Remove(ctx, "admin:perms:"+gconv.String(userId))
|
||||
cool.CacheManager.Remove(ctx, "admin:token:"+gconv.String(userId))
|
||||
|
||||
Reference in New Issue
Block a user