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

@@ -25,7 +25,7 @@ func InitConfig(configFile string) {
// Get 获取配置信息 // Get 获取配置信息
// //
// Get("framework.name") // Get("server.port")
func Get(key string) any { func Get(key string) any {
return v.Get(key) return v.Get(key)
} }

View File

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

View File

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

View File

@@ -24,6 +24,19 @@ import (
// 请将中间件放在最前置,对请求优先处理 // 请将中间件放在最前置,对请求优先处理
func CryptoApi(requestDecrypt, responseEncrypt bool) gin.HandlerFunc { func CryptoApi(requestDecrypt, responseEncrypt bool) gin.HandlerFunc {
return func(c *gin.Context) { 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注入 // 请求解密时对请求data注入
if requestDecrypt { if requestDecrypt {
method := c.Request.Method method := c.Request.Method

View File

@@ -3,6 +3,7 @@ package middleware
import ( import (
"strings" "strings"
"be.ems/src/framework/config"
AdminConstants "be.ems/src/framework/constants/admin" AdminConstants "be.ems/src/framework/constants/admin"
commonConstants "be.ems/src/framework/constants/common" commonConstants "be.ems/src/framework/constants/common"
"be.ems/src/framework/i18n" "be.ems/src/framework/i18n"
@@ -36,6 +37,21 @@ var URL_WHITE_LIST = []string{
// 同时匹配其中权限 "matchPerms": {"xxx"}, // 同时匹配其中权限 "matchPerms": {"xxx"},
func PreAuthorize(options map[string][]string) gin.HandlerFunc { func PreAuthorize(options map[string][]string) gin.HandlerFunc {
return func(c *gin.Context) { 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) language := ctxUtils.AcceptLanguage(c)
requestURI := c.Request.RequestURI requestURI := c.Request.RequestURI

View File

@@ -3,7 +3,7 @@ package controller
import ( import (
"fmt" "fmt"
"be.ems/src/framework/config" libGlobal "be.ems/lib/global"
"be.ems/src/framework/vo/result" "be.ems/src/framework/vo/result"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -21,8 +21,8 @@ type IndexController struct{}
// //
// GET / // GET /
func (s *IndexController) Handler(c *gin.Context) { func (s *IndexController) Handler(c *gin.Context) {
name := config.Get("framework.name").(string) name := "OMC"
version := config.Get("framework.version").(string) version := libGlobal.Version
// str := "欢迎使用%s核心网管理平台当前版本%s请通过前台地址访问。" // str := "欢迎使用%s核心网管理平台当前版本%s请通过前台地址访问。"
str := "Welcome to the %s Core Network Management Platform, current version: %s, please access via the frontend address." str := "Welcome to the %s Core Network Management Platform, current version: %s, please access via the frontend address."
c.JSON(200, result.OkMsg(fmt.Sprintf(str, name, version))) c.JSON(200, result.OkMsg(fmt.Sprintf(str, name, version)))

View File

@@ -30,7 +30,6 @@ func (s *CommontImpl) SystemConfigInfo() map[string]string {
// 获取打包注入的全局变量信息 // 获取打包注入的全局变量信息
infoMap["version"] = global.Version infoMap["version"] = global.Version
infoMap["buildTime"] = global.BuildTime infoMap["buildTime"] = global.BuildTime
infoMap["goVer"] = global.GoVer
// 系统首次使用标记 // 系统首次使用标记
launchInfo := machine.LaunchInfo launchInfo := machine.LaunchInfo
if launchInfo != nil { if launchInfo != nil {
@@ -42,6 +41,8 @@ func (s *CommontImpl) SystemConfigInfo() map[string]string {
} else { } else {
infoMap[common.LAUNCH_BOOTLOADER] = "true" infoMap[common.LAUNCH_BOOTLOADER] = "true"
} }
// 用户登录认证
infoMap["loginAuth"] = fmt.Sprint(config.Get("user.loginAuth"))
// 序列号 // 序列号
infoMap["serialNum"] = fmt.Sprint(config.Get("omc.sn")) infoMap["serialNum"] = fmt.Sprint(config.Get("omc.sn"))
// 获取LOGO类型 // 获取LOGO类型