middleware

package
v1.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DataScopeAll           = 1 // 全部数据
	DataScopeDepartment    = 2 // 本部门数据
	DataScopeDepartmentSub = 3 // 本部门及下级数据
	DataScopeSelf          = 4 // 仅本人数据
)

DataScope 数据权限范围常量

View Source
const AdminCookieName = "admin_access_token"

AdminCookieName 管理员 Access Token 的 HttpOnly Cookie 名

View Source
const UserCookieName = "user_access_token"

UserCookieName 普通用户会话 Access Token 的 HttpOnly Cookie 名(个人中心使用)

Variables

This section is empty.

Functions

func AdminAuthMiddleware

func AdminAuthMiddleware() httpInterfaces.HandlerFunc

AdminAuthMiddleware 管理员认证中间件。 角色检查必须发生在 c.Next() 之前——不能复用 AuthMiddleware() 再补检查, 其内部的 c.Next() 会先把业务 handler 跑完,403 就形同虚设。

func ApplyDataScope

func ApplyDataScope(db *gorm.DB, c httpInterfaces.RouterContextInterface, tableName string) *gorm.DB

ApplyDataScope 应用数据权限到查询中

func AuthMiddleware

func AuthMiddleware() httpInterfaces.HandlerFunc

AuthMiddleware JWT认证中间件 支持两种凭据来源(任选其一):

  1. Authorization: Bearer <token>
  2. HttpOnly Cookie admin_access_token / user_access_token(浏览器推荐,防 XSS 窃取)

func CorsMiddleware

func CorsMiddleware() httpInterfaces.HandlerFunc

CorsMiddleware 使用配置文件中的 app.cors.* 构建 CORS 中间件。

注意:本函数在 autoload 阶段被调用构造 HandlerFunc,此时 gsr.Provider 尚未装配完成; 因此配置读取必须延迟到首次请求(用 sync.Once 只做一次),避免触发 "provider gsr.Provider not found" panic。配置非法时(* + AllowCredentials)会 panic。

func CorsWithConfig

func CorsWithConfig(config CorsConfig) httpInterfaces.HandlerFunc

CorsWithConfig 使用自定义配置创建CORS中间件(按精确白名单返回 origin)

func DataScopeMiddleware

func DataScopeMiddleware() httpInterfaces.HandlerFunc

DataScopeMiddleware 数据权限中间件

func GetCurrentRole

func GetCurrentRole(c httpInterfaces.RouterContextInterface) (string, bool)

GetCurrentRole 从上下文中获取当前用户角色

func GetCurrentSessionID added in v1.7.0

func GetCurrentSessionID(c httpInterfaces.RouterContextInterface) string

func GetCurrentUser

GetCurrentUser 从上下文中获取当前用户信息

func GetCurrentUserID

func GetCurrentUserID(c httpInterfaces.RouterContextInterface) (uint, bool)

GetCurrentUserID 从上下文中获取当前用户ID

func GetDataScopeCondition

func GetDataScopeCondition(userID uint, db *gorm.DB, tableName string) (string, []interface{})

GetDataScopeCondition 获取数据权限查询条件(用于Service层)

func OptionalAuthMiddleware

func OptionalAuthMiddleware() httpInterfaces.HandlerFunc

OptionalAuthMiddleware 可选认证中间件 如果提供了token则验证,如果没有提供则继续执行,但不设置用户信息

func RBACMiddleware

func RBACMiddleware() httpInterfaces.HandlerFunc

RBACMiddleware RBAC 权限验证中间件

func RequireAnyRole

func RequireAnyRole(roleCodes []string) httpInterfaces.HandlerFunc

RequireAnyRole 要求任意一个角色的中间件

func RequireDataScope

func RequireDataScope(requiredScope int) httpInterfaces.HandlerFunc

RequireDataScope 要求特定数据权限范围的中间件

func RequirePermission

func RequirePermission(resource, action string) httpInterfaces.HandlerFunc

RequirePermission 要求特定权限的中间件

func RequireRole

func RequireRole(roleCode string) httpInterfaces.HandlerFunc

RequireRole 要求特定角色的中间件

func UserAuthMiddleware added in v1.7.0

func UserAuthMiddleware() httpInterfaces.HandlerFunc

UserAuthMiddleware resolves personal cookies before administrator cookies.

func ValidateUserSessionToken added in v1.7.0

func ValidateUserSessionToken(token string) (*jwt.Claims, error)

ValidateUserSessionToken validates one raw IAM user-session token without consulting request headers. OIDC registration-intent detection uses this for the HttpOnly user cookie and deliberately ignores JavaScript-supplied bearer tokens.

Types

type CorsConfig

type CorsConfig struct {
	AllowOrigins     []string
	AllowMethods     []string
	AllowHeaders     []string
	ExposeHeaders    []string
	AllowCredentials bool
	MaxAge           int
}

CorsConfig 配置CORS中间件的选项

func DefaultCorsConfig

func DefaultCorsConfig() CorsConfig

DefaultCorsConfig 返回默认CORS配置(安全默认:本地开发白名单,非通配符)

Source Files

  • auth_middleware.go
  • cors_middleware.go
  • data_scope_middleware.go
  • oidc_cors.go
  • rbac_middleware.go

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL