<p>I have a keycloak server and a spring boot app running on Kubernetes cluster and I'm facing 2 scenarios the first one is working and the other one I'm getting 401 Unauthorized.</p>
<p>First scenario:</p>
<ul>
<li>Enter a test container with kubectl exec command inside my k8s cluster and ask for a token with a curl POST request :
http://keycloak-service-name:8080/auth/realms/lisa/protocol/openid-connect/token</li>
<li>I get a token</li>
<li>I send a request from postman using that token to the secured endpoint of my app and
It's working, access is granted</li>
</ul>
<p>Second scenario:</p>
<ul>
<li>Ask for a token by sending a POST request to: http://keycloak-external-ip:port/auth/realms/lisa/protocol/openid-connect/token with the necessary parameters from postman</li>
<li>I receive an access token</li>
<li>I send a request from postman to the secured endpoint using that token of my app I get 401 unauthorized</li>
</ul>
<p>The Keycloak is exposed using NodePort service.</p>
<p>The app is exposed using istio ingress gateway.</p>
<p>Keycloak server version:9.0.0.</p>
<p>keycloak version in spring boot app: 10.0.2</p>

Kamèl Romdhani
<p>Fixed this by changing the keycloak.auth-server-url value in the application.properties file.
The keycloak deployment has 2 services, one of type clusterIP, the second is of type nodePort</p>
<p>before</p>
<p><code>keycloak.auth-server-url=http://keycloak-clusterIP-service-name:8080/auth</code></p>
<p>after</p>
<p><code>keycloak.auth-server-url=http://node-ip-adress:nodePort/auth</code></p>
