javaangularspring-bootjhipster

JHipster 8.5 get remote IP address from request


I need to obtain the IP of the request that reaches the Resource for business validation purposes. Please help if someone has done it.

I have tried:

String ipAddress = request.getRemoteAddr();
String ipAddress1 = request.getHeader("X-Forwarded-For");

But in the first case, it returns the address 0:0:0:0:0:0:0:1, and in the second case it returns null.


Solution

  • Looks like your application is behind a proxy,

    Couple of things you need to do.

    1. make sure the proxy you're using is forwarding the headers you need, EG config in nginx:
    location / {
    
            proxy_pass http://localhost:8080;
            proxy_read_timeout 7200s;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            client_max_body_size 200M;
        }
    
    1. If above doesnot work, try adding following config in addition to above to your spring properties

    server.forward-headers-strategy= native