feat(login): 添加 IP 限速机制以防止频繁登录尝试
- 在登录功能中实现同一 IP 1 分钟内最多尝试 10 次的限制 - 记录登录时间戳并过滤过期记录,确保登录尝试次数的准确性 - 优化未登录用户的返回信息,避免泄露敏感数据
This commit is contained in:
@@ -744,6 +744,9 @@ func setMakeCodeListener(name string, appIns *Application) {
|
||||
}
|
||||
}
|
||||
|
||||
if context.RouterString[0] != name {
|
||||
return isFinished
|
||||
}
|
||||
if len(context.RouterString) < 2 || len(context.RouterString) > 3 ||
|
||||
!(context.Router[context.RouterString[0]] != nil &&
|
||||
context.Router[context.RouterString[0]][context.RouterString[1]] != nil) {
|
||||
|
||||
@@ -10,9 +10,12 @@ import (
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var adminLoginRateLimiter sync.Map // key: ip,value: *[]int64(1分钟内登录时间戳列表)
|
||||
|
||||
// Project 管理端项目
|
||||
var TptProject = Proj{
|
||||
//"user": UserCtr,
|
||||
@@ -964,6 +967,26 @@ var TptProject = Proj{
|
||||
that.Display(0, re)
|
||||
},
|
||||
"login": func(that *Context) {
|
||||
// 限速:同一 IP 1分钟内最多尝试10次
|
||||
ip := that.Req.Header.Get("X-Forwarded-For")
|
||||
if ip == "" {
|
||||
ip = that.Req.RemoteAddr
|
||||
}
|
||||
now := time.Now().Unix()
|
||||
val, _ := adminLoginRateLimiter.LoadOrStore(ip, &[]int64{})
|
||||
times := val.(*[]int64)
|
||||
filtered := (*times)[:0]
|
||||
for _, t := range *times {
|
||||
if now-t < 60 {
|
||||
filtered = append(filtered, t)
|
||||
}
|
||||
}
|
||||
*times = append(filtered, now)
|
||||
if len(*times) > 10 {
|
||||
that.Display(5, "登录尝试过于频繁,请稍后再试")
|
||||
return
|
||||
}
|
||||
|
||||
hotimeName := that.RouterString[0]
|
||||
fileConfig := that.MakeCodeRouter[hotimeName].FileConfig
|
||||
|
||||
@@ -1017,15 +1040,15 @@ var TptProject = Proj{
|
||||
that.Display(4, "找不到配置文件")
|
||||
return
|
||||
}
|
||||
if that.Session(fileConfig.GetString("table")+"_id").Data == nil {
|
||||
|
||||
conf := ObjToMap(string(btes))
|
||||
delete(conf, "menus")
|
||||
delete(conf, "tables")
|
||||
//没有登录只需要返回这些信息
|
||||
that.Display(0, conf)
|
||||
return
|
||||
}
|
||||
if that.Session(fileConfig.GetString("table")+"_id").Data == nil {
|
||||
conf := ObjToMap(string(btes))
|
||||
//未登录只返回前端登录页所需的最少字段,避免泄露表结构等敏感信息
|
||||
that.Display(0, Map{
|
||||
"label": conf["label"],
|
||||
"name": conf["name"],
|
||||
})
|
||||
return
|
||||
}
|
||||
//可读写配置
|
||||
conf := ObjToMap(string(btes))
|
||||
menus := conf.GetSlice("menus")
|
||||
|
||||
Reference in New Issue
Block a user