我有Spring MVC应用程序,托管在Tomcat容器中。 Tomcat面向NginX,因此我在NginX配置中设置了explcitly头:
位置/ {
proxy_passhttp://127.0.0.1:8000
;
proxy_set_header主机$ host;
proxy_set_header X-Real-IP $ remote_addr;
}
这似乎工作正常,标题就在那里(我在调试器中看到它)。
现在,我尝试使用请求包装和过滤器跟随this solution here,但它不起作用,因为显然我的自定义包装请求对象不是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安全性接受包装请求相关,或者将此过滤器应用为处理链中的第一个过滤器,或者为此产生影响(如果可能的话)。 / p>
答案 0 :(得分:1)
确保在弹簧安全之前进行包装。在spring security的filter-mapping之前移动包含yuor包装过滤器的filter-mapping元素。