feat: 系统用户免登录认证,默认为admin操作所有接口

This commit is contained in:
TsMask
2024-11-25 11:58:08 +08:00
parent 1588cbaedd
commit 75de667f7a
7 changed files with 40 additions and 11 deletions

View File

@@ -142,7 +142,7 @@ func RunTime() time.Time {
// Get 获取配置信息
//
// Get("framework.name")
// Get("server.port")
func Get(key string) any {
return viper.Get(key)
}

View File

@@ -1,8 +1,3 @@
# 项目信息
framework:
name: "OMC"
version: "2.2411.3"
# 应用服务配置
server:
# 服务端口
@@ -181,6 +176,10 @@ aes:
# 用户配置
user:
# 登录认证,默认打开
loginAuth: true
# 接口加密,默认打开
cryptoApi: true
# 密码
password:
# 密码最大错误次数

View File

@@ -24,6 +24,19 @@ import (
// 请将中间件放在最前置,对请求优先处理
func CryptoApi(requestDecrypt, responseEncrypt bool) gin.HandlerFunc {
return func(c *gin.Context) {
// 登录认证,默认打开
enable := true
if v := config.Get("user.loginAuth"); v != nil {
enable = v.(bool)
}
if v := config.Get("user.cryptoApi"); v != nil && enable {
enable = v.(bool)
}
if !enable {
c.Next()
return
}
// 请求解密时对请求data注入
if requestDecrypt {
method := c.Request.Method

View File

@@ -3,6 +3,7 @@ package middleware
import (
"strings"
"be.ems/src/framework/config"
AdminConstants "be.ems/src/framework/constants/admin"
commonConstants "be.ems/src/framework/constants/common"
"be.ems/src/framework/i18n"
@@ -36,6 +37,21 @@ var URL_WHITE_LIST = []string{
// 同时匹配其中权限 "matchPerms": {"xxx"},
func PreAuthorize(options map[string][]string) gin.HandlerFunc {
return func(c *gin.Context) {
// 登录认证,默认打开
enable := true
if v := config.Get("user.loginAuth"); v != nil {
enable = v.(bool)
}
if !enable {
loginUser, _ := ctxUtils.LoginUser(c)
loginUser.UserID = "2"
loginUser.User.UserID = "2"
loginUser.User.UserName = "admin"
c.Set(commonConstants.CTX_LOGIN_USER, loginUser)
c.Next()
return
}
language := ctxUtils.AcceptLanguage(c)
requestURI := c.Request.RequestURI