admin管理员组文章数量:1355096
I have a java web application with tomcat and keycloak for authentication. Both services (the app and Keycloak) are running in separate Docker containers, connected through a docker bridge network
keycloak
service runs athttp://keycloak:8080/auth
inside Docker.- My java app with tomcat exposing ports internal and host on 8090
- The frontend is JS
In my app profile was configured with keycloak.json as:
{
"realm": "${realm}",
"auth-server-url": "http://keycloak:8080/auth",
"ssl-required": "none",
"resource": "${client_id}",
"public-client": true,
"confidential-port": 0
}
I had a problem when trying to access http://localhost:8090/system
, the app redirected the browser to this Keycloak URL:
http://keycloak:8080/auth/realms/tdoc-api/protocol/openid-connect/auth...
Which failed on browser showingERR_CONNECTION_REFUSED
, cause I understand this service can not be resolved externally from the host.
Also I tried changing keycloak.json to:
{
"realm": "${realm}",
"auth-server-url": "http://localhost:8012/auth",
"ssl-required": "none",
"resource": "${client_id}",
"public-client": true,
"confidential-port": 0
}
But my app failed when trying to log in showing a 500 internal server error:
[WARN ] 12:08:46.150 .keycloak.adapters.KeycloakDeployment.resolveUrls() - Failed to load URLs from http://localhost:8012/auth/realms/${realm}/.well-known/openid-configuration
java.ConnectException: Connection refused (Connection refused)
Because it can not resolve from inside the container my service exposed locally in 8012 port. Which I think was the answer provided in this post:
Does anyone know why didnt work for me this solution?
I was able to fix it setting a environment in docker-compose.yml:
- KEYCLOAK_FRONTEND_URL=http://localhost:8012/auth
More context of my docker environment:
version: '3'
services:
app-db:
image: mysql:5.7
ports:
- "3309:3306"
environment:
MYSQL_ROOT_PASSWORD: example
networks:
- my-network
my-app:
build: .
ports:
- "8090:8080"
- "8000:8000"
environment:
- JAVA_OPTS=...
volumes:
- ./app:/usr/local/tomcat/webapps/my-app
depends_on:
- app-db
networks:
- my-network
keycloak:
image: quay.io/keycloak/keycloak:16.1.1
ports:
- "8012:8080"
environment:
- DB_VENDOR=mysql
- DB_ADDR=keycloak-db
- DB_PORT=3306
- DB_USER=keycloak_user
- DB_PASSWORD=keycloak_pass
- DB_DATABASE=keycloak
- PROXY_ADDRESS_FORWARDING=true
- KEYCLOAK_FRONTEND_URL=http://localhost:8012/auth
volumes:
- ./themes:/opt/jboss/keycloak/themes
depends_on:
- keycloak-db
networks:
- my-network
keycloak-db:
image: mysql:5.7
ports:
- "3307:3306"
environment:
- MYSQL_ROOT_PASSWORD=keycloak_pass
- MYSQL_DATABASE=keycloak
- MYSQL_USER=keycloak_user
- MYSQL_PASSWORD=keycloak_pass
volumes:
- ./volumes/mysql:/var/lib/mysql
networks:
- my-network
networks:
my-network:
external: true
name: my-network
I have a java web application with tomcat and keycloak for authentication. Both services (the app and Keycloak) are running in separate Docker containers, connected through a docker bridge network
keycloak
service runs athttp://keycloak:8080/auth
inside Docker.- My java app with tomcat exposing ports internal and host on 8090
- The frontend is JS
In my app profile was configured with keycloak.json as:
{
"realm": "${realm}",
"auth-server-url": "http://keycloak:8080/auth",
"ssl-required": "none",
"resource": "${client_id}",
"public-client": true,
"confidential-port": 0
}
I had a problem when trying to access http://localhost:8090/system
, the app redirected the browser to this Keycloak URL:
http://keycloak:8080/auth/realms/tdoc-api/protocol/openid-connect/auth...
Which failed on browser showingERR_CONNECTION_REFUSED
, cause I understand this service can not be resolved externally from the host.
Also I tried changing keycloak.json to:
{
"realm": "${realm}",
"auth-server-url": "http://localhost:8012/auth",
"ssl-required": "none",
"resource": "${client_id}",
"public-client": true,
"confidential-port": 0
}
But my app failed when trying to log in showing a 500 internal server error:
[WARN ] 12:08:46.150 .keycloak.adapters.KeycloakDeployment.resolveUrls() - Failed to load URLs from http://localhost:8012/auth/realms/${realm}/.well-known/openid-configuration
java.ConnectException: Connection refused (Connection refused)
Because it can not resolve from inside the container my service exposed locally in 8012 port. Which I think was the answer provided in this post: https://stackoverflow/a/74929659
Does anyone know why didnt work for me this solution?
I was able to fix it setting a environment in docker-compose.yml:
- KEYCLOAK_FRONTEND_URL=http://localhost:8012/auth
More context of my docker environment:
version: '3'
services:
app-db:
image: mysql:5.7
ports:
- "3309:3306"
environment:
MYSQL_ROOT_PASSWORD: example
networks:
- my-network
my-app:
build: .
ports:
- "8090:8080"
- "8000:8000"
environment:
- JAVA_OPTS=...
volumes:
- ./app:/usr/local/tomcat/webapps/my-app
depends_on:
- app-db
networks:
- my-network
keycloak:
image: quay.io/keycloak/keycloak:16.1.1
ports:
- "8012:8080"
environment:
- DB_VENDOR=mysql
- DB_ADDR=keycloak-db
- DB_PORT=3306
- DB_USER=keycloak_user
- DB_PASSWORD=keycloak_pass
- DB_DATABASE=keycloak
- PROXY_ADDRESS_FORWARDING=true
- KEYCLOAK_FRONTEND_URL=http://localhost:8012/auth
volumes:
- ./themes:/opt/jboss/keycloak/themes
depends_on:
- keycloak-db
networks:
- my-network
keycloak-db:
image: mysql:5.7
ports:
- "3307:3306"
environment:
- MYSQL_ROOT_PASSWORD=keycloak_pass
- MYSQL_DATABASE=keycloak
- MYSQL_USER=keycloak_user
- MYSQL_PASSWORD=keycloak_pass
volumes:
- ./volumes/mysql:/var/lib/mysql
networks:
- my-network
networks:
my-network:
external: true
name: my-network
Share
Improve this question
asked Mar 28 at 12:57
joshSP95joshSP95
11 bronze badge
1 Answer
Reset to default 0Why
http://keycloak:8080/auth
didn't work?As you noticed, this works only inside Docker, where
keyclock
is resolvable via the internal Docker network. But when the browser gets redirected to that URL, it tries to resolvehttp://keycloak:8080
from your host OS, which fails — becausekeycloak
is not a real hostname on your host machine. That's why it ends up withERR_CONNECTION_REFUSED
error.Why
http://localhost:8012/auth
didn't work?With this
auth-server-url
, your app (inside themy-app
container) tried to connect tolocalhost:8012
, but in container-land,localhost
means "inside this container", not your host machine.So it's trying to find Keycloak inside the same container (where it doesn't exist), and fails with
Connection refused
exception.Why
KEYCLOAK_FRONTEND_URL=http://localhost:8012/auth
works as expected?When you set
KEYCLOAK_FRONTEND_URL
, you told Keycloak itself: "When generating URLs (like the redirect URI), usehttp://localhost:8012/auth
."This works because:
Your Java app talks to Keycloak using the internal
http://keycloak:8080/auth
URL.The browser gets redirected to the correct host-facing URL:
http://localhost:8012/auth
.
That solves both sides:
internal container -> container hostname
browser -> localhost-bound service
本文标签: KeycloakJava App Redirect within DockerStack Overflow
版权声明:本文标题:Keycloak - Java App Redirect within Docker - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744036474a2579863.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论