繁体   English   中英

如何在 Micronaut GraphQL 中使用 Keycloak JWT 进行身份验证

[英]How to use Keycloak JWT for authentication in Micronaut GraphQL

我正在尝试通过 keycloak JWT 使用 Micronaut GraphQL。 我能够使用 Basic Auth 进行处理,尝试转而使用不记名令牌,但我遗漏了一些东西,因为我总是收到 401 Unauthorized,但我没有在日志中看到任何有用的错误消息,即使将日志记录设置为 TRACE

使用 Micronaut 3.0.0。

我的 application.yml 看起来像这样:

micronaut:
  application:
    name: myapp
  server:
    cors:
      enabled: true
    port: 8080
  security:
    authentication: bearer
    intercept-url-map:
      - pattern: /graphiql
        access:
          - isAnonymous()
      - pattern: /graphql
        access:
          - isAuthenticated()
    endpoints:
      login:
        enabled: false
    token:
      jwt:
        enabled: true
        signatures:
          jwks:
            keycloak:
              url: http://xx.xx.xx.xx:8090/auth/realms/myrealm/protocol/openid-connect/certs
    oauth2.clients.keycloak:
      grant-type: password
      client-id: myapp-backend
      client-secret: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
      authorization:
        url:  http://xx.xx.xx.xx:8090/auth/realms/myrealm/protocol/openid-connect/auth
custom:
  keycloak:
    url: http://xx.xx.xx.xx:8090

graphql:
  enabled: true
  path: /graphql
  graphiql:
    enabled: true
    path: /graphiql

这是我要发布的测试内容:

curl --location --request POST 'localhost:8080/graphql' \
--header 'Authorization: Bearer {exceptionally long jwt token}' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"query test { scenarios { id } }","operationName":"test"}'

我不确定还有什么是有用的。 有什么想法吗?

我对 Micronaut 了解不多,但这不是缺少这样的 openid 配置:

micronaut:
  security:
    oauth2.clients.keycloak.openid:
      issuer: http://xx.xx.xx.xx:8090/auth/realms/myrealm

在调试器中进行更多搜索和逐步检查后,我终于确定我输入了错误的域名。

但是,对于后代,这是我需要运行的最低配置:

micronaut:
  application:
    name: myapplication
  server:
    cors:
      enabled: true
    port: 8080
  security:
    enabled: true
    authentication: bearer
    intercept-url-map:
      - pattern: /graphiql
        access:
          - isAnonymous()
      - pattern: /graphql
        access:
          - isAuthenticated()
    endpoints:
      login:
        enabled: false
    token:
      jwt:
        enabled: true
        signatures:
          jwks:
            keycloak:
              url: http://xx.xx.xx.xx:8090/auth/realms/MyRealm/protocol/openid-connect/certs
    oauth2.clients.keycloak:
      grant-type: password
      client-id: myapp-backend
      client-secret: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
      authorization:
        url: http://xx.xx.xx.xx:8090/auth/realms/MyRealm/protocol/openid-connect/auth

custom:
  keycloak:
    url: http://xx.xx.xx.xx:8090/auth/realms/MyRealm

graphql:
  enabled: true
  graphiql.enabled: true
  graphql-ws.enabled: true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM