How to get Client IP address in Spring bean
Asked Answered
S

3

5

I have define a Spring bean.

<beans>
  <bean id="remoteService" class="edu.wustl.catissuecore.CaTissueApplictionServicImpl" />
</beans>

Is there any way to get the IP address of client in this class? Similarly as available in the servlet request.getRemoteAddr();

Spermatocyte answered 30/12, 2009 at 10:16 Comment(1)
1.) Is this a webapp or just some random Spring app? 2.) It's great that edu.wustl.catissuecore.CaTissueApplictionServicImpl (or gov.nih.nci.system.comm.server.ApplicationServiceServerImpl?) doesn't implement AbstractController... but what does it implement? If it's not a controller itself, is there a Controller 'above' this that can pass this into this class? 3.) The Spring configuration for the class was already posted in the main question - you need to post code, or at least describe the inheritance hierarchy and what other beans your class interacts with.Serviceberry
R
20

The simplest (and ugliest) approach is to use RequestContextHolder:

  String remoteAddress = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes())
       .getRequest().getRemoteAddr();

Without knowing more about your bean and how it's wired up, that's the best I can suggest. If your bean is a controller (either subclassing AbstractController or being annotated with @Controller) then it should be able to get direct access to the request object.

Reichenberg answered 30/12, 2009 at 10:50 Comment(3)
No my bean is not inheriting AbstarctController. The bean is : <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "springframework.org /dtd/spring-beans.dtd"> <beans> <bean id="remoteService" class="gov.nih.nci.system.comm.server.ApplicationServiceServerImpl" /> </beans> But i am not getting RequestContextHolder in my classpath. I am using spring 1.2.7Spermatocyte
I'm running my code on localhost and later on server side. Server side its working perfectly. But localhost is return 0:0:0:0:0:0:0:1 and even using getLocalAddr()Tele
"it should be able to get direct access to the request object" -- what does this mean?Shepley
F
8

The best way to get client ip is to loop through the headers

   private static final String[] IP_HEADER_CANDIDATES = {
        "X-Forwarded-For",
        "Proxy-Client-IP",
        "WL-Proxy-Client-IP",
        "HTTP_X_FORWARDED_FOR",
        "HTTP_X_FORWARDED",
        "HTTP_X_CLUSTER_CLIENT_IP",
        "HTTP_CLIENT_IP",
        "HTTP_FORWARDED_FOR",
        "HTTP_FORWARDED",
        "HTTP_VIA",
        "REMOTE_ADDR" };

public static String getClientIpAddress(HttpServletRequest request) {
    for (String header : IP_HEADER_CANDIDATES) {
        String ip = request.getHeader(header);
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            return ip;
        }
    }
    return request.getRemoteAddr();
}
Frankhouse answered 7/6, 2016 at 7:4 Comment(0)
N
-1

Construct this:

@Autowired(required = true)
private HttpServletRequest  request;

and use like this:

request.getRemoteAddr()
Nearby answered 12/11, 2014 at 14:21 Comment(1)
This does not work for beans in general: ERROR No qualifying bean of type 'javax.servlet.http.HttpServletRequest' available: expected at least 1 bean which qualifies as autowire candidate.Lophophore

© 2022 - 2024 — McMap. All rights reserved.