Documentation
¶
Index ¶
- Constants
- Variables
- func DELETE(policy string, uri string, handler gin.HandlerFunc)
- func GET(policy string, uri string, handler gin.HandlerFunc)
- func HEAD(policy string, uri string, handler gin.HandlerFunc)
- func OPTIONS(policy string, uri string, handler gin.HandlerFunc)
- func PATCH(policy string, uri string, handler gin.HandlerFunc)
- func POST(policy string, uri string, handler gin.HandlerFunc)
- func PUT(policy string, uri string, handler gin.HandlerFunc)
- type SecurityConfig
Constants ¶
View Source
const ( MethodGet httpmethod = "GET" MethodHead httpmethod = "HEAD" MethodPost httpmethod = "POST" MethodPut httpmethod = "PUT" MethodPatch httpmethod = "PATCH" // RFC 5789 MethodDelete httpmethod = "DELETE" MethodOptions httpmethod = "OPTIONS" )
Variables ¶
View Source
var ( ErrNotActive = errors.New("未启用security模块") ErrNotActiveInvalidParam = errors.New("参数错误,未启用security模块") ErrRegistFailed = errors.New("无法注册:错误的uri,需要更多层级的路由") ErrPolicyNotFound = errors.New("找不到权限信息") ErrNoApiPolicy = errors.New("接口权限不足") ErrNoCustomerInfo = errors.New("无法获取用户信息") )
View Source
var SECURITY_HANDLER gin.HandlerFunc = func(ctx *gin.Context) { if id := jwt.GetClaims(ctx).Id; len(id) == 0 { log.Error(ErrNoCustomerInfo) restful.Errors.FromError(ErrNoCustomerInfo).Restful(ctx) ctx.Abort() } else if uid, e := strconv.ParseUint(id, 10, 64); e != nil || uid == 0 { log.Error(ErrNoCustomerInfo) restful.Errors.FromError(ErrNoCustomerInfo).Restful(ctx) ctx.Abort() } else if p, b := role.GetPolicy(apiObj(ctx.Request.Method, ctx.Request.URL.Path), role.ACT_API_ACCESS); !b { log.Error(ErrPolicyNotFound) restful.Errors.FromError(ErrPolicyNotFound).Restful(ctx) ctx.Abort() } else if urs, e := role.FindUserRoleByUid(uid); e != nil { log.Error(e) restful.Errors.FromError(e).Restful(ctx) ctx.Abort() } else if len(urs) == 0 { log.Error(ErrNoApiPolicy) restful.Errors.FromError(ErrNoApiPolicy).Restful(ctx) ctx.Abort() } else { for _, ur := range urs { if nil != ur.Role && ur.Role.Match(p) { ctx.Next() return } } restful.Errors.FromError(ErrNoApiPolicy).Restful(ctx) ctx.Abort() } }
Functions ¶
Types ¶
type SecurityConfig ¶
type SecurityConfig struct {
Enable bool `mapstructure:"enable" json:"enable" yaml:"enable"` // 是否开启
AllowCors bool `mapstructure:"allow_cors" json:"allow_cors" yaml:"allow_cors"` // 是否允许跨域
EnableRedisCache bool `mapstructure:"enable-redis-cache" json:"enableRedisCache" yaml:"enable-redis-cache"` // 是否开启redis缓存
}
func (SecurityConfig) Load ¶
func (c SecurityConfig) Load()
Click to show internal directories.
Click to hide internal directories.