feat(login): 添加Bcrypt密码哈希功能并集成用户认证

- 引入golang.org/x/crypto/bcrypt包用于密码哈希处理
- 实现HashPassword函数对密码进行Bcrypt哈希
- 实现CheckPasswordHash函数验证密码与哈希匹配
- 添加示例代码演示密码哈希和验证功能

feat(login): 集成外部用户信息服务

- 实现GetUserInfo方法调用外部服务获取用户信息
- 添加用户信息展示的示例代码
- 集成用户登录验证流程

fix
This commit is contained in:
2025-12-31 16:20:01 +08:00
parent 689367ba7d
commit ba60b03bbf
5 changed files with 287 additions and 6 deletions

View File

@@ -2,6 +2,7 @@ package main
import (
_ "github.com/gogf/gf/contrib/nosql/redis/v2"
"golang.org/x/crypto/bcrypt"
"blazing/common/data/xmlres"
_ "blazing/contrib/files/local"
@@ -27,6 +28,31 @@ func init() {
}
func main() {
// 调用方法
// userInfo, err := service.GetUserInfo("694425176@qq.com", "qq694425176")
// 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)
// password := "12345678"
// // 哈希密码
// hash, err := HashPassword(password)
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println("哈希结果:", hash)
// hash = "$2y$10$027xtOgP6yG8HRPP5zpNdO1ay1MzaxIeBDdNt73VgE2WWQqTUYih2"
// // 验证密码
// match := CheckPasswordHash(password, hash)
// fmt.Println("密码匹配:", match)
// service.NewShinyService().Args(53)
//player.TestPureMatrixSplit()
// for _, i := range xmlres.ItemsMAP {
@@ -113,3 +139,16 @@ func kick(id int) {
// fmt.Println(err)
// }()
}
// HashPassword 对密码进行 Bcrypt 哈希
func HashPassword(password string) (string, error) {
// bcrypt.DefaultCost = 10与 Laravel 默认一致
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
return string(bytes), err
}
// CheckPasswordHash 验证密码与哈希是否匹配
func CheckPasswordHash(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}