我在Spring网关后面运行了Keycloak和一个Microservice。我希望允许客户端应用程序执行/login HTTP请求,允许用户使用用户名和密码登录。
由于客户端应用程序将使用密码授予类型,通过REST对密钥披风进行身份验证,因此它将向密钥披风/token端点发送HTTP请求
http://localhost:8080/auth/realms/<realm>/protocol/openid-connect/token我希望这个请求通过网关。因此,我已经配置了一个路由,但是由于某种原因,即使密钥披风正在运行并且端点是正确的,也没有找到404错误。直接向上述端点发送HTTP请求,而不通过API网关,可以工作。我想知道为什么我会得到404错误?
下面是我的Spring网关路由配置:
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请求,我将得到404未找到错误。
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网关配置为将此HTTP请求路由到本地主机上运行的另一个微服务,那么一切都正常。如果我不使用API并将上面的HTTP请求直接发送到Keycloak端点http://localhost:8080/auth/realms/myrealm/protocol/openid-connect/token,那么我将得到一个访问令牌。因此,Keycloak正在运行,OAuth客户端应用程序被配置为使用密码授予。
只有当通过API网关发送上述请求并使网关将请求路由到Keycloak端点http://localhost:8080/auth/realms/myrealm/protocol/openid-connect/token时,才会发生404错误代码
我做错什么了?
谢谢!
发布于 2022-01-04 14:16:48
我使用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/tokenhttps://stackoverflow.com/questions/62666825
复制相似问题