Skip to content

Commit 4247ee4

Browse files
committed
getip
1 parent d3edd1f commit 4247ee4

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

pkg/jwtmanager/jwtcache.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,16 @@ func JWTCacheHandler(next http.Handler) http.Handler {
5353
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
5454
// check to see if the request is from a whitelisted IP
5555
ipWhitelist := cfg.Cfg.IPWhiteList
56-
ipHost, _, _ := net.SplitHostPort(r.RemoteAddr) // 去掉端口
57-
log.Debugf("request ip: %s", ipHost)
56+
realIP := r.Header.Get("X-Forwarded-For")
57+
if realIP == "" {
58+
realIP = r.Header.Get("X-Real-IP")
59+
}
60+
if realIP == "" {
61+
realIP, _, _ = net.SplitHostPort(r.RemoteAddr)
62+
}
63+
log.Debugf("request ip: %s", realIP)
5864
for _, wip := range ipWhitelist {
59-
if ipHost == wip {
65+
if realIP == wip {
6066
logger.Debug("IP whitelisted, access granted")
6167
w.Header().Add(cfg.Cfg.Headers.User, "whitelisted")
6268
w.Header().Add(cfg.Cfg.Headers.Success, "true")

0 commit comments

Comments
 (0)