Get client's ip on request

1

I have a REST service in my application and I want to get the IP of the client that is calling, use Spring-mvc in this project, here is the source of the service

/**
 * HttpServletRequest.
 */
@Autowired
private HttpServletRequest request;

@ResponseBody
@RequestMapping("/buscar/{sessionid}/{codUsuario}")
public List<ProdutoVersaoVO> buscarProdutoCliente(
        @PathVariable("sessionid") String sessionid,
        @PathVariable("codUsuario") String codUsuario)
        throws ParseException {
System.out.println(request.getLocalAddr());
System.out.println(request.getRemoteAddr());
}

When I start the ip that came in the request "127.0.0.1", I'm invoking the service that is on a server with ip 192.168.0.33, internal server only for testing and my ip is 192.168.0.63, I do the request of my machine soon in my entiment should print 192.168.0.62 which is my ip, I will post the client code that makes the request

public static List<ProdutoVersaoVO> buscarListaProdutoCliente(String usuarioId) throws IOException {
    final String sessionId = login();

    final HttpURLConnection urlConnection = getHttpUrlConnectionAreaCliente(URL_AREA_CLIENTE
            + "spring/produto/buscar/" + sessionId + "/" + usuarioId);

    urlConnection.setUseCaches(false);
    urlConnection.setDoInput(true);
    urlConnection.setDoOutput(true);

    final String resposta = readStringResponse(urlConnection);

    if (!resposta.isEmpty()) {
        final ObjectMapper mapper = getObjectMapper();
        return mapper.readValue(resposta, new TypeReference<List<ProdutoVersaoVO>>() {
        });
    } else {
        return null;
    }
}

private static String readStringResponse(HttpURLConnection urlConnection) throws IOException {
    final BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
    final StringBuilder str = new StringBuilder("");
    String line = "";

    while ((line = in.readLine()) != null) {
        str.append(line);
    }

    return str.toString();
}

private static HttpURLConnection getHttpUrlConnectionAreaCliente(String enderecoWeb) throws IOException {
    final URL url = new URL(enderecoWeb);
    return (HttpURLConnection) url.openConnection();
}

Any suggestions on why the ip print is 127.0.01?

    
asked by anonymous 17.03.2014 / 17:53

1 answer

2

According to getRemoteAddr documentation , if the client is under a proxy this method will return the address of the proxy, not the actual client. How do clients connect to your server? Is Spring-MVC itself acting as a proxy? (since in that case it would be your own address - 127.0.0.1 or localhost - that would be returned)

If this is the case, you may be able to access the original IP through the header x-forwarded-for (or something similar; check using getHeaderNames ), commonly used by proxies for this purpose:

String ipAddress = req.getHeader("x-forwarded-for");
if (ipAddress == null) {
    ipAddress = req.getHeader("X_FORWARDED_FOR");
    if (ipAddress == null){
        ipAddress = req.getRemoteAddr();
    }
}

Font .

    
17.03.2014 / 18:16