Merge pull request #359 from prq0705/master

修复验证码图片路径未跟随 web_base_url,反代子路径后无法登录
This commit is contained in:
yisier
2026-10-10 14:13:41 +08:00
2 changed files with 19 additions and 2 deletions
+15 -2
View File
@@ -5,6 +5,7 @@ import (
"github.com/astaxie/beego/utils/captcha"
"math/rand"
"net"
"strings"
"sync"
"time"
@@ -27,10 +28,22 @@ type record struct {
lastLoginTime time.Time
}
func init() {
// InitCaptcha registers the captcha image route and the create_captcha template
// function. The image URL prefix must include web_base_url, otherwise the login
// page would request the captcha from the site root and fail behind a reverse
// proxy that only forwards the web_base_url sub path.
//
// It must be called after beego.LoadAppConfig, so it cannot live in init():
// package init() runs before main() loads conf/nps.conf, where web_base_url
// would still be empty.
func InitCaptcha() {
// use beego cache system store the captcha data
store := cache.NewMemoryCache()
cpt = captcha.NewWithFilter("/captcha/", store)
base := strings.TrimSuffix(beego.AppConfig.String("web_base_url"), "/")
if base != "" && !strings.HasPrefix(base, "/") {
base = "/" + base
}
cpt = captcha.NewWithFilter(base+"/captcha/", store)
}
func (self *LoginController) Index() {
+4
View File
@@ -6,6 +6,10 @@ import (
)
func Init() {
// The captcha image path and its filter are built from web_base_url, so it
// has to be registered here (after the config is loaded) instead of in an
// init() function.
controllers.InitCaptcha()
web_base_url := beego.AppConfig.String("web_base_url")
if len(web_base_url) > 0 {
ns := beego.NewNamespace(web_base_url,