从后端容器连接到 keycloak 容器时连接被拒绝

Rad*_*ad1 4 spring-security docker spring-boot keycloak docker-compose

我有两个容器后端(Spring Boot 应用程序)和 Keycloak。如果我在本地容器和后端中运行 keycloak :它可以工作

如果它们都在容器中运行,则后端不会启动并显示以下错误:

Failed to instantiate [org.springframework.security.oauth2.jwt.JwtDecoder]: Factory method 'jwtDecoderByIssuerUri' threw exception; nested exception is java.lang.IllegalArgumentException: Unable to resolve the Configuration with the provided Issuer of "http://keycloak:8082/auth/realms/myrealm"

Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://keycloak:8082/auth/realms/myrealm/.well-known/openid-configuration": Connection refused (Connection refused); 
Run Code Online (Sandbox Code Playgroud)

以下是我的配置:

docker-compose:

services:

  keycloak:
    image: jboss/keycloak:8.0.1
    command:
      - " -b 0.0.0.0"
    container_name: "keycloak"
    networks:
      - myproject
    volumes:
      - "./keycloak/realm-export.json:/opt/jboss/keycloak/bin/keycloak_export_dir/realm-export.json"
    environment:
      KEYCLOAK_USER: admin
      KEYCLOAK_PASSWORD: admin
      KEYCLOAK_IMPORT: /opt/jboss/keycloak/bin/keycloak_export_dir/realm-export.json
    ports:
      - "8082:8080"

  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: "backend"
    environment:
      - spring.oauth2.resourceserver.jwt.issuer-uri= http://keycloak:8082/auth/realms/myrealm
    links:
      - keycloak
    networks:
      - myproject
    restart: on-failure
    ports:
      - "8080:8080"

networks:
  myproject:
    driver: bridge
Run Code Online (Sandbox Code Playgroud)

应用程序.yml:

  application:
    name: backend
  security:
    oauth2:
      resourceserver:
        jwt:
          issuer-uri: http://localhost:8082/auth/realms/myrealm
Run Code Online (Sandbox Code Playgroud)

您知道为什么我的连接被拒绝吗?任何帮助表示赞赏:)

Evi*_*unk 11

您的 Keycloak 容器使用以下端口配置

ports:
  - "8082:8080"
Run Code Online (Sandbox Code Playgroud)

这意味着:

Keycloak 可以从外部通过 Port 到达8082

但在内部(在这个 docker 网络中),keycloak 只能通过暴露的8080端口访问。因此您的后端应用程序需要(内部)连接到http://keycloak:8080