简体   繁体   English

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

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

I have Spring MVC app, hosted in Tomcat container. 我有Spring MVC应用程序,托管在Tomcat容器中。 Tomcat is fronted with NginX, and because of that I set explcitly the header in NginX configuration: Tomcat面向NginX,因此我在NginX配置中设置了explcitly头部:

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

This seems to be working OK, the header is there (I see it in the debugger). 这似乎工作正常,标题就在那里(我在调试器中看到它)。

Now, I tried to follow this solution here , with the request wrapping and filter, but it does not work, as apparently my custom-wrapped request object is not the one used by MVC mechanism. 现在,我尝试使用请求包装和过滤器来遵循此解决方案 ,但它不起作用,因为显然我的自定义包装请求对象不是MVC机制使用的对象。 Mappings are as follows: 映射如下:

 <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>

I try to extract remote address in two (different) places in the code: 我尝试在代码中的两个(不同的)位置提取远程地址:

1) 1)

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

and
2) 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();
            }
        }
}

What could be the proper way to go about this with Spring MVC ? 使用Spring MVC可能是什么方法?

EDIT 编辑
Since the application is secured with Spring security, this may as well be related only to making Spring security accept wrapped request, or apply this filter as a first filter in the processing chain, or something to that effect (if at all possible). 由于应用程序是使用Spring安全性保护的,因此这可能仅与使Spring安全性接受包装请求相关,或者将此过滤器应用为处理链中的第一个过滤器,或者应用此类效果(如果可能的话)。

Make sure that your wrapping occurs BEFORE spring security. 确保在弹簧安全之前进行包装。 Move the filter-mapping element containing yuor wrapping filter before the filter-mapping for spring security. 移动包含yuor包装过滤器的filter-mapping元素,然后在spring-security的过滤器映射之前移动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM