mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-03 16:00:25 +08:00
refactor: oauth2登录优化
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package constant
|
||||
package consts
|
||||
|
||||
import "time"
|
||||
|
||||
@@ -3,7 +3,7 @@ package application
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"mayfly-go/internal/constant"
|
||||
"mayfly-go/internal/common/consts"
|
||||
"mayfly-go/internal/db/domain/entity"
|
||||
"mayfly-go/internal/db/domain/repository"
|
||||
"mayfly-go/internal/machine/infrastructure/machine"
|
||||
@@ -294,7 +294,7 @@ func (d *DbInstance) Close() {
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
// 客户端连接缓存,指定时间内没有访问则会被关闭, key为数据库实例id:数据库
|
||||
var dbCache = cache.NewTimedCache(constant.DbConnExpireTime, 5*time.Second).
|
||||
var dbCache = cache.NewTimedCache(consts.DbConnExpireTime, 5*time.Second).
|
||||
WithUpdateAccessTime(true).
|
||||
OnEvicted(func(key any, value any) {
|
||||
global.Log.Info(fmt.Sprintf("删除db连接缓存 id = %s", key))
|
||||
|
||||
@@ -3,7 +3,7 @@ package machine
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"mayfly-go/internal/constant"
|
||||
"mayfly-go/internal/common/consts"
|
||||
"mayfly-go/internal/machine/domain/entity"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/cache"
|
||||
@@ -139,7 +139,7 @@ func (c *Cli) GetMachine() *Info {
|
||||
}
|
||||
|
||||
// 机器客户端连接缓存,指定时间内没有访问则会被关闭
|
||||
var cliCache = cache.NewTimedCache(constant.MachineConnExpireTime, 5*time.Second).
|
||||
var cliCache = cache.NewTimedCache(consts.MachineConnExpireTime, 5*time.Second).
|
||||
WithUpdateAccessTime(true).
|
||||
OnEvicted(func(_, value any) {
|
||||
value.(*Cli).Close()
|
||||
|
||||
@@ -3,7 +3,7 @@ package application
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"mayfly-go/internal/constant"
|
||||
"mayfly-go/internal/common/consts"
|
||||
machineapp "mayfly-go/internal/machine/application"
|
||||
"mayfly-go/internal/machine/infrastructure/machine"
|
||||
"mayfly-go/internal/mongo/domain/entity"
|
||||
@@ -101,7 +101,7 @@ func (d *mongoAppImpl) GetMongoCli(id uint64) *mongo.Client {
|
||||
// -----------------------------------------------------------
|
||||
|
||||
// mongo客户端连接缓存,指定时间内没有访问则会被关闭
|
||||
var mongoCliCache = cache.NewTimedCache(constant.MongoConnExpireTime, 5*time.Second).
|
||||
var mongoCliCache = cache.NewTimedCache(consts.MongoConnExpireTime, 5*time.Second).
|
||||
WithUpdateAccessTime(true).
|
||||
OnEvicted(func(key any, value any) {
|
||||
global.Log.Info("删除mongo连接缓存: id = ", key)
|
||||
|
||||
@@ -3,7 +3,7 @@ package application
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"mayfly-go/internal/constant"
|
||||
"mayfly-go/internal/common/consts"
|
||||
machineapp "mayfly-go/internal/machine/application"
|
||||
"mayfly-go/internal/machine/infrastructure/machine"
|
||||
"mayfly-go/internal/redis/domain/entity"
|
||||
@@ -254,7 +254,7 @@ func getRedisDialer(machineId int) func(ctx context.Context, network, addr strin
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
// redis客户端连接缓存,指定时间内没有访问则会被关闭
|
||||
var redisCache = cache.NewTimedCache(constant.RedisConnExpireTime, 5*time.Second).
|
||||
var redisCache = cache.NewTimedCache(consts.RedisConnExpireTime, 5*time.Second).
|
||||
WithUpdateAccessTime(true).
|
||||
OnEvicted(func(key any, value any) {
|
||||
global.Log.Info(fmt.Sprintf("删除redis连接缓存 id = %s", key))
|
||||
|
||||
@@ -53,5 +53,6 @@ func (c *Config) Oauth2Config(rc *req.Ctx) {
|
||||
oauth2LoginConfig := c.ConfigApp.GetConfig(entity.ConfigKeyOauth2Login).ToOauth2Login()
|
||||
rc.ResData = map[string]any{
|
||||
"enable": oauth2LoginConfig.Enable,
|
||||
"name": oauth2LoginConfig.Name,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ func (c *Config) BoolValue(defaultValue bool) bool {
|
||||
if c.Id == 0 {
|
||||
return defaultValue
|
||||
}
|
||||
return convertBool(c.Value, defaultValue)
|
||||
return c.ConvBool(c.Value, defaultValue)
|
||||
}
|
||||
|
||||
// 值返回json map
|
||||
@@ -52,7 +52,7 @@ func (c *Config) IntValue(defaultValue int) int {
|
||||
if c.Id == 0 {
|
||||
return defaultValue
|
||||
}
|
||||
return convertInt(c.Value, defaultValue)
|
||||
return c.ConvInt(c.Value, defaultValue)
|
||||
}
|
||||
|
||||
type AccountLoginSecurity struct {
|
||||
@@ -67,10 +67,10 @@ type AccountLoginSecurity struct {
|
||||
func (c *Config) ToAccountLoginSecurity() *AccountLoginSecurity {
|
||||
jm := c.GetJsonMap()
|
||||
als := new(AccountLoginSecurity)
|
||||
als.UseCaptcha = convertBool(jm["useCaptcha"], true)
|
||||
als.UseOtp = convertBool(jm["useOtp"], false)
|
||||
als.LoginFailCount = convertInt(jm["loginFailCount"], 5)
|
||||
als.LoginFailMin = convertInt(jm["loginFailMin"], 10)
|
||||
als.UseCaptcha = c.ConvBool(jm["useCaptcha"], true)
|
||||
als.UseOtp = c.ConvBool(jm["useOtp"], false)
|
||||
als.LoginFailCount = c.ConvInt(jm["loginFailCount"], 5)
|
||||
als.LoginFailMin = c.ConvInt(jm["loginFailMin"], 10)
|
||||
otpIssuer := jm["otpIssuer"]
|
||||
if otpIssuer == "" {
|
||||
otpIssuer = "mayfly-go"
|
||||
@@ -80,7 +80,8 @@ func (c *Config) ToAccountLoginSecurity() *AccountLoginSecurity {
|
||||
}
|
||||
|
||||
type ConfigOauth2Login struct {
|
||||
Enable bool // 是否启用
|
||||
Enable bool // 是否启用
|
||||
Name string
|
||||
ClientId string `json:"clientId"`
|
||||
ClientSecret string `json:"clientSecret"`
|
||||
AuthorizationURL string `json:"authorizationURL"`
|
||||
@@ -96,7 +97,8 @@ type ConfigOauth2Login struct {
|
||||
func (c *Config) ToOauth2Login() *ConfigOauth2Login {
|
||||
jm := c.GetJsonMap()
|
||||
ol := new(ConfigOauth2Login)
|
||||
ol.Enable = convertBool(jm["enable"], false)
|
||||
ol.Enable = c.ConvBool(jm["enable"], false)
|
||||
ol.Name = jm["name"]
|
||||
ol.ClientId = jm["clientId"]
|
||||
ol.ClientSecret = jm["clientSecret"]
|
||||
ol.AuthorizationURL = jm["authorizationURL"]
|
||||
@@ -105,12 +107,12 @@ func (c *Config) ToOauth2Login() *ConfigOauth2Login {
|
||||
ol.Scopes = jm["scopes"]
|
||||
ol.ResourceURL = jm["resourceURL"]
|
||||
ol.UserIdentifier = jm["userIdentifier"]
|
||||
ol.AutoRegister = convertBool(jm["autoRegister"], true)
|
||||
ol.AutoRegister = c.ConvBool(jm["autoRegister"], true)
|
||||
return ol
|
||||
}
|
||||
|
||||
// 转换配置中的值为bool类型(默认"1"或"true"为true,其他为false)
|
||||
func convertBool(value string, defaultValue bool) bool {
|
||||
func (c *Config) ConvBool(value string, defaultValue bool) bool {
|
||||
if value == "" {
|
||||
return defaultValue
|
||||
}
|
||||
@@ -118,7 +120,7 @@ func convertBool(value string, defaultValue bool) bool {
|
||||
}
|
||||
|
||||
// 转换配置值中的值为int
|
||||
func convertInt(value string, defaultValue int) int {
|
||||
func (c *Config) ConvInt(value string, defaultValue int) int {
|
||||
if value == "" {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user