Documentation
¶
Index ¶
- Constants
- func AdminAuthMiddleware() httpInterfaces.HandlerFunc
- func ApplyDataScope(db *gorm.DB, c httpInterfaces.RouterContextInterface, tableName string) *gorm.DB
- func AuthMiddleware() httpInterfaces.HandlerFunc
- func CorsMiddleware() httpInterfaces.HandlerFunc
- func CorsWithConfig(config CorsConfig) httpInterfaces.HandlerFunc
- func DataScopeMiddleware() httpInterfaces.HandlerFunc
- func GetCurrentRole(c httpInterfaces.RouterContextInterface) (string, bool)
- func GetCurrentSessionID(c httpInterfaces.RouterContextInterface) string
- func GetCurrentUser(c httpInterfaces.RouterContextInterface) (uint, string, bool)
- func GetCurrentUserID(c httpInterfaces.RouterContextInterface) (uint, bool)
- func GetDataScopeCondition(userID uint, db *gorm.DB, tableName string) (string, []interface{})
- func OptionalAuthMiddleware() httpInterfaces.HandlerFunc
- func RBACMiddleware() httpInterfaces.HandlerFunc
- func RequireAnyRole(roleCodes []string) httpInterfaces.HandlerFunc
- func RequireDataScope(requiredScope int) httpInterfaces.HandlerFunc
- func RequirePermission(resource, action string) httpInterfaces.HandlerFunc
- func RequireRole(roleCode string) httpInterfaces.HandlerFunc
- func UserAuthMiddleware() httpInterfaces.HandlerFunc
- func ValidateUserSessionToken(token string) (*jwt.Claims, error)
- type CorsConfig
Constants ¶
const ( DataScopeAll = 1 // 全部数据 DataScopeDepartment = 2 // 本部门数据 DataScopeDepartmentSub = 3 // 本部门及下级数据 DataScopeSelf = 4 // 仅本人数据 )
DataScope 数据权限范围常量
const AdminCookieName = "admin_access_token"
AdminCookieName 管理员 Access Token 的 HttpOnly Cookie 名
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认证中间件 支持两种凭据来源(任选其一):
- Authorization: Bearer <token>
- 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 ¶
func GetCurrentUser(c httpInterfaces.RouterContextInterface) (uint, string, bool)
GetCurrentUser 从上下文中获取当前用户信息
func GetCurrentUserID ¶
func GetCurrentUserID(c httpInterfaces.RouterContextInterface) (uint, bool)
GetCurrentUserID 从上下文中获取当前用户ID
func GetDataScopeCondition ¶
GetDataScopeCondition 获取数据权限查询条件(用于Service层)
func OptionalAuthMiddleware ¶
func OptionalAuthMiddleware() httpInterfaces.HandlerFunc
OptionalAuthMiddleware 可选认证中间件 如果提供了token则验证,如果没有提供则继续执行,但不设置用户信息
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
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 ¶
Source Files
¶
- auth_middleware.go
- cors_middleware.go
- data_scope_middleware.go
- oidc_cors.go
- rbac_middleware.go