修改,兼容go-admin sdk方案

This commit is contained in:
linwenxiang committed 2021-03-09 19:12:49 +08:00
1 parent c541d09076
commit 25d8364bf4
151 files changed
+578 -3525

No files matched your search

+2 -2
View File
@@ -3,9 +3,9 @@ package middleware
import (
"time"
"github.com/go-admin-team/go-admin-core/sdk/config"
jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth"
"go-admin/app/admin/middleware/handler"
jwt "go-admin/pkg/jwtauth"
"go-admin/tools/config"
)
// AuthInit jwt验证new
+14 -13
View File
@@ -1,19 +1,20 @@
package handler
import (
"go-admin/common/apis"
"net/http"
"github.com/gin-gonic/gin"
"github.com/go-admin-team/go-admin-core/sdk/config"
"github.com/go-admin-team/go-admin-core/sdk/pkg"
jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth"
"github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
"github.com/go-admin-team/go-admin-core/sdk/pkg/response"
"github.com/mojocn/base64Captcha"
"github.com/mssola/user_agent"
"go-admin/app/admin/models/system"
"go-admin/app/admin/service"
jwt "go-admin/pkg/jwtauth"
"go-admin/tools"
"go-admin/tools/app"
"go-admin/tools/config"
"go-admin/common/apis"
)
var store = base64Captcha.DefaultMemStore
@@ -60,7 +61,7 @@ func IdentityHandler(c *gin.Context) interface{} {
// @Router /login [post]
func Authenticator(c *gin.Context) (interface{}, error) {
log := apis.GetRequestLogger(c)
db, err := tools.GetOrm(c)
db, err := pkg.GetOrm(c)
if err != nil {
log.Errorf("get db error, %s", err.Error())
app.Error(c, http.StatusInternalServerError, err, "数据库连接获取失败")
@@ -109,16 +110,16 @@ func LoginLogToDB(c *gin.Context, status string, msg string, username string) {
log := apis.GetRequestLogger(c)
if config.LoggerConfig.EnabledDB {
var loginLog system.SysLoginLog
db, err := tools.GetOrm(c)
db, err := pkg.GetOrm(c)
if err != nil {
log.Errorf("获取Orm失败, error:%s", err)
}
ua := user_agent.New(c.Request.UserAgent())
loginLog.Ipaddr = c.ClientIP()
loginLog.Username = username
location := tools.GetLocation(c.ClientIP())
location := pkg.GetLocation(c.ClientIP())
loginLog.LoginLocation = location
loginLog.LoginTime = tools.GetCurrentTime()
loginLog.LoginTime = pkg.GetCurrentTime()
loginLog.Status = status
loginLog.Remark = c.Request.UserAgent()
browserName, browserVersion := ua.Browser()
@@ -146,18 +147,18 @@ func LogOut(c *gin.Context) {
var loginLog system.SysLoginLog
ua := user_agent.New(c.Request.UserAgent())
loginLog.Ipaddr = c.ClientIP()
location := tools.GetLocation(c.ClientIP())
location := pkg.GetLocation(c.ClientIP())
loginLog.LoginLocation = location
loginLog.LoginTime = tools.GetCurrentTime()
loginLog.LoginTime = pkg.GetCurrentTime()
loginLog.Status = "2"
loginLog.Remark = c.Request.UserAgent()
browserName, browserVersion := ua.Browser()
loginLog.Browser = browserName + " " + browserVersion
loginLog.Os = ua.OS()
loginLog.Platform = ua.Platform()
loginLog.Username = tools.GetUserName(c)
loginLog.Username = user.GetUserName(c)
loginLog.Msg = "退出成功"
db, err := tools.GetOrm(c)
db, err := pkg.GetOrm(c)
if err != nil {
log.Errorf("获取Orm失败, error:%s", err)
}
+1 -1
View File
@@ -4,7 +4,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/unrolled/secure"
"go-admin/tools/config"
"github.com/go-admin-team/go-admin-core/sdk/config"
)
func TlsHandler() gin.HandlerFunc {
+11 -10
View File
@@ -2,19 +2,20 @@ package middleware
import (
"fmt"
"go-admin/common/apis"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/go-admin-team/go-admin-core/sdk/config"
"github.com/go-admin-team/go-admin-core/sdk/pkg"
"github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
"github.com/go-admin-team/go-admin-core/sdk/pkg/response"
"go-admin/app/admin/models"
"go-admin/app/admin/models/system"
"go-admin/app/admin/service"
"go-admin/tools"
"go-admin/tools/app"
"go-admin/tools/config"
"go-admin/common/apis"
)
// LoggerToFile 日志记录到文件
@@ -79,7 +80,7 @@ func LoggerToFile() gin.HandlerFunc {
// SetDBOperLog 写入操作日志表 fixme 该方法后续即将弃用
func SetDBOperLog(c *gin.Context, clientIP string, statusCode int, reqUri string, reqMethod string, latencyTime time.Duration, body string, result string, status int) {
log := apis.GetRequestLogger(c)
db, err := tools.GetOrm(c)
db, err := pkg.GetOrm(c)
if err != nil {
log.Errorf("get db connection error, %s", err.Error())
app.Error(c, http.StatusInternalServerError, err, "数据库连接获取失败")
@@ -92,9 +93,9 @@ func SetDBOperLog(c *gin.Context, clientIP string, statusCode int, reqUri string
menuList, _ := menu.Get(db)
sysOperaLog := system.SysOperaLog{}
sysOperaLog.OperIp = clientIP
sysOperaLog.OperLocation = tools.GetLocation(clientIP)
sysOperaLog.Status = tools.IntToString(statusCode)
sysOperaLog.OperName = tools.GetUserName(c)
sysOperaLog.OperLocation = pkg.GetLocation(clientIP)
sysOperaLog.Status = pkg.IntToString(statusCode)
sysOperaLog.OperName = user.GetUserName(c)
sysOperaLog.RequestMethod = c.Request.Method
sysOperaLog.OperUrl = reqUri
sysOperaLog.OperParam = body
@@ -120,8 +121,8 @@ func SetDBOperLog(c *gin.Context, clientIP string, statusCode int, reqUri string
if len(menuList) > 0 {
sysOperaLog.Title = menuList[0].Title
}
sysOperaLog.CreateBy = tools.GetUserId(c)
sysOperaLog.OperTime = tools.GetCurrentTime()
sysOperaLog.CreateBy = user.GetUserId(c)
sysOperaLog.OperTime = pkg.GetCurrentTime()
sysOperaLog.LatencyTime = fmt.Sprintf("%v", latencyTime)
sysOperaLog.JsonResult = result
-48
View File
@@ -1,48 +0,0 @@
package middleware
import (
"go-admin/common/apis"
"net/http"
"github.com/gin-gonic/gin"
"go-admin/pkg/jwtauth"
"go-admin/tools/app"
)
//权限检查中间件
func AuthCheckRole() gin.HandlerFunc {
return func(c *gin.Context) {
log := apis.GetRequestLogger(c)
data, _ := c.Get(jwtauth.JwtPayloadKey)
v := data.(jwtauth.MapClaims)
e := app.Runtime.GetCasbinKey(c.Request.Host)
var res bool
var err error
//检查权限
if v["rolekey"] == "admin" {
res = true
log.Infof("info:%s method:%s path:%s", v["rolekey"], c.Request.Method, c.Request.URL.Path)
} else {
res, err = e.Enforce(v["rolekey"], c.Request.URL.Path, c.Request.Method)
if err != nil {
log.Errorf("AuthCheckRole error:%s method:%s path:%s", err, c.Request.Method, c.Request.URL.Path)
app.Error(c, 500, err, "")
return
}
}
if res {
log.Infof("isTrue: %v role: %s method: %s path: %s", res, v["rolekey"], c.Request.Method, c.Request.URL.Path)
c.Next()
} else {
log.Warnf("isTrue: %v role: %s method: %s path: %s message: %s", res, v["rolekey"], c.Request.Method, c.Request.URL.Path, "当前request无权限,请管理员确认!")
c.JSON(http.StatusOK, gin.H{
"code": 403,
"msg": "对不起,您没有该接口访问权限,请联系管理员",
})
c.Abort()
return
}
}
}