当前位置: 首页 > 知识库问答 >
问题:

登录到Spring Cloud Api网关后面的Keyclope

束俊材
2023-03-14

我有Keycloak和一个运行在Spring Cloud API Gateway后面的微服务。我想允许客户端应用程序执行一个 /login的HTTP请求,让用户使用他们的用户名和密码登录。

因为客户端应用程序将使用密码授予类型,通过REST API对Keycloak进行身份验证,所以它会将HTTP Post请求发送到Keycloak /tokenendpoint

http://localhost:8080/auth/realms/<realm>/protocol/openid-connect/token

我希望此请求通过Spring Cloud API Gateway进行。所以我配置了一个路由,但由于某种原因,即使Keycloak正在运行并且endpoint正确,我也会出现404未找到错误。直接将HTTP Post请求发送到上述endpoint,而无需通过API网关,可以工作。我想知道为什么我会收到404错误?

以下是我的Spring Cloud API网关路由配置:

server.port=8085
spring.application.name=my-gateway-service  

spring.security.oauth2.client.provider.custom.issuer-uri = http://localhost:8080/auth/realms/myrealm
spring.security.oauth2.client.provider.custom.user-name-attribute = preferred_username
spring.security.oauth2.client.provider.custom2.user-info-uri = http://localhost:8080/auth/realms/myrealm/protocol/openid-connect/userinfo

spring.security.oauth2.client.registration.custom.client-name=my-spring-cloud-api-gateway
spring.security.oauth2.client.registration.custom.client-id = my-spring-cloud-api-gateway
spring.security.oauth2.client.registration.custom.client-secret = 3c832be7-2950-4490-801e-276b874d3dd3
spring.security.oauth2.client.registration.custom.scope = profile, openid, roles
spring.security.oauth2.client.registration.custom.authorization-grant-type = password


spring.cloud.gateway.routes[0].id = token-service
spring.cloud.gateway.routes[0].uri = http://localhost:8080/auth/realms/myrealm/protocol/openid-connect/token
spring.cloud.gateway.routes[0].predicates[0]=Path=/token-service
spring.cloud.gateway.routes[0].predicates[1]=Method=POST

如果我通过API网关发送HTTP Post请求,我将得到404 Not found错误。

curl --location --request POST 'http://localhost:8085/token-service' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'username=username' \
--data-urlencode 'password=mypassword' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'client_id=my-spring-cloud-api-gateway' \
--data-urlencode 'client_secret=3c832be7-2950-4490-801e-276b874d3dd3'

如果我将Spring Cloud API Gateway配置为将此HTTP Post请求路由到localhost上运行的另一个微服务,则一切正常。如果我不使用API Gateway并将上述HTTP Post请求直接发送到Keycloakendpointhttp://localhost:8080/auth/realms/myrealm/protocol/openid-connect/token,那么我将获得访问令牌。因此Keycloak正在运行,并且OAuth客户端应用程序配置为使用密码授予。

只有通过API网关发送上述请求并使网关将请求路由到Keyclopeendpoint时,才会发生404错误代码http://localhost:8080/auth/realms/myrealm/protocol/openid-连接/令牌

我做错了什么?

谢谢你!

共有1个答案

景才英
2023-03-14

我使用SetPath过滤器:

spring.cloud.gateway.routes[0].id=authToken
spring.cloud.gateway.routes[0].uri=http://localhost:8080
spring.cloud.gateway.routes[0].predicates[0]=Path=/auth/token
spring.cloud.gateway.routes[0].predicates[0]=Method=POST
spring.cloud.gateway.routes[0].filters[0]=SetPath=/auth/realms/myrealm/protocol/openid-connect/token
 类似资料:
  • 记录器文件中的日志- org.springframework.Security.Access.event.loggerlistener-安全授权失败,原因是:org.springframework.Security.Access.accessdeniedexception:访问被拒绝;通过身份验证的主体:org.springframework.security.authentication.ano

  • 我正在学习如何使用SpringCloud构建API网关。我已经浏览了有关如何传递参数的文档,所有示例似乎都显示了它们是硬编码的。但如果我有一个动态值呢? 例如,我有这种类型的请求:http://localhost:8080/people/lookup?searchKey=jdoe, 如何通过“jdoe”部分? 我尝试了下面的代码,只有当我硬编码代码中的值时,它才能工作。 我不确定如何使用提供的生成

  • 我在Laravel5.2中使用了php artisan函数。 我想重定向的客人登录页面,若客人点击链接,只有用户并没有客人。 我想在登录后将用户重定向到后面的页面。 我怎么能这么做?请详细展示一些文件名示例。 ///////编辑 路线 控制器 Kernel.php

  • 我使用的是Spring Security 4.1.1,但我遇到了一个问题:我试图访问URL,应用程序会重定向到登录页面。到现在为止,一直都还不错。 但是,成功登录后,应用程序会再次将我重定向到登录页面,并且不会创建任何会话,因此即使尝试直接访问URL(在URL栏中键入),应用程序也会重定向到登录页面。 有一些URL我必须要求登录才能访问它们。其他的,我可以访问无需身份验证。这些我不需要验证的URL

  • 在我的laravel 5.2应用程序中,我正在努力解决我的路由问题。 预期的行为是: 未注册用户试图访问受保护的页面 重定向到登录 重定向(成功)到预期页面 当然,如果没有预期的页面(当用户主动登录时),则应将其重定向到“仪表板” 例子: 用户将被重定向到登录页面,但是,登录后,他将被重定向到我在authcontroller中定义为$redirectTo的页面 有没有办法将重定向设置为类似(int

  • 这是views.py的内容: 在转到“/”时,我被重定向到“/accounts/login”,它将我带到登录页面。我输入用户名和密码后,在“验证”打印语句printd“登录”到终端。 到目前为止还不错。现在,我不再被重定向到“/”,而是再次被重定向到“/accounts/login”,并再次显示输入用户名和密码的页面。为什么?