package Services import ( "fmt" "github.com/gin-gonic/gin" "github.com/golang-jwt/jwt/v5" Mysql "goLoad1/Databases" "goLoad1/Models/loginMondel" "net/http" "time" ) func LoginCheck(name string, password string, c *gin.Context) map[string]bool { status := map[string]bool{ "nameStatus": true, "passwordStatus": true, } if len(name) == 0 { c.JSON(http.StatusUnprocessableEntity, gin.H{ "code": 422, "message": "用户名不能为空", }) status["nameStatus"] = false } if len(password) == 0 { c.JSON(http.StatusUnprocessableEntity, gin.H{ "code": 422, "message": "密码不能为空", }) status["passwordStatus"] = false } return status } func QueryUser(c *gin.Context) { var data []loginMondel.Login Mysql.DB.Find(&data) c.JSON(http.StatusOK, data) for _, val := range data { fmt.Print(val) } } // MyCustomClaims ................................................... // MyCustomClaims 1.自定义声明类型 type MyCustomClaims struct { UserName string `json:"username"` Password string `json:"password"` exp string jwt.RegisteredClaims } var ( // SecretKey 假设这是你的签名密钥 SecretKey = []byte("your-secret-key") ) // GenerateToken 创建Token func GenerateToken(name string, password string) (string, error) { token := jwt.New(jwt.SigningMethodHS256) claims := token.Claims.(jwt.MapClaims) claims["UserName"] = name claims["Password"] = password claims["exp"] = time.Now().Add(time.Hour * 1).Unix() // Token有效期1小时 return token.SignedString(SecretKey) }