代码
package serviceimport ("fmt"//"fmt""gin/common""gin/config"sysEntity "gin/system/entity"sysUtil "gin/system/util""github.com/gin-gonic/gin""log"
)func Login(c *gin.Context, loginBody *sysEntity.LoginBody) map[string]string {var resultList []map[string]interface{}var resultTokenList = make(map[string]string)//参数接收userName := loginBody.UserNamepassword := loginBody.Password//不为空校验传参if userName == "" {common.ReturnError(c, 500, sysUtil.MapToJson("用户名不能为空"), resultList, int64(len(resultList)))panic("")}if password == "" {common.ReturnError(c, 500, sysUtil.MapToJson("密码不能为空"), resultList, int64(len(resultList)))panic("")}log.Println("用户输入的账号:" + userName)log.Println("用户输入的密码:" + password)//TODO 校验数据库中的 用户信息//从数据库中查询输入用户的信息sysUser := &sysEntity.LoginBody{}//取值 赋值sysUser.UserName = userName//查收过滤条件拼装//查询多条记录,Find函数返回的是一个数组err := config.GormDb.Model(&sysEntity.LoginBody{}).Table("sys_user ").Select("user_id as UserId", "user_name as UserName", "password as Password", "dept_id as DeptId").Where("LOWER(user_name) = LOWER(?)", userName).Scan(&resultList).Errorif err != nil {log.Println("查询用户失败,原因:", err)//panic("用户查询失败")}if resultList == nil {log.Println("查询用户失败,原因:数据库中没有查到 userName 叫 admin 的用户信息")common.ReturnError(c, 500, sysUtil.MapToJson("查询用户失败,原因:数据库中没有查到 userName 叫 admin 的用户信息"), resultList, int64(len(resultList)))panic("")}//数据库中有数据进行后面操作userNameDB := ""passwordDB := ""deptIdDB := 0if len(resultList) > 0 {log.Println("用户信息查询成功!!!!!,进行后续操作")for _, item := range resultList {// 将集合中的值赋值给sysUser结构体的对应字段userNameDB = item["UserName"].(string)passwordDB = item["Password"].(string)//deptIdDB = item["DeptId"].(int)log.Println("数据库中的密码密文为:" + userNameDB + "=====" + passwordDB)}}//rsa解密 数据库中用户的密文密码plainText := []byte(passwordDB)plainText, err = sysUtil.RsaDecrypt(plainText, "./private.pem")if err != nil {log.Println("==================解密发生异常")fmt.Println(err)}fmt.Printf("数据库密码解密后的明文为:%s\n", plainText)log.Println("====================用户输入的明文密码为:" + password)log.Println("====================数据库中查找到的密码解密后为:" + string(plainText))//数据库中的密码密文 解密后,与用户输入的 明文密码 进行比较if string(plainText) == password {log.Println("************************您输入的密码正确,密码校验通过!!!********************************")} else {log.Println("************************您输入的密码错误,密码校验不通过!!!********************************")common.ReturnError(c, 500, sysUtil.MapToJson("登录失败,用户密码错误,请仔细检查"), resultList, int64(len(resultList)))panic("")}//TODO 创建令牌sysUserEntity := &sysEntity.SysUserEntity{}sysUserEntity.UserName = userNamesysUserEntity.DeptId = deptIdDBsysUserEntity.Password = string(plainText)//TODOtoken := CreateToken(c, userName, sysUserEntity)log.Println("111111111111111111111111111111111111111111111111111----token生成结束, TIKEN:" + token)resultTokenList["token"] = tokenreturn resultTokenList
}//RSA加密解密测试验证
func Rsapassword(c *gin.Context, loginBody *sysEntity.LoginBody) []map[string]interface{} {var resultList []map[string]interface{}//参数接收//userName := loginBody.UserName//password := loginBody.Password//生成密钥对err := sysUtil.GenerateRsaKey(1024, "./")if err != nil {log.Println("==================生成密钥对发生异常")fmt.Println(err)}//加密plainText := []byte("123456")cipherText, err := sysUtil.RsaEncrypt(plainText, "./public.pem")if err != nil {fmt.Println(err)log.Println("==================加密发生异常")}fmt.Printf("加密后为:%s\n", cipherText)//解密plainText, err = sysUtil.RsaDecrypt(cipherText, "./private.pem")if err != nil {log.Println("==================解密发生异常")fmt.Println(err)}fmt.Printf("解密后为:%s\n", plainText)return resultList
}
2.