Skip to content

Commit 7e6d8ae

Browse files
committed
fix: unauthorized access due to 'web api' enabled by defalut
ehang-io#1091
1 parent fb6b5b0 commit 7e6d8ae

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

web/controllers/base.go

+14-11
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type BaseController struct {
2020
actionName string
2121
}
2222

23-
//初始化参数
23+
// 初始化参数
2424
func (s *BaseController) Prepare() {
2525
s.Data["web_base_url"] = beego.AppConfig.String("web_base_url")
2626
controllerName, actionName := s.GetControllerAndAction()
@@ -33,6 +33,9 @@ func (s *BaseController) Prepare() {
3333
timestamp := s.GetIntNoErr("timestamp")
3434
configKey := beego.AppConfig.String("auth_key")
3535
timeNowUnix := time.Now().Unix()
36+
if configKey == "" {
37+
configKey = crypt.GetRandomString(128)
38+
}
3639
if !(md5Key != "" && (math.Abs(float64(timeNowUnix-int64(timestamp))) <= 20) && (crypt.Md5(configKey+strconv.Itoa(timestamp)) == md5Key)) {
3740
if s.GetSession("auth") != true {
3841
s.Redirect(beego.AppConfig.String("web_base_url")+"/login/index", 302)
@@ -62,7 +65,7 @@ func (s *BaseController) Prepare() {
6265
s.Data["allow_user_change_username"], _ = beego.AppConfig.Bool("allow_user_change_username")
6366
}
6467

65-
//加载模板
68+
// 加载模板
6669
func (s *BaseController) display(tpl ...string) {
6770
s.Data["web_base_url"] = beego.AppConfig.String("web_base_url")
6871
var tplname string
@@ -86,19 +89,19 @@ func (s *BaseController) display(tpl ...string) {
8689
s.TplName = tplname
8790
}
8891

89-
//错误
92+
// 错误
9093
func (s *BaseController) error() {
9194
s.Data["web_base_url"] = beego.AppConfig.String("web_base_url")
9295
s.Layout = "public/layout.html"
9396
s.TplName = "public/error.html"
9497
}
9598

96-
//getEscapeString
99+
// getEscapeString
97100
func (s *BaseController) getEscapeString(key string) string {
98101
return html.EscapeString(s.GetString(key))
99102
}
100103

101-
//去掉没有err返回值的int
104+
// 去掉没有err返回值的int
102105
func (s *BaseController) GetIntNoErr(key string, def ...int) int {
103106
strv := s.Ctx.Input.Query(key)
104107
if len(strv) == 0 && len(def) > 0 {
@@ -108,7 +111,7 @@ func (s *BaseController) GetIntNoErr(key string, def ...int) int {
108111
return val
109112
}
110113

111-
//获取去掉错误的bool值
114+
// 获取去掉错误的bool值
112115
func (s *BaseController) GetBoolNoErr(key string, def ...bool) bool {
113116
strv := s.Ctx.Input.Query(key)
114117
if len(strv) == 0 && len(def) > 0 {
@@ -118,29 +121,29 @@ func (s *BaseController) GetBoolNoErr(key string, def ...bool) bool {
118121
return val
119122
}
120123

121-
//ajax正确返回
124+
// ajax正确返回
122125
func (s *BaseController) AjaxOk(str string) {
123126
s.Data["json"] = ajax(str, 1)
124127
s.ServeJSON()
125128
s.StopRun()
126129
}
127130

128-
//ajax错误返回
131+
// ajax错误返回
129132
func (s *BaseController) AjaxErr(str string) {
130133
s.Data["json"] = ajax(str, 0)
131134
s.ServeJSON()
132135
s.StopRun()
133136
}
134137

135-
//组装ajax
138+
// 组装ajax
136139
func ajax(str string, status int) map[string]interface{} {
137140
json := make(map[string]interface{})
138141
json["status"] = status
139142
json["msg"] = str
140143
return json
141144
}
142145

143-
//ajax table返回
146+
// ajax table返回
144147
func (s *BaseController) AjaxTable(list interface{}, cnt int, recordsTotal int, kwargs map[string]interface{}) {
145148
json := make(map[string]interface{})
146149
json["rows"] = list
@@ -157,7 +160,7 @@ func (s *BaseController) AjaxTable(list interface{}, cnt int, recordsTotal int,
157160
s.StopRun()
158161
}
159162

160-
//ajax table参数
163+
// ajax table参数
161164
func (s *BaseController) GetAjaxParams() (start, limit int) {
162165
return s.GetIntNoErr("offset"), s.GetIntNoErr("limit")
163166
}

0 commit comments

Comments
 (0)