Browse Source

调整token

terry 2 years ago
parent
commit
6cfd0f2a1c
3 changed files with 10 additions and 4 deletions
  1. 8 1
      app/middleware/checkAuth.go
  2. 0 1
      app/middleware/token.go
  3. 2 2
      app/system/controller/token.go

+ 8 - 1
app/middleware/checkAuth.go

@@ -1,6 +1,7 @@
 package middleware
 
 import (
+	"fmt"
 	"github.com/gin-gonic/gin"
 	"iot_manager_service/util"
 	"net/http"
@@ -10,17 +11,23 @@ import (
 func CheckAuth() gin.HandlerFunc {
 	return func(ctx *gin.Context) {
 		//该路由下不校验token
-		if strings.Contains(ctx.Request.RequestURI, "/login") {
+		if strings.Contains(ctx.Request.RequestURI, "/login") ||
+			strings.Contains(ctx.Request.RequestURI, "/captcha") ||
+			strings.Contains(ctx.Request.RequestURI, "/tenant/info") ||
+			strings.Contains(ctx.Request.RequestURI, "/token") {
 			ctx.Next()
 			return
 		}
 
 		authorization := ctx.GetHeader(Authorization)
+		fmt.Printf("CheckAuth authorization = %s \n", authorization)
 		if authorization != "" {
 			token := parseAccessToken(authorization)
 			if token != nil {
 				ctx.Set(Authorization, token)
 				ctx.Next()
+				fmt.Printf("CheckAuth next \n")
+				return
 			}
 		}
 		ctx.JSON(http.StatusUnauthorized, util.NormalResponse(http.StatusUnauthorized, "token is invalid", nil))

+ 0 - 1
app/middleware/token.go

@@ -19,7 +19,6 @@ type Claims struct {
 	UserId   int64  `json:"user_id"`
 	RoleId   int64  `json:"role_id"`
 	UserName string `json:"user_name"`
-	NickName string `json:"nick_name"`
 	Random   string `json:"random"`
 }
 

+ 2 - 2
app/system/controller/token.go

@@ -196,8 +196,8 @@ func captchaGrant(token model.Token, ctx *gin.Context) (*model.UserInfo, *util.E
 
 	if token.UserName != "" && token.Password != "" {
 		// 获取租户信息
-		tenant, _ := service.TenantService.GetOne(token.TenantId)
-		if tenant == nil {
+		tenant, err := service.TenantService.GetOne(token.TenantId)
+		if err != nil || tenant == nil {
 			return nil, util.NormalResponse(http.StatusOK, model.UserHasNoTenant, nil)
 		}
 		if judgeTenant(tenant) {