Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Principal is null when executing requests over WebSocket with Spring MVC #342

Closed
maxhov opened this issue Mar 28, 2022 · 1 comment
Closed
Assignees
Labels
type: bug A general bug
Milestone

Comments

@maxhov
Copy link

maxhov commented Mar 28, 2022

When executing GraphQL requests through websockets, the injected Principal is null. I tried with and without the introduced @AuthenticatedPrincipal annotation, but without a succesful result. Through the HTTP transport, the Principal is injected and everything works as expected.

To demonstrate the issue, I created a demo repo with a basic Spring Boot 2.7.0-m3 application.

To reproduce the issue for the websocket transport with websocat:

~  spring-graphql-principal$ websocat --basic-auth user:password ws://localhost:8080/graphql
**{"type":"connection_init"}**
{"id":null,"type":"connection_ack","payload":{}}
**{"id":"1","type":"subscribe","payload":{"query":"{ helloWorld }"}}**
{"id":"1","type":"next","payload":{"errors":[{"message":"Cannot invoke \"java.security.Principal.getName()\" because \"principal\" is null","locations":[{"line":1,"column":3}],"path":["helloWorld"],"extensions":{"classification":"INTERNAL_ERROR"}}],"data":{"helloWorld":null}}}
{"id":"1","type":"complete","payload":{}}

To see the result for the HTTP transport, use the following curl command:

curl --location --request POST 'http://localhost:8080/graphql' \
--header 'Authorization: Basic dXNlcjpwYXNzd29yZA==' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"{ helloWorld }"}'

Seems to be somewhat related to #268 . I tried to pinpoint where it goes wrong, but couldn't figure it out. My first guess would be that the ThreadLocal variables from e.g. the SecurityContext are not properly transferred to the thread handling the actual query.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 28, 2022
@rstoyanchev rstoyanchev self-assigned this Mar 29, 2022
@rstoyanchev
Copy link
Contributor

Thanks for the sample.

We do attempt to propagate per WebSocket message but the SecurityContext was established on a different thread during the handshake, so we need to bridge but it seems GraphQlWebSocketHandler alone is not sufficient because afterSessionEstablished is also not on the initial thread. We'll likely use a HandshakeInterceptor to capture the context during the handshake and save it in the WebSocketSession attributes, and that'll require a change in the Boot starter as well.

@rstoyanchev rstoyanchev added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 29, 2022
@rstoyanchev rstoyanchev added this to the 1.0.0-RC1 milestone Mar 29, 2022
@rstoyanchev rstoyanchev changed the title Principal is null when executing requests through websockets Principal is null when executing requests over WebSocket with Spring MVC Apr 12, 2022
rstoyanchev added a commit to rstoyanchev/spring-boot that referenced this issue Apr 12, 2022
As a result of changes for
spring-projects/spring-graphql#342,
GraphQlWebSocketHandler now exposes a method to create the
WebSocketHttpRequestHandler, pre-configured with a context propagating
HandshakeInterceptor. This commit updates the autoconfig to use it.
bclozel pushed a commit to spring-projects/spring-boot that referenced this issue Apr 12, 2022
As a result of changes for
spring-projects/spring-graphql#342,
GraphQlWebSocketHandler now exposes a method to create the
WebSocketHttpRequestHandler, pre-configured with a context propagating
HandshakeInterceptor. This commit updates the autoconfig to use it.

Closes gh-30641
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants