How to use OAuth2 with GraphQL Spring Webclient

Viewed 10

I'm using the GraphQL Spring Webclient and followed the readme, but can't seem to get OAuth 2 to work. I'm trying to get auth from the keycloak server to be able to query the GraphQL API. The readme only mentioned to put the OAuth 2 info into the app yml file, so I'm not sure if I needed to do anything else.

graphql:
  client:
    url: 
    oauth2:
      client-id: 
      client-secret: 
      token-uri: 
      authorization-uri: 
      scope: 
      authorization-grant-type: 
      client-name: 

When sending a request to the GraphQL API, I end up getting 401 unauthorized. I put breakpoints in the GraphQLWebClientAutoConfiguration class as well as the OAuth2ClientRegistrationRepositoryConfiguration class from the package graphql.kickstart.spring.webclient.boot. Both appear to be working as intended, although I did notice that the provider and issuer-uri were null. I wasn't sure if these were required and honestly not sure what I would put in those spots.

When digging into the 401 error, there wasn't any bearer token in the header or body. I'm not sure if I need to create the token or just attach it somehow.

I wasn't sure if I needed to create my own SecurityConfig class, but when I tried doing it, I was running into ClientRegistrationRepository not found errors. They were similar errors to when I left the client-id and client-secret fields empty in the yaml, so I figured I didn't need a SecurityConfig file if the GraphQL Spring Webclient took care of it for me.

If someone could help point me in the right direction or provide examples, that would be much appreciated.

0 Answers
Related