mirror of
https://github.com/gotoeasy/glogcenter.git
synced 2025-09-15 12:58:34 +08:00
增加API秘钥过滤器
This commit is contained in:
parent
7467414299
commit
b4e4011f8d
@ -13,6 +13,7 @@ type HttpController struct {
|
||||
|
||||
var mapHandleGet map[string]*HttpController
|
||||
var mapHandlePost map[string]*HttpController
|
||||
var filters []func(*HttpRequest) *HttpResult
|
||||
|
||||
func init() {
|
||||
mapHandleGet = make(map[string]*HttpController)
|
||||
@ -51,3 +52,11 @@ func RegisterController(methodType string, path string, fnController func(*HttpR
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func RegisterFilter(fnFilter func(*HttpRequest) *HttpResult) {
|
||||
filters = append(filters, fnFilter)
|
||||
}
|
||||
|
||||
func getFilters() []func(*HttpRequest) *HttpResult {
|
||||
return filters
|
||||
}
|
||||
|
||||
@ -14,9 +14,20 @@ func Run() {
|
||||
|
||||
server.NoRoute(func(c *gin.Context) {
|
||||
req := NewHttpRequest(c)
|
||||
|
||||
// filter
|
||||
filters := getFilters()
|
||||
for _, fnFilter := range filters {
|
||||
rs := fnFilter(req)
|
||||
if rs != nil {
|
||||
c.JSON(rs.Code, rs) // 过滤器返回有内容时直接返回处理结果,结束
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// controller
|
||||
path := strings.ToLower(c.Request.URL.Path)
|
||||
method := strings.ToUpper(c.Request.Method)
|
||||
|
||||
handle := getHttpController(method, path)
|
||||
if handle == nil {
|
||||
c.JSON(http.StatusNotFound, Error404())
|
||||
|
||||
@ -18,8 +18,15 @@ var maxIdleTime int
|
||||
var storeNameAutoAddDate bool
|
||||
var serverPort int
|
||||
var contextPath string
|
||||
var enableSecurityKey bool
|
||||
var securityKey string
|
||||
var headerSecurityKey string
|
||||
|
||||
func init() {
|
||||
UpdateConfigByEnv()
|
||||
}
|
||||
|
||||
func UpdateConfigByEnv() {
|
||||
// 读取环境变量初始化配置,各配置都有默认值
|
||||
storeRoot = Getenv("GLC_STORE_ROOT", "/glogcenter") // 存储根目录
|
||||
storeChanLength = GetenvInt("GLC_STORE_CHAN_LENGTH", 64) // 存储通道长度
|
||||
@ -27,6 +34,24 @@ func init() {
|
||||
storeNameAutoAddDate = GetenvBool("GLC_STORE_NAME_AUTO_ADD_DATE", true) // 存储名是否自动添加日期(日志量大通常按日单位区分存储),默认true
|
||||
serverPort = GetenvInt("GLC_SERVER_PORT", 8080) // web服务端口
|
||||
contextPath = Getenv("GLC_CONTEXT_PATH", "/glc") // web服务contextPath
|
||||
enableSecurityKey = GetenvBool("GLC_ENABLE_SECURITY_KEY", true) // web服务是否开启API秘钥校验,默认false
|
||||
securityKey = Getenv("GLC_HEADER_SECURITY_KEY", "X-GLC-AUTH") // web服务API秘钥的header键名
|
||||
headerSecurityKey = Getenv("GLC_SECURITY_KEY", "glogcenter") // web服务API秘钥
|
||||
}
|
||||
|
||||
// 取配置: web服务API秘钥的header键名,可通过环境变量“GLC_HEADER_SECURITY_KEY”设定,默认值“X-GLC-AUTH”
|
||||
func IsEnableSecurityKey() bool {
|
||||
return enableSecurityKey
|
||||
}
|
||||
|
||||
// 取配置: web服务API秘钥的header键名,可通过环境变量“GLC_HEADER_SECURITY_KEY”设定,默认值“X-GLC-AUTH”
|
||||
func GetHeaderSecurityKey() string {
|
||||
return headerSecurityKey
|
||||
}
|
||||
|
||||
// 取配置: web服务API秘钥,可通过环境变量“GLC_SECURITY_KEY”设定,默认值“glogcenter”
|
||||
func GetSecurityKey() string {
|
||||
return securityKey
|
||||
}
|
||||
|
||||
// 取配置: web服务端口,可通过环境变量“GLC_CONTEXT_PATH”设定,默认值“8080”
|
||||
|
||||
21
glc/web/filter/filter.go
Normal file
21
glc/web/filter/filter.go
Normal file
@ -0,0 +1,21 @@
|
||||
package filter
|
||||
|
||||
import (
|
||||
"glc/gweb"
|
||||
"glc/ldb/conf"
|
||||
)
|
||||
|
||||
// 校验HEADER的API秘钥
|
||||
func ApiKeyFilter(req *gweb.HttpRequest) *gweb.HttpResult {
|
||||
|
||||
// 开启API秘钥校验时才检查
|
||||
if !conf.IsEnableSecurityKey() {
|
||||
return nil
|
||||
}
|
||||
|
||||
auth := req.GetHeader(conf.GetHeaderSecurityKey())
|
||||
if auth != conf.GetSecurityKey() {
|
||||
return gweb.Error(403, "未经授权的访问,拒绝服务")
|
||||
}
|
||||
return nil // 返回nil表示正常过滤成功
|
||||
}
|
||||
@ -5,16 +5,23 @@ import (
|
||||
"glc/gweb/method"
|
||||
"glc/ldb/conf"
|
||||
"glc/web/controller"
|
||||
"glc/web/filter"
|
||||
)
|
||||
|
||||
func Register() {
|
||||
|
||||
// ContextPath
|
||||
contextPath := conf.GetContextPath()
|
||||
|
||||
// 过滤器
|
||||
gweb.RegisterFilter(filter.ApiKeyFilter)
|
||||
|
||||
// 控制器器(跳转)
|
||||
gweb.RegisterController(method.GET, "/", controller.RedirectToSearchController)
|
||||
gweb.RegisterController(method.GET, contextPath, controller.RedirectToSearchController)
|
||||
gweb.RegisterController(method.GET, contextPath+"/", controller.RedirectToSearchController)
|
||||
|
||||
// 控制器
|
||||
gweb.RegisterController(method.POST, contextPath+"/search", controller.LogSearchController)
|
||||
gweb.RegisterController(method.POST, contextPath+"/add", controller.LogAddController)
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user