You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
1.9 KiB

package LoginController
import (
"dsSdsf/Utils/CommonUtil"
"dsSdsf/Utils/ConfigUtil"
"dsSdsf/Utils/RedisUtil"
"dsSdsf/Utils/RsaUtil"
"encoding/base64"
"github.com/gin-gonic/gin"
"net/http"
)
//模块的路由配置
func Routers(r *gin.RouterGroup) {
rr := r.Group("/login")
rr.GET("/test", test)
rr.GET("/test1", test1)
rr.POST("/doLogin", doLogin)
return
}
func test(c *gin.Context) {
b := "eWgaeStLsS3Kh/M2oaL3rEzndYZtk5mdnEGyKScOuD40iUnC/a4I0N6gRhNbHRrQplZixM8C0Ng8B3gsy70Abg=="
enb, _ := base64.StdEncoding.DecodeString(b)
decryptPwd, err := RsaUtil.RsaDecrypt(enb)
if err != nil {
c.JSON(http.StatusOK, gin.H{"sucess": false, "info": "无法解密!"})
return
}
c.JSON(http.StatusOK, gin.H{"sucess": true, "info": string(decryptPwd)})
}
func test1(c *gin.Context) {
redirectUri := c.Query("redirect_uri")
token := CommonUtil.GetUUID()
RedisUtil.SET(token, "1", 3000)
c.SetCookie("token", token, 0, "/", "", false, true)
c.Redirect(http.StatusMovedPermanently, redirectUri)
}
func doLogin(c *gin.Context) {
user := c.PostForm("user")
pwd := c.PostForm("pwd")
if user == "" || pwd == "" {
c.JSON(http.StatusOK, gin.H{"sucess": false, "info": "用户名或密码不允许为空!"})
return
}
//RSA解密密码
base64Pwd, _ := base64.StdEncoding.DecodeString(pwd)
decryptPwdByte, err := RsaUtil.RsaDecrypt(base64Pwd)
if err != nil {
c.JSON(http.StatusOK, gin.H{"sucess": false, "info": "用户名或密码不正确!"})
return
}
//真实密码
realPwd := string(decryptPwdByte)
//验证密码
if realPwd != ConfigUtil.UserPwd {
c.JSON(http.StatusOK, gin.H{"sucess": false, "info": "用户名或密码不正确!"})
return
}
//生成token
token := CommonUtil.GetUUID()
//将token放到redis设置30分钟过期
RedisUtil.SET(token, "1", 1800)
//写cookie
c.SetCookie("token", token, 0, "/", "", false, true)
c.JSON(http.StatusOK, gin.H{"sucess": true, "info": "登录成功!"})
}