简体   繁体   中英

Is it possible to get the domain or ip address if the request came from?

I am trying to put security in the controller , by checking the domain or ip address from the request and compare it to the domain that is save in the database. But I'm having a problem on how to get the client ip address. I'm using spring-boot java . Is there anyone know how to do it ?

This could help

public static String getRequester(HttpServletRequest request) {
    String forwardHeader= request.getHeader("X-Forwarded-For");
    if (forwardHeader == null) {
        return request.getRemoteAddr();
    } else {
        return new StringTokenizer(forwardHeader, ",").nextToken().trim();
    }
}

Reference 2 - https://www.mkyong.com/java/how-to-get-client-ip-address-in-java/

Reference 1 - https://en.wikipedia.org/wiki/X-Forwarded-For

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM