parent
8f82600347
commit
761ad266f4
@ -0,0 +1,44 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"expenses/model"
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"time"
|
||||
)
|
||||
|
||||
var jwtKey = []byte("a_secret_key")
|
||||
|
||||
type Claims struct {
|
||||
UserId uint
|
||||
jwt.StandardClaims
|
||||
}
|
||||
|
||||
func ReleaseToken(user model.User) (string, error) {
|
||||
// 设置Token过期时间
|
||||
expirationTime := time.Now().Add(7 * 24 * time.Hour)
|
||||
claims := Claims{
|
||||
UserId: user.ID,
|
||||
StandardClaims: jwt.StandardClaims{
|
||||
ExpiresAt: expirationTime.Unix(),
|
||||
IssuedAt: time.Now().Unix(),
|
||||
Issuer: "github.com/zggsong/expenses",
|
||||
Subject: "user token",
|
||||
},
|
||||
}
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
tokenString, err := token.SignedString(jwtKey)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return tokenString, nil
|
||||
}
|
||||
|
||||
func ParseToken(tokenString string) (*jwt.Token, *Claims, error) {
|
||||
claims := &Claims{}
|
||||
|
||||
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
|
||||
return jwtKey, nil
|
||||
})
|
||||
return token, claims, err
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Response(ctx *gin.Context, httpStatus int, code int, data gin.H, msg string) {
|
||||
ctx.JSON(httpStatus, gin.H{
|
||||
"code": code,
|
||||
"data": data,
|
||||
"msg": msg,
|
||||
})
|
||||
}
|
||||
|
||||
func Success(ctx *gin.Context, data gin.H, msg string) {
|
||||
Response(ctx, http.StatusOK, 200, data, msg)
|
||||
}
|
||||
|
||||
func Fail(ctx *gin.Context, data gin.H, msg string) {
|
||||
Response(ctx, http.StatusOK, 400, data, msg)
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"expenses/common"
|
||||
"expenses/dto"
|
||||
"expenses/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Info(ctx *gin.Context) {
|
||||
user, _ := ctx.Get("user")
|
||||
common.Success(ctx, gin.H{"user": dto.ToUserDto(user.(model.User))}, "")
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"expenses/common"
|
||||
"expenses/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Login(ctx *gin.Context) {
|
||||
DB := common.GetDB()
|
||||
// 获取参数
|
||||
telephone := ctx.Query("telephone")
|
||||
password := ctx.Query("password")
|
||||
|
||||
// 数据验证
|
||||
if len(telephone) != 11 {
|
||||
common.Fail(ctx, nil, "手机号必须是11位")
|
||||
return
|
||||
}
|
||||
if len(password) < 6 {
|
||||
common.Fail(ctx, nil, "密码必须大于6位")
|
||||
return
|
||||
}
|
||||
|
||||
// 判断手机号是否存在
|
||||
var user model.User
|
||||
DB.Where("telephone = ?", telephone).First(&user)
|
||||
if user.ID == 0 {
|
||||
common.Fail(ctx, nil, "用户不存在")
|
||||
return
|
||||
}
|
||||
|
||||
// 密码验证
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)); err != nil {
|
||||
common.Fail(ctx, nil, "密码错误")
|
||||
return
|
||||
}
|
||||
|
||||
// 发放Token
|
||||
token, err := common.ReleaseToken(user)
|
||||
if err != nil {
|
||||
common.Response(ctx, http.StatusInternalServerError, 500, nil, "发放Token失败")
|
||||
return
|
||||
}
|
||||
|
||||
// 返回结果
|
||||
common.Success(ctx, gin.H{"token": token}, "登录成功")
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"expenses/common"
|
||||
"expenses/model"
|
||||
"expenses/util"
|
||||
"github.com/gin-gonic/gin"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/gorm"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Register(ctx *gin.Context) {
|
||||
DB := common.GetDB()
|
||||
// 获取参数
|
||||
name := ctx.Query("name")
|
||||
telephone := ctx.Query("telephone")
|
||||
password := ctx.Query("password")
|
||||
|
||||
// 数据验证
|
||||
if len(telephone) != 11 {
|
||||
common.Fail(ctx, nil, "手机号必须是11位")
|
||||
return
|
||||
}
|
||||
if len(password) < 6 {
|
||||
common.Fail(ctx, nil, "密码必须大于6位")
|
||||
return
|
||||
}
|
||||
// 如果名字为空则返回10为随机字符串
|
||||
if name == "" {
|
||||
name = util.RandomString(10)
|
||||
}
|
||||
|
||||
// 判断手机号是否存在
|
||||
if isExistTelephone(DB, telephone) {
|
||||
common.Fail(ctx, nil, "用户已存在")
|
||||
return
|
||||
}
|
||||
|
||||
// 创建用户
|
||||
hasedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
common.Response(ctx, http.StatusInternalServerError, 500, nil, "密码加密失败")
|
||||
return
|
||||
}
|
||||
newUser := model.User{
|
||||
Name: name,
|
||||
Telephone: telephone,
|
||||
Password: string(hasedPassword),
|
||||
}
|
||||
DB.Create(&newUser)
|
||||
|
||||
// 返回结果
|
||||
common.Success(ctx, nil, "注册成功")
|
||||
}
|
||||
|
||||
func isExistTelephone(db *gorm.DB, telephone string) bool {
|
||||
var user model.User
|
||||
db.Where("telephone = ?", telephone).First(&user)
|
||||
return user.ID != 0
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package dto
|
||||
|
||||
import "expenses/model"
|
||||
|
||||
type UserDto struct {
|
||||
Name string `json:"name"`
|
||||
Telephone string `json:"telephone"`
|
||||
}
|
||||
|
||||
func ToUserDto(user model.User) UserDto {
|
||||
return UserDto{
|
||||
Name: user.Name,
|
||||
Telephone: user.Telephone,
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package global
|
||||
|
||||
import (
|
||||
"expenses/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var (
|
||||
GLO_CONF model.Config
|
||||
GLO_DB *gorm.DB
|
||||
)
|
@ -0,0 +1,63 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"expenses/common"
|
||||
"expenses/model"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// AuthMiddleWare
|
||||
//
|
||||
// @Description: 认证中间件
|
||||
// @return gin.HandlerFunc
|
||||
func AuthMiddleWare() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// 获取 authorization header
|
||||
tokenString := c.GetHeader("Authorization")
|
||||
|
||||
// 验证token
|
||||
if tokenString == "" || !strings.HasPrefix(tokenString, "Bearer ") {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": http.StatusUnauthorized,
|
||||
"message": "请求未授权",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
tokenString = tokenString[7:]
|
||||
|
||||
token, claims, err := common.ParseToken(tokenString)
|
||||
if err != nil || !token.Valid {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": http.StatusUnauthorized,
|
||||
"message": "请求未授权",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
// 验证通过后获取Claim中的UserId
|
||||
userId := claims.UserId
|
||||
DB := common.GetDB()
|
||||
var user model.User
|
||||
DB.First(&user, userId)
|
||||
|
||||
// 用户不存在
|
||||
if user.ID == 0 {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": http.StatusUnauthorized,
|
||||
"message": "请求未授权",
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
// 用户信息存在,则将用户信息存入上下文
|
||||
c.Set("user", user)
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
@ -1,10 +1,14 @@
|
||||
package router
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"expenses/controller"
|
||||
"expenses/middleware"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func CollectRoute(r *gin.Engine) *gin.Engine {
|
||||
//r.POST("/auth/register", controller.Register)
|
||||
//r.POST("/auth/login", controller.Login)
|
||||
//r.GET("/auth/info", middleware.AuthMiddleWare(), controller.Info) // 认证中间件保护info接口
|
||||
r.POST("/auth/register", controller.Register)
|
||||
r.POST("/auth/login", controller.Login)
|
||||
r.GET("/auth/info", middleware.AuthMiddleWare(), controller.Info) // 认证中间件保护info接口
|
||||
return r
|
||||
}
|
||||
|
@ -0,0 +1,22 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
// RandomString
|
||||
//
|
||||
// @Description: 随机字符串
|
||||
// @param length
|
||||
// @return string
|
||||
func RandomString(length int) string {
|
||||
str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
bytes := []byte(str)
|
||||
var result []byte
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
for i := 0; i < length; i++ {
|
||||
result = append(result, bytes[r.Intn(len(bytes))])
|
||||
}
|
||||
return string(result)
|
||||
}
|
Loading…
Reference in new issue