# Spring OAuth2 Keycloak Kubernetes internal/external access

**URL:** <https://forum.keycloak.org/t/spring-oauth2-keycloak-kubernetes-internal-external-access/5082>\
**Category:** Getting advice\
**Created:** [September 28, 2020, 9:56am UTC](https://forum.keycloak.org/t/spring-oauth2-keycloak-kubernetes-internal-external-access/5082 "2020-09-28T09:56:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhapy](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@jhapy](https://forum.keycloak.org/u/jhapy)\
**Post date:** [September 28, 2020, 9:56am UTC](https://forum.keycloak.org/t/spring-oauth2-keycloak-kubernetes-internal-external-access/5082/1 "2020-09-28T09:56:24Z")

</div>

I have Keycloak (10.0.3) server configured inside a Kubernetes Cluster.

The keycloak server has to handle authentification for external user (using an external url) and also handle oauth2 token for Spring microservices communications.

Then web application spring services uses oidc providers :

```
spring:
  security:
    oauth2:
      client:
        provider:
          oidc:
            issuer-uri: xxx://keycloak-cluster-http.keycloak-cluster.svc.cluster.local/auth/realms/myrealm
            authorization-uri: http://keycloak-cluster-http.keycloak-cluster.svc.cluster.local/auth/realms/myrealm/protocol/openid-connect/auth
            jwk-set-uri: xxx://keycloak-cluster-http.keycloak-cluster.svc.cluster.local/auth/realms/myrealm/protocol/openid-connect/certs
            token-uri: xxx://keycloak-cluster-http.keycloak-cluster.svc.cluster.local/auth/realms/myrealm/protocol/openid-connect/token
            user-name-attribute: preferred_username

```

The external URL of keycloak is xxx://keycloak.localhost, managed by ingress redirection handled by Traefik v2

```
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  name: keycloak-https
  namespace: keycloak-cluster
  annotations:
    traefik.frontend.passHostHeader: "true"
spec:
  entryPoints:
    - websecure
  routes:
    - match: Host(`keycloak.localhost`)
      kind: Rule
      services:
        - name: keycloak-cluster-http
          port: 80
  tls:
    options:
      name: mytlsoption
      namespace: traefik
    store:
      name: default

```

I can access Keycloak using xxx://keycloak.localhost, no problem, it works.

The problem is that when I try to access my web application, it will always redirect to ‘xxx://keycloak-cluster-http.keycloak-cluster.svc.cluster.local/auth/realms/myrealm’, which is not resolved outside k8s.

If I change issuer-uri to [http://keycloak.localhost](http://keycloak.localhost) then it doesn’t work as keycloak.locahost is not resolved inside k8s.

I tried to set the KEYCLOAK\_FRONTEND\_URL to [https://keycloak.localhost/auth](https://keycloak.localhost/auth), but no change.

Please, does someone has the same kind of settings and managed to make it working ?

Best regards

---

<div class="post-metadata">

**Author:** ![jhapy](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@jhapy](https://forum.keycloak.org/u/jhapy)\
**Post date:** [September 30, 2020, 8:26am UTC](https://forum.keycloak.org/t/spring-oauth2-keycloak-kubernetes-internal-external-access/5082/2 "2020-09-30T08:26:11Z")

</div>

Managed to fix it using coredns and adding a rewrite rule… :

**rewrite name keycloak.localhost keycloak-cluster-http.keycloak-cluster.svc.cluster.local**

```
apiVersion: v1
data:
  Corefile: |
    .:53 {
        errors
        health
        ready
        kubernetes cluster.local in-addr.arpa ip6.arpa {
           pods insecure
           fallthrough in-addr.arpa ip6.arpa
           ttl 30
        }
        rewrite name keycloak.localhost keycloak-cluster-http.keycloak-cluster.svc.cluster.local
        prometheus :9153
        forward . /etc/resolv.conf
        cache 30
        loop
        reload
        loadbalance
    }
kind: ConfigMap
metadata:
  name: coredns
  namespace: kube-system
```

---

<div class="post-metadata">

**Author:** ![gadi-textelvr](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/gadi-textelvr/32/1898_2.png) [@gadi-textelvr](https://forum.keycloak.org/u/gadi-textelvr)\
**Post date:** [October 1, 2020, 10:32am UTC](https://forum.keycloak.org/t/spring-oauth2-keycloak-kubernetes-internal-external-access/5082/3 "2020-10-01T10:32:07Z")

</div>

just out of curiosity why didn’t you use keycloak-spring-boot-starter (example in [https://www.baeldung.com/spring-boot-keycloak](https://www.baeldung.com/spring-boot-keycloak))?

---

<div class="post-metadata">

**Author:** ![jhapy](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@jhapy](https://forum.keycloak.org/u/jhapy)\
**Post date:** [October 1, 2020, 10:45am UTC](https://forum.keycloak.org/t/spring-oauth2-keycloak-kubernetes-internal-external-access/5082/4 "2020-10-01T10:45:34Z")

</div>

First, in order to be independent of any OAuth2 provider and second to be independent regarding client-lib version of Keycloak.
