优化
This commit is contained in:
@@ -20,10 +20,10 @@ import (
|
||||
kratoshttp "github.com/go-kratos/kratos/v3/transport/http"
|
||||
)
|
||||
|
||||
func NewGinEngine(runtime *conf.Runtime, access *service.AccessService, authority *handler.Authority, menu *handler.Menu, api *handler.API, permission *handler.Permission, organization *handler.Organization, announcement *handler.Announcement, email *handler.Email, task *handler.Task, media *handler.Media, auditHandler *handler.Audit, export *handler.Export, version *handler.Version, dictionary *handler.Dictionary, parameter *handler.Parameter, apiToken *handler.APIToken, systemConfig *handler.SystemConfig, publicHandler *handler.Public, user *handler.User, navigation *handler.Navigation, session *handler.Session, settings *service.SettingsService, audit *service.AuditService, logger *slog.Logger) *gin.Engine {
|
||||
func NewGinEngine(runtime *conf.Runtime, access *service.AccessService, authority *handler.Authority, menu *handler.Menu, api *handler.API, permission *handler.Permission, organization *handler.Organization, announcement *handler.Announcement, email *handler.Email, task *handler.Task, media *handler.Media, auditHandler *handler.Audit, export *handler.Export, version *handler.Version, dictionary *handler.Dictionary, parameter *handler.Parameter, apiToken *handler.APIToken, systemConfig *handler.SystemConfig, publicHandler *handler.Public, user *handler.User, navigation *handler.Navigation, session *handler.Session, security *service.SecurityService, tokens *service.TokenService, audit *service.AuditService, logger *slog.Logger) *gin.Engine {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
engine := gin.New()
|
||||
engine.Use(servermiddleware.RequestMeta(), servermiddleware.Recovery(audit, logger), servermiddleware.AccessLog(runtime, logger), servermiddleware.ErrorAudit(audit), servermiddleware.SecurityRateLimit(settings), servermiddleware.OperationAudit(runtime, audit))
|
||||
engine.Use(servermiddleware.RequestMeta(), servermiddleware.Recovery(audit, logger), servermiddleware.AccessLog(runtime, logger), servermiddleware.ErrorAudit(audit), servermiddleware.SecurityRateLimit(security), servermiddleware.OperationAudit(runtime, audit))
|
||||
|
||||
prefix := ""
|
||||
config := runtime.Admin()
|
||||
@@ -35,7 +35,7 @@ func NewGinEngine(runtime *conf.Runtime, access *service.AccessService, authorit
|
||||
serverrouter.RegisterPublic(public, engine, publicHandler)
|
||||
|
||||
private := engine.Group(prefix)
|
||||
private.Use(servermiddleware.Auth(runtime, settings), servermiddleware.MustChangePassword(), servermiddleware.AccessControl(runtime, access, audit))
|
||||
private.Use(servermiddleware.Auth(runtime, security, tokens), servermiddleware.MustChangePassword(), servermiddleware.AccessControl(runtime, access, audit))
|
||||
serverrouter.RegisterUser(private, user)
|
||||
serverrouter.RegisterNavigation(private, navigation)
|
||||
serverrouter.RegisterSession(private, session)
|
||||
|
||||
@@ -9,10 +9,10 @@ import (
|
||||
)
|
||||
|
||||
type APIToken struct {
|
||||
service *service.SettingsService
|
||||
service *service.TokenService
|
||||
}
|
||||
|
||||
func NewAPIToken(service *service.SettingsService) *APIToken {
|
||||
func NewAPIToken(service *service.TokenService) *APIToken {
|
||||
return &APIToken{service: service}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,9 +11,9 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Dictionary struct{ service *service.SettingsService }
|
||||
type Dictionary struct{ service *service.DictionaryService }
|
||||
|
||||
func NewDictionary(service *service.SettingsService) *Dictionary {
|
||||
func NewDictionary(service *service.DictionaryService) *Dictionary {
|
||||
return &Dictionary{service: service}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,11 +16,11 @@ import (
|
||||
)
|
||||
|
||||
type Export struct {
|
||||
settings *service.SettingsService
|
||||
settings *service.SecurityService
|
||||
service *service.ExportService
|
||||
}
|
||||
|
||||
func NewExport(settings *service.SettingsService, export *service.ExportService) *Export {
|
||||
func NewExport(settings *service.SecurityService, export *service.ExportService) *Export {
|
||||
return &Export{settings: settings, service: export}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,10 +11,10 @@ import (
|
||||
)
|
||||
|
||||
type Parameter struct {
|
||||
service *service.SettingsService
|
||||
service *service.ParameterService
|
||||
}
|
||||
|
||||
func NewParameter(service *service.SettingsService) *Parameter {
|
||||
func NewParameter(service *service.ParameterService) *Parameter {
|
||||
return &Parameter{service: service}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,14 +21,14 @@ import (
|
||||
type Public struct {
|
||||
auth *service.AuthService
|
||||
system *service.SystemConfigService
|
||||
settings *service.SettingsService
|
||||
settings *service.SecurityService
|
||||
audit *service.AuditService
|
||||
scheduler *worker.TaskScheduler
|
||||
runtime *conf.Runtime
|
||||
store *captchaStore
|
||||
}
|
||||
|
||||
func NewPublic(runtime *conf.Runtime, auth *service.AuthService, system *service.SystemConfigService, settings *service.SettingsService, audit *service.AuditService, scheduler *worker.TaskScheduler) *Public {
|
||||
func NewPublic(runtime *conf.Runtime, auth *service.AuthService, system *service.SystemConfigService, settings *service.SecurityService, audit *service.AuditService, scheduler *worker.TaskScheduler) *Public {
|
||||
return &Public{auth: auth, system: system, settings: settings, audit: audit, scheduler: scheduler, runtime: runtime, store: &captchaStore{service: settings, runtime: runtime}}
|
||||
}
|
||||
|
||||
@@ -131,7 +131,7 @@ func (h *Public) Login(c *gin.Context) {
|
||||
httpx.Write(c, httpx.CodeSuccess, result, "登录成功")
|
||||
}
|
||||
|
||||
func ensureLoginIPCounter(ctx context.Context, settings *service.SettingsService, ip string, expiration time.Duration) (int, error) {
|
||||
func ensureLoginIPCounter(ctx context.Context, settings *service.SecurityService, ip string, expiration time.Duration) (int, error) {
|
||||
value, exists, err := settings.CacheGet(ctx, ip)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@@ -195,7 +195,7 @@ func (h *Public) InitializeDatabase(engine *gin.Engine) gin.HandlerFunc {
|
||||
}
|
||||
|
||||
type captchaStore struct {
|
||||
service *service.SettingsService
|
||||
service *service.SecurityService
|
||||
runtime *conf.Runtime
|
||||
}
|
||||
|
||||
|
||||
@@ -7,15 +7,15 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Session struct{ settings *service.SettingsService }
|
||||
type Session struct{ tokens *service.TokenService }
|
||||
|
||||
func NewSession(settings *service.SettingsService) *Session { return &Session{settings: settings} }
|
||||
func NewSession(tokens *service.TokenService) *Session { return &Session{tokens: tokens} }
|
||||
func (h *Session) Logout(c *gin.Context) {
|
||||
token := c.GetHeader("x-token")
|
||||
if token == "" {
|
||||
token, _ = c.Cookie("x-token")
|
||||
}
|
||||
if err := h.settings.BlacklistToken(c.Request.Context(), token); err != nil {
|
||||
if err := h.tokens.BlacklistToken(c.Request.Context(), token); err != nil {
|
||||
httpx.Fail(c, "jwt作废失败")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -17,11 +17,11 @@ import (
|
||||
|
||||
type SystemConfig struct {
|
||||
system *service.SystemConfigService
|
||||
settings *service.SettingsService
|
||||
settings *service.SecurityService
|
||||
scheduler *worker.TaskScheduler
|
||||
}
|
||||
|
||||
func NewSystemConfig(system *service.SystemConfigService, settings *service.SettingsService, scheduler *worker.TaskScheduler) *SystemConfig {
|
||||
func NewSystemConfig(system *service.SystemConfigService, settings *service.SecurityService, scheduler *worker.TaskScheduler) *SystemConfig {
|
||||
return &SystemConfig{system: system, settings: settings, scheduler: scheduler}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ type refreshedToken struct {
|
||||
expiresAt int64
|
||||
}
|
||||
|
||||
func Auth(runtime *conf.Runtime, settings *service.SettingsService) gin.HandlerFunc {
|
||||
func Auth(runtime *conf.Runtime, security *service.SecurityService, tokens *service.TokenService) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
token := c.GetHeader("x-token")
|
||||
if token == "" {
|
||||
@@ -57,7 +57,7 @@ func Auth(runtime *conf.Runtime, settings *service.SettingsService) gin.HandlerF
|
||||
httpx.NoAuth(c, message)
|
||||
return
|
||||
}
|
||||
if disabled, checkErr := settings.IsTokenDisabled(c.Request.Context(), token); checkErr != nil || disabled {
|
||||
if disabled, checkErr := tokens.IsTokenDisabled(c.Request.Context(), token); checkErr != nil || disabled {
|
||||
httpx.SetTokenCookie(c, "", -1)
|
||||
httpx.NoAuth(c, "您的帐户异地登陆或令牌失效")
|
||||
return
|
||||
@@ -82,7 +82,7 @@ func Auth(runtime *conf.Runtime, settings *service.SettingsService) gin.HandlerF
|
||||
if generateErr != nil {
|
||||
return nil, generateErr
|
||||
}
|
||||
if rotateErr := settings.RotateActiveToken(c.Request.Context(), claims.Username, token, newToken, expires); rotateErr != nil {
|
||||
if rotateErr := security.RotateActiveToken(c.Request.Context(), claims.Username, token, newToken, expires); rotateErr != nil {
|
||||
return nil, rotateErr
|
||||
}
|
||||
return refreshedToken{token: newToken, expiresAt: newClaims.ExpiresAt.Unix()}, nil
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func SecurityRateLimit(settings *service.SettingsService) gin.HandlerFunc {
|
||||
func SecurityRateLimit(settings *service.SecurityService) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
path := strings.TrimSuffix(c.Request.URL.Path, "/")
|
||||
if !strings.HasSuffix(path, "/base/login") && !strings.HasSuffix(path, "/base/captcha") {
|
||||
|
||||
Reference in New Issue
Block a user