簡體   English   中英

Spring MVC應用程序中的NginX反向代理背后的getRemoteAddr()?

[英]getRemoteAddr() behind NginX reverse proxy in Spring MVC app?

我有Spring MVC應用程序,托管在Tomcat容器中。 Tomcat面向NginX,因此我在NginX配置中設置了explcitly頭部:

位置 / {
proxy_pass http://127.0.0.1:8000 ;
proxy_set_header主機$ host;
proxy_set_header X-Real-IP $ remote_addr;
}

這似乎工作正常,標題就在那里(我在調試器中看到它)。

現在,我嘗試使用請求包裝和過濾器來遵循此解決方案 ,但它不起作用,因為顯然我的自定義包裝請求對象不是MVC機制使用的對象。 映射如下:

 <servlet-mapping>  
   <servlet-name>mvc-dispatcher</servlet-name>  
   <url-pattern>/</url-pattern>  
 </servlet-mapping>

 <filter-mapping>
   <filter-name>RealIPFilter</filter-name>
   <url-pattern>/*</url-pattern>
 </filter-mapping>

我嘗試在代碼中的兩個(不同的)位置提取遠程地址:

1)

public class PostAuthSuccessHandler implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(
            HttpServletRequest request,
            HttpServletResponse response, 
            Authentication authentication)
    throws IOException, ServletException {
           // here
           request.getRemoteAddr();
    }
}


2)

public class LoginFailureEventListenter implements
ApplicationListener<AuthenticationFailureBadCredentialsEvent> {

    @Override
    public void onApplicationEvent(AuthenticationFailureBadCredentialsEvent event) {
        String remoteIP = null;
        Object src = event.getSource();
        if(src instanceof UsernamePasswordAuthenticationToken) {
            UsernamePasswordAuthenticationToken upat = (UsernamePasswordAuthenticationToken) src;
            if(upat.getDetails() instanceof WebAuthenticationDetails) {
                // here
                remoteIP = ((WebAuthenticationDetails)upat.getDetails()).getRemoteAddress();
            }
        }
}

使用Spring MVC可能是什么方法?

編輯
由於應用程序是使用Spring安全性保護的,因此這可能僅與使Spring安全性接受包裝請求相關,或者將此過濾器應用為處理鏈中的第一個過濾器,或者應用此類效果(如果可能的話)。

確保在彈簧安全之前進行包裝。 移動包含yuor包裝過濾器的filter-mapping元素,然后在spring-security的過濾器映射之前移動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM