Skip to content

Commit 91ecd04

Browse files
committed
feat: 优化登录中间件使用白名单
1 parent 6743ac9 commit 91ecd04

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

internal/http/middleware/middleware.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ func GlobalMiddleware() []func(http.Handler) http.Handler {
2424
LogRequestHeaders: []string{"User-Agent"},
2525
}),
2626
middleware.Recoverer,
27-
Entrance,
2827
Status,
28+
Entrance,
29+
MustLogin,
2930
MustInstall,
3031
}
3132
}

internal/http/middleware/must_login.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package middleware
33
import (
44
"context"
55
"net/http"
6+
"slices"
7+
"strings"
68

79
"github.com/go-rat/chix"
810
"github.com/spf13/cast"
@@ -12,6 +14,14 @@ import (
1214

1315
// MustLogin 确保已登录
1416
func MustLogin(next http.Handler) http.Handler {
17+
// 白名单
18+
whiteList := []string{
19+
"/api/user/login",
20+
"/api/user/logout",
21+
"/api/user/isLogin",
22+
"/api/dashboard/panel",
23+
}
24+
1525
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1626
sess, err := app.Session.GetSession(r)
1727
if err != nil {
@@ -22,6 +32,12 @@ func MustLogin(next http.Handler) http.Handler {
2232
})
2333
}
2434

35+
// 对白名单和非 API 请求放行
36+
if slices.Contains(whiteList, r.URL.Path) || !strings.HasPrefix(r.URL.Path, "/api") {
37+
next.ServeHTTP(w, r)
38+
return
39+
}
40+
2541
if sess.Missing("user_id") {
2642
render := chix.NewRender(w)
2743
render.Status(http.StatusUnauthorized)

internal/route/http.go

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,24 @@ func Http(r chi.Router) {
2121
r.With(middleware.Throttle(5, time.Minute)).Post("/login", user.Login)
2222
r.Post("/logout", user.Logout)
2323
r.Get("/isLogin", user.IsLogin)
24-
r.With(middleware.MustLogin).Get("/info", user.Info)
24+
r.Get("/info", user.Info)
2525
})
2626

2727
r.Route("/dashboard", func(r chi.Router) {
2828
dashboard := service.NewDashboardService()
2929
r.Get("/panel", dashboard.Panel)
30-
r.With(middleware.MustLogin).Get("/homeApps", dashboard.HomeApps)
31-
r.With(middleware.MustLogin).Post("/current", dashboard.Current)
32-
r.With(middleware.MustLogin).Get("/systemInfo", dashboard.SystemInfo)
33-
r.With(middleware.MustLogin).Get("/countInfo", dashboard.CountInfo)
34-
r.With(middleware.MustLogin).Get("/installedDbAndPhp", dashboard.InstalledDbAndPhp)
35-
r.With(middleware.MustLogin).Get("/checkUpdate", dashboard.CheckUpdate)
36-
r.With(middleware.MustLogin).Get("/updateInfo", dashboard.UpdateInfo)
37-
r.With(middleware.MustLogin).Post("/update", dashboard.Update)
38-
r.With(middleware.MustLogin).Post("/restart", dashboard.Restart)
30+
r.Get("/homeApps", dashboard.HomeApps)
31+
r.Post("/current", dashboard.Current)
32+
r.Get("/systemInfo", dashboard.SystemInfo)
33+
r.Get("/countInfo", dashboard.CountInfo)
34+
r.Get("/installedDbAndPhp", dashboard.InstalledDbAndPhp)
35+
r.Get("/checkUpdate", dashboard.CheckUpdate)
36+
r.Get("/updateInfo", dashboard.UpdateInfo)
37+
r.Post("/update", dashboard.Update)
38+
r.Post("/restart", dashboard.Restart)
3939
})
4040

4141
r.Route("/task", func(r chi.Router) {
42-
r.Use(middleware.MustLogin)
4342
task := service.NewTaskService()
4443
r.Get("/status", task.Status)
4544
r.Get("/", task.List)
@@ -48,7 +47,6 @@ func Http(r chi.Router) {
4847
})
4948

5049
r.Route("/website", func(r chi.Router) {
51-
r.Use(middleware.MustLogin)
5250
website := service.NewWebsiteService()
5351
r.Get("/defaultConfig", website.GetDefaultConfig)
5452
r.Post("/defaultConfig", website.UpdateDefaultConfig)
@@ -65,7 +63,6 @@ func Http(r chi.Router) {
6563
})
6664

6765
r.Route("/database", func(r chi.Router) {
68-
r.Use(middleware.MustLogin)
6966
database := service.NewDatabaseService()
7067
r.Get("/", database.List)
7168
r.Post("/", database.Create)
@@ -74,7 +71,6 @@ func Http(r chi.Router) {
7471
})
7572

7673
r.Route("/databaseServer", func(r chi.Router) {
77-
r.Use(middleware.MustLogin)
7874
database := service.NewDatabaseService()
7975
r.Get("/", database.List)
8076
r.Post("/", database.Create)
@@ -83,7 +79,6 @@ func Http(r chi.Router) {
8379
})
8480

8581
r.Route("/backup", func(r chi.Router) {
86-
r.Use(middleware.MustLogin)
8782
backup := service.NewBackupService()
8883
r.Get("/{type}", backup.List)
8984
r.Post("/{type}", backup.Create)
@@ -93,7 +88,6 @@ func Http(r chi.Router) {
9388
})
9489

9590
r.Route("/cert", func(r chi.Router) {
96-
r.Use(middleware.MustLogin)
9791
cert := service.NewCertService()
9892
r.Get("/caProviders", cert.CAProviders)
9993
r.Get("/dnsProviders", cert.DNSProviders)
@@ -131,7 +125,6 @@ func Http(r chi.Router) {
131125
})
132126

133127
r.Route("/app", func(r chi.Router) {
134-
r.Use(middleware.MustLogin)
135128
app := service.NewAppService()
136129
r.Get("/list", app.List)
137130
r.Post("/install", app.Install)
@@ -143,7 +136,6 @@ func Http(r chi.Router) {
143136
})
144137

145138
r.Route("/cron", func(r chi.Router) {
146-
r.Use(middleware.MustLogin)
147139
cron := service.NewCronService()
148140
r.Get("/", cron.List)
149141
r.Post("/", cron.Create)
@@ -154,7 +146,6 @@ func Http(r chi.Router) {
154146
})
155147

156148
r.Route("/safe", func(r chi.Router) {
157-
r.Use(middleware.MustLogin)
158149
safe := service.NewSafeService()
159150
r.Get("/ssh", safe.GetSSH)
160151
r.Post("/ssh", safe.UpdateSSH)
@@ -163,7 +154,6 @@ func Http(r chi.Router) {
163154
})
164155

165156
r.Route("/firewall", func(r chi.Router) {
166-
r.Use(middleware.MustLogin)
167157
firewall := service.NewFirewallService()
168158
r.Get("/status", firewall.GetStatus)
169159
r.Post("/status", firewall.UpdateStatus)
@@ -179,7 +169,6 @@ func Http(r chi.Router) {
179169
})
180170

181171
r.Route("/ssh", func(r chi.Router) {
182-
r.Use(middleware.MustLogin)
183172
ssh := service.NewSSHService()
184173
r.Get("/", ssh.List)
185174
r.Post("/", ssh.Create)
@@ -189,7 +178,6 @@ func Http(r chi.Router) {
189178
})
190179

191180
r.Route("/container", func(r chi.Router) {
192-
r.Use(middleware.MustLogin)
193181
r.Route("/container", func(r chi.Router) {
194182
container := service.NewContainerService()
195183
r.Get("/", container.List)
@@ -230,7 +218,6 @@ func Http(r chi.Router) {
230218
})
231219

232220
r.Route("/file", func(r chi.Router) {
233-
r.Use(middleware.MustLogin)
234221
file := service.NewFileService()
235222
r.Post("/create", file.Create)
236223
r.Get("/content", file.Content)
@@ -251,7 +238,6 @@ func Http(r chi.Router) {
251238
})
252239

253240
r.Route("/monitor", func(r chi.Router) {
254-
r.Use(middleware.MustLogin)
255241
monitor := service.NewMonitorService()
256242
r.Get("/setting", monitor.GetSetting)
257243
r.Post("/setting", monitor.UpdateSetting)
@@ -260,14 +246,12 @@ func Http(r chi.Router) {
260246
})
261247

262248
r.Route("/setting", func(r chi.Router) {
263-
r.Use(middleware.MustLogin)
264249
setting := service.NewSettingService()
265250
r.Get("/", setting.Get)
266251
r.Post("/", setting.Update)
267252
})
268253

269254
r.Route("/systemctl", func(r chi.Router) {
270-
r.Use(middleware.MustLogin)
271255
systemctl := service.NewSystemctlService()
272256
r.Get("/status", systemctl.Status)
273257
r.Get("/isEnabled", systemctl.IsEnabled)
@@ -280,7 +264,6 @@ func Http(r chi.Router) {
280264
})
281265

282266
r.Route("/apps", func(r chi.Router) {
283-
r.Use(middleware.MustLogin)
284267
apps.Boot(r)
285268
})
286269
})

0 commit comments

Comments
 (0)