簡體   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