<p>A bit inexperienced at this, so looking for some help on how I can do this! Sorry if it is unclear of what I'm looking to do.</p>
<h1>Objective</h1>
<p>I have an Angular front-end that is location based. I am hoping to be able to use the users public IP by taking it and using a geolocation service to give me the city/region that they are from.</p>
<h1>Update #1</h1>
<p>From one of the answers below, I now am getting an IP address in SpringBoot, but unfortunately it is the IP address of the DigitalOcean droplet.</p>
<h1>Current Setup</h1>
<p>I am using a Spring Security Custom Filter to perform this action. This sits behind the Angular application.</p>
<p>I was hoping that I would be able to use the <code>HttpServletRequest request.getRemoteAddr()</code> to get the IP address, but I have found that once the SpringBoot application is deployed on Kubernetes, which sits behind an NGINX proxy, the getRemoteAddr() gives me the Digital Ocean droplet IP.</p>
<p>Due to this, I was hoping I would be able to pass this client IP address forward as the X-Forwarded-For header, or even a custom X-Client-IP header. How would I go about this if I'm performing these actions as part of a Spring Security Filter? Is it even possible?</p>
<h2>Nginx Config</h2>
<pre><code>location / {
    proxy_set_header    Host               $host;
    proxy_set_header    X-Client-IP        $proxy_add_x_forwarded_for;
    proxy_set_header    X-Real-IP          $remote_addr;
    proxy_set_header    X-Forwarded-For    $proxy_add_x_forwarded_for;
    proxy_set_header    X-Forwarded-Host   $host;
    proxy_set_header    X-Forwarded-Server $host;
    proxy_set_header    X-Forwarded-Port   $server_port;
    proxy_set_header    X-Forwarded-Proto  $scheme;

    rewrite .* /index.html break;
  }
</code></pre>
<h2>Spring Boot Filter</h2>
<pre><code>private static String getClientIpAddr(HttpServletRequest request) {
        String ip = request.getHeader(&quot;X-Forwarded-For&quot;);

        if (ip == null || ip.length() == 0 || &quot;unknown&quot;.equalsIgnoreCase(ip)) {
            ip = request.getHeader(&quot;Proxy-Client-IP&quot;);
        }
        if (ip == null || ip.length() == 0 || &quot;unknown&quot;.equalsIgnoreCase(ip)) {
            ip = request.getHeader(&quot;WL-Proxy-Client-IP&quot;);
        }
        if (ip == null || ip.length() == 0 || &quot;unknown&quot;.equalsIgnoreCase(ip)) {
            ip = request.getHeader(&quot;HTTP_CLIENT_IP&quot;);
        }
        if (ip == null || ip.length() == 0 || &quot;unknown&quot;.equalsIgnoreCase(ip)) {
            ip = request.getHeader(&quot;HTTP_X_FORWARDED_FOR&quot;);
        }
        if (ip == null || ip.length() == 0 || &quot;unknown&quot;.equalsIgnoreCase(ip)) {
            ip = request.getHeader(&quot;X-Real-IP&quot;);
        }
        if (ip == null || ip.length() == 0 || &quot;unknown&quot;.equalsIgnoreCase(ip)) {
            ip = request.getHeader(&quot;X-Client-IP&quot;);
        }
        if (ip == null || ip.length() == 0 || &quot;unknown&quot;.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        return ip;
    }
</code></pre>
<p>All of these return null apart from request.getRemoteAddr() (which returns the loadbalancer IP).</p>
<h2>Kubernetes Setup</h2>
<pre><code>kind: Deployment
apiVersion: apps/v1
metadata:
  name: example-webapp-frontend-deployment
spec:
  revisionHistoryLimit: 3
  minReadySeconds: 30
  replicas: 1
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  selector:
    matchLabels:
      app: example-webapp-frontend
  template:
    metadata:
      labels:
        app: example-webapp-frontend
    spec:
      restartPolicy: Always
      containers:
        - name: example-webapp-frontend
          image: docker-example/example-webapp-frontend:latest
          imagePullPolicy: Always
          ports:
            - containerPort: 80
      imagePullSecrets:
        - name: docker-creds

---

apiVersion: v1
kind: Service
metadata:
  name: example-webapp-frontend-service
spec:
  selector:
    app: example-webapp-frontend
  ports:
    - protocol: TCP
      targetPort: 80
      port: 80

---

kind: Deployment
apiVersion: apps/v1
metadata:
  name: example-webapp-bff-deployment
spec:
  revisionHistoryLimit: 3
  minReadySeconds: 30
  replicas: 1
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  selector:
    matchLabels:
      app: example-webapp-bff
  template:
    metadata:
      labels:
        app: example-webapp-bff
    spec:
      restartPolicy: Always
      containers:
        - name: example-webapp-bff
          image: docker-example/example-webapp-bff:latest
          imagePullPolicy: Always
          ports:
            - containerPort: 9874
      imagePullSecrets:
        - name: docker-creds

---

apiVersion: v1
kind: Service
metadata:
  name: example-webapp-bff-service
spec:
  selector:
    app: example-webapp-bff
  ports:
    - protocol: TCP
      targetPort: 9874
      port: 9874

---

    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      annotations:
        kubernetes.io/ingress.class: &quot;nginx&quot;
        nginx.ingress.kubernetes.io/proxy-read-timeout: &quot;12h&quot;
        nginx.ingress.kubernetes.io/ssl-redirect: &quot;true&quot;
        nginx.ingress.kubernetes.io/force-ssl-redirect: &quot;true&quot;
      name: webapp-ingress
      namespace: default
    spec:
      tls:
        - hosts:
            - example.com
          secretName: example-tls
      rules:
        - host: example.com
          http:
            paths:
              - path: /
                backend:
                  serviceName: webapp-frontend-service
                  servicePort: 80
        - host: example.com
          http:
            paths:
              - path: /api/
                backend:
                  serviceName: webapp-bff-service
                  servicePort: 9874
</code></pre>
<h2>Spring Boot Config</h2>
<pre><code>server.forward-headers-strategy=NATIVE
server.tomcat.remote-ip-header=x-forwarded-for
server.tomcat.internal-proxies=&quot;\
              10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\
              192\\.168\\.\\d{1,3}\\.\\d{1,3}|\
              169\\.254\\.\\d{1,3}\\.\\d{1,3}|\
              127\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\
              172\\.1[6-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\
              172\\.2[0-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\
              172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3}\
              172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3}\
              &lt;digital-ocean-droplet-IP&gt;\
              &lt;digital-ocean-droplet-IP&gt;\
              &lt;digital-ocean-loadbalancer-IP&gt;&quot;
</code></pre>
<h2>Digital Ocean Kubernetes Config</h2>
<p>I have a cluster that has a single pool of two nodes. I have a Digital Ocean load balancer that sits in front of it. Currently running version 1.17.5.,</p>
<p>Anyone able to provide suggestions? Thanks in advance.</p>

Justin Johnson
<p>Spring boot contains a filter to integrate with reverse proxies out of the box and sets the remote address on the request appropriately.
You may need to configure the allowed IPs to accept the header.</p>
<p>Here is an example:</p>
<pre><code>server:
  forward-headers-strategy: native
  tomcat:
    remoteip:
      remote-ip-header: x-forwarded-for
      #private: 10/8, 192.168/16, 169.254/16, 127/8, 172.16/12
      internal-proxies: &quot;\
              10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\
              192\\.168\\.\\d{1,3}\\.\\d{1,3}|\
              169\\.254\\.\\d{1,3}\\.\\d{1,3}|\
              127\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\
              172\\.1[6-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\
              172\\.2[0-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\
              172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3}&quot;
</code></pre>
<p>And here is the documentation: <a href="https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-use-behind-a-proxy-server" rel="nofollow noreferrer">https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-use-behind-a-proxy-server</a></p>
<p>It is important, though, that the right x-forwarded-for header is received by the spring application.
Since you are deploying on kubernetes you are most probably using an Ingress to route external requests to the right kubernetes service.
Ingress is most often implemented using nginx or traefik and must be configured to accept the x-forwarded-for header, if a L7 reverse proxy is used as external load balancer and TLS termination, if applicable.</p>
<p>If using nginx create a config map to configure nginx</p>
<pre><code>use-forwarded-headers: &quot;true&quot;
proxy-real-ip-cidr: &quot;10.0.0.0/8,...&quot;
</code></pre>
<p>When TLS is not terminated externally the external load balancer will not be able to add headers due to the encryption (L3 load balancer). In that case the only option is to use the PROXY protocol which adds meta data to the forwarded tcp stream containing the real remote address.</p>
<p>Example configuration for nginx</p>
<pre><code>use-proxy-protocol: 'true'
</code></pre>
<p>Note that there some issues when using proxy protocol and cert-manager to manage TLS certificates with external (kubernetes) <code>LoadBalancer</code> configurations. (See <a href="https://github.com/kubernetes/kubernetes/issues/66607" rel="nofollow noreferrer">https://github.com/kubernetes/kubernetes/issues/66607</a> and <a href="https://github.com/kubernetes/ingress-nginx/issues/3996" rel="nofollow noreferrer">https://github.com/kubernetes/ingress-nginx/issues/3996</a> )</p>
<p>Once the setup is correct in <em>all</em> places, the remote ip should be set correctly.</p>
<p>When debugging this kind of setup I recommend to begin on the most outer layer by intercepting (ngrep, tcpdump) or logging requests and move forward step by step.</p>
