Take client ip on request

Asked

Viewed 5,979 times

1

I have a REST service in my application and in it I want to pick up the IP of the client who is making the call, 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 printo o ip que veio na request printa "127.0.0.1", I am invoking the service that is on a server with the ip 192.168.0.33, internal server only for testing same and my ip is 192.168.0.63, I make the request of my machine right in my entimento should print 192.168.0.62 that is my ip, Voui post the code of the client 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 hint as to why the IP printout is 127.0.01?

1 answer

2


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

If that’s the case, you might 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();
    }
}

Source.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.