From cb04f8320ae7dcdbdebbbe9a664275a327b00566 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E5=85=A8?= Date: Tue, 24 Dec 2019 14:56:05 +0800 Subject: [PATCH] =?UTF-8?q?v1.4=20=20=E5=A2=9E=E5=8A=A0userInfo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- conf/rbac_model.conf | 3 ++- docs/docs.go | 4 ++-- docs/swagger/swagger.json | 2 +- docs/swagger/swagger.yaml | 2 +- main.go | 2 +- pkg/util/jwt.go | 2 +- routers/api/user.go | 10 ++++++---- routers/router.go | 1 - test/main.go | 10 ++++++++++ 10 files changed, 26 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index adb068b..b4a15a9 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Go Web Admin ![构建](https://travis-ci.org/hequan2017/go-admin.svg?branch=master) -![版本](https://img.shields.io/badge/release-1.2.2-blue.svg) +![版本](https://img.shields.io/badge/release-1.4.0-blue.svg) ![语言](https://img.shields.io/badge/language-goland1.2-blue.svg) ![base](https://img.shields.io/badge/base-gin-blue.svg) ![base](https://img.shields.io/badge/base-casbin-blue.svg) @@ -66,7 +66,7 @@ hequan 运维部 用户 admin 有所有的权限,不进行权限匹配 -登录接口 /auth 不进行验证 +登录接口 /auth /api/v1/userInfo 不进行验证 ``` ## 请求 diff --git a/conf/rbac_model.conf b/conf/rbac_model.conf index 7cdad42..dc68101 100644 --- a/conf/rbac_model.conf +++ b/conf/rbac_model.conf @@ -15,4 +15,5 @@ m = g(r.sub, p.sub) == true \ && keyMatch2(r.obj, p.obj) == true \ && regexMatch(r.act, p.act) == true \ || r.sub == "admin" \ - || keyMatch2(r.obj, "/auth") == true \ \ No newline at end of file + || keyMatch2(r.obj, "/auth") == true \ + || keyMatch2(r.obj, "/api/v1/userInfo") == true \ No newline at end of file diff --git a/docs/docs.go b/docs/docs.go index 702efba..2029051 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -1,6 +1,6 @@ // GENERATED BY THE COMMAND ABOVE; DO NOT EDIT // This file was generated by swaggo/swag at -// 2019-12-24 12:19:55.4766668 +0800 CST m=+0.090628501 +// 2019-12-24 14:55:53.8650092 +0800 CST m=+0.083769601 package docs @@ -23,7 +23,7 @@ var doc = `{ "name": "MIT", "url": "https://github.com/hequan2017/go-admin/blob/master/LICENSE" }, - "version": "1.2.2" + "version": "1.4.0" }, "host": "127.0.0.1:8000", "paths": { diff --git a/docs/swagger/swagger.json b/docs/swagger/swagger.json index 684c79e..434bd0b 100644 --- a/docs/swagger/swagger.json +++ b/docs/swagger/swagger.json @@ -13,7 +13,7 @@ "name": "MIT", "url": "https://github.com/hequan2017/go-admin/blob/master/LICENSE" }, - "version": "1.2.2" + "version": "1.4.0" }, "host": "127.0.0.1:8000", "paths": { diff --git a/docs/swagger/swagger.yaml b/docs/swagger/swagger.yaml index e81c208..897ddf7 100644 --- a/docs/swagger/swagger.yaml +++ b/docs/swagger/swagger.yaml @@ -71,7 +71,7 @@ info: url: https://github.com/hequan2017/go-admin/blob/master/LICENSE termsOfService: https://github.com/hequan2017/go-admin title: go-admin - version: 1.2.2 + version: 1.4.0 paths: /api/v1/menus: get: diff --git a/main.go b/main.go index 0053b65..e38456d 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,7 @@ import ( ) // @title go-admin -// @version 1.2.2 +// @version 1.4.0 // @description go-admin // @termsOfService https://github.com/hequan2017/go-admin diff --git a/pkg/util/jwt.go b/pkg/util/jwt.go index f21facb..d9d7a24 100644 --- a/pkg/util/jwt.go +++ b/pkg/util/jwt.go @@ -19,7 +19,7 @@ type Claims struct { func GenerateToken(username, password string) (string, error) { nowTime := time.Now() - expireTime := nowTime.Add(3 * time.Hour) + expireTime := nowTime.Add(24 * time.Hour) claims := Claims{ username, diff --git a/routers/api/user.go b/routers/api/user.go index 09f38a2..8d6c4f6 100644 --- a/routers/api/user.go +++ b/routers/api/user.go @@ -1,7 +1,6 @@ package api import ( - "fmt" "github.com/astaxie/beego/validation" "github.com/dgrijalva/jwt-go" "github.com/gin-gonic/gin" @@ -134,10 +133,14 @@ func GetUserInfo(c *gin.Context) { appG := app.Gin{C: c} Authorization := c.GetHeader("Authorization") token := strings.Split(Authorization, " ") - t, _ := jwt.Parse(token[1], func(*jwt.Token) (interface{}, error) { + t, err := jwt.Parse(token[1], func(*jwt.Token) (interface{}, error) { return jwtGet.JwtSecret, nil }) - fmt.Print(jwtGet.GetIdFromClaims("username", t.Claims)) + + if err != nil { + appG.Response(http.StatusInternalServerError, e.ERROR_AUTH, nil) + return + } userService := user_service.User{ Username: jwtGet.GetIdFromClaims("username", t.Claims), @@ -170,7 +173,6 @@ func GetUserInfo(c *gin.Context) { for _, v2 := range r.Menu { menus = append(menus, v2.Name) } - fmt.Print(menus) } menus = util.RemoveRepByMap(menus) v.Password = strings.Join(menus, ",") diff --git a/routers/router.go b/routers/router.go index 8eb93d5..7e02777 100644 --- a/routers/router.go +++ b/routers/router.go @@ -31,7 +31,6 @@ func InitRouter() *gin.Engine { apiV1 := r.Group("/api/v1") apiV1.Use(jwt.JWT()) // token 验证 - apiV1.GET("/userInfo", api.GetUserInfo) apiV1.Use(permission.CasbinMiddleware()) // 权限 验证 diff --git a/test/main.go b/test/main.go index 06ab7d0..02c176a 100644 --- a/test/main.go +++ b/test/main.go @@ -1 +1,11 @@ package main + +import ( + "fmt" +) + +func main() { + fruitarray := [...]string{"apple", "orange", "grape", "mango", "water melon", "pine apple", "chikoo"} + fruitslice := fruitarray[2:3] + fmt.Printf("length of slice %d capacity %d", len(fruitslice), cap(fruitslice)) // length of is 2 and capacity is 6 +}