go gin 使用jwt 鉴权

本博客代码已发布在github: https://github.com/Huochengyan/myGoProject

第一步: 服务器生成Token

   

package api

import (
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"log"
	"myProject/corll"
	"myProject/models"
	"myProject/pkg/e"
	"myProject/pkg/util"
	"net/http"
)


/*
 获得一个Token
*/
func GetAuth(c *gin.Context) {
	username := c.Query("username")
	password := c.Query("password")

	valid := validation.Validation{}
	a := models.Auth{Username: username, Password: password}
	ok, _ := valid.Valid(&a)

	data := make(map[string]interface{})
	code := e.INVALID_PARAMS
	if ok {
		isExist := corll.GetUserByNameAndPassword(username, password) //models.CheckAuth(username, password)
		if isExist {
			token, err := util.GenerateToken(username, password)
			if err != nil {
				code = e.ERROR_AUTH_TOKEN
			} else {
				data["token"] = token

				code = e.SUCCESS
			}

		} else {
			code = e.ERROR_AUTH
		}
	} else {
		for _, err := range valid.Errors {
			log.Println(err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": data,
	})
}

 

你可能感兴趣的:(GO,实战web,mongo,jwt,go,Token,鉴权)