spring 在localhost中处理K8S群集中的重定向

rjjhvcjd  于 5个月前  发布在  Spring
关注(0)|答案(1)|浏览(70)

我有K8S集群运行在我的主机上(Docker桌面)我有6个组件,每个组件都有自己的部署。
每个节点都有一个服务(Loadbalancer或LoadterIP)

  1. Spring API GW(端口8000上的主机api-gw)
  2. SpringEureka 服务器(端口8001上的主机服务发现)
    1.命令服务(端口8002上的主机命令服务)
    1.产品服务(端口8003上的主机产品服务)
  3. MariaDB(端口3306上的主机数据库)
  4. PHPMyAdmin(host phpmyadmin on port 8004)
    一切正常,而我导航到localhost:8000
    我将autn的keycloak添加到我的系统中,所有配置都在我的spring-apigw中(充当oauth2客户端)

keycloak(host kyecloak on port 8005)

现在,当我导航到localhost:8000时,它会引导我到keycloak:8005(这很好)。
我怎么能保持它作为localhost,所以它的工作(不修改我的hosts文件,我知道这种方式它会工作)
解决方案可以来自spring-gw application.yml文件吗?

server:
  port: 8000

spring:
  application:
    name: api-gateway
  security:
    oauth2:
      client:
        provider:
          keycloak:
            issuer-uri: http://keycloak:8005/auth/realms/myapp
        registration:
          keycloak:
            provider: keycloak
            client-id: my-client
            client-secret: *****
            scope:
              - email
              - profile
              - roles

eureka:
  client:
    service-url:
      defaultZone: http://service-discovery:8001/eureka
  instance:
    hostname: ${spring.application.name}

字符串
还是通过K8S配置?
谢谢你,谢谢

tuwxkamq

tuwxkamq1#

问题是这样的:

client -> server -> oidc discovery  -> http://keycloack/authorize

字符串
我说oidc discovery的地方指的是这个规范,https://swagger.io/docs/specification/authentication/openid-connect-discovery/
您可以直接告诉客户端URL,而不是进行OIDC发现。

client -> server -> http://localhost/authorize


我在blog post中找到了这个例子。解决方案就在里面。
在下面的配置中,您可以看到如何手动设置URI,而不是像您一样只设置颁发者URL并依赖于发现。

pring:
  security:
    oauth2:
      client:
        registration:
          messaging-gateway-oidc:
            provider: gateway-client-provider
            client-id: relive-client
            client-secret: relive-client
            authorization-grant-type: authorization_code
            redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
            scope:
              - openid
              - profile
            client-name: messaging-gateway-oidc
        provider:
          gateway-client-provider:
            authorization-uri: http://127.0.0.1:8080/oauth2/authorize
            token-uri: http://127.0.0.1:8080/oauth2/token
            jwk-set-uri: http://127.0.0.1:8080/oauth2/jwks
            user-info-uri: http://127.0.0.1:8080/userinfo
            user-name-attribute: sub


通过这种方式,您可以将正确的URL发送给用户,而不必在颁发者的发现端点上查找它们。
ODIC,查找将导致相同的URI,我们在上面的例子中看到,除了具有颁发者URL,主机名。这正是你不想要的。
因此,通过手动设置URI,您可以拥有更多的控制权,并可以按照您想要的方式进行设置。
只要确保你的ingress控制器理解一个没有主机名的请求,并路由到你的oauth2服务。
对于URI,我指的是这些属性

而不是:

  • 发布者-uri:http://keycloak:8005/auth/realms/myapp

请注意,这只是概念上的解决方案。如何做这个具体的事情与您的堆栈的选择,我不知道,但这是你必须做的。

相关问题