How to extract IP Address in Spring MVC Controller get call?

后端 未结 9 537
长发绾君心
长发绾君心 2020-11-28 04:04

I am working on Spring MVC controller project in which I am making a GET URL call from the browser -

Below is the url by which I am making a GET call from the browse

相关标签:
9条回答
  • 2020-11-28 04:52

    See below. This code works with spring-boot and spring-boot + apache CXF/SOAP.

        // in your class RequestUtil
        private static final String[] IP_HEADER_NAMES = { 
                                                            "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 getRemoteIP(RequestAttributes requestAttributes)
        {
            if (requestAttributes == null)
            {
                return "0.0.0.0";
            }
            HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
            String ip = Arrays.asList(IP_HEADER_NAMES)
                .stream()
                .map(request::getHeader)
                .filter(h -> h != null && h.length() != 0 && !"unknown".equalsIgnoreCase(h))
                .map(h -> h.split(",")[0])
                .reduce("", (h1, h2) -> h1 + ":" + h2);
            return ip + request.getRemoteAddr();
        }
    
        //... in service class:
        String remoteAddress = RequestUtil.getRemoteIP(RequestContextHolder.currentRequestAttributes());
    

    :)

    0 讨论(0)
  • 2020-11-28 04:52

    In my case, I was using Nginx in front of my application with the following configuration:

    location / {
         proxy_pass        http://localhost:8080/;
         proxy_set_header  X-Real-IP $remote_addr;
         proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header  Host $http_host;
         add_header Content-Security-Policy 'upgrade-insecure-requests';
    }
    

    so in my application I get the real user ip like so:

    String clientIP = request.getHeader("X-Real-IP");
    
    0 讨论(0)
  • 2020-11-28 04:54

    Below is the Spring way, with autowired request bean in @Controller class:

    @Autowired 
    private HttpServletRequest request;
    
    System.out.println(request.getRemoteHost());
    
    0 讨论(0)
提交回复
热议问题