Closed nictas closed 3 months ago
The @AuthenticationPrincipal
injects whatever comes out of Authentication#getPrincipal
as explained in the reference documentation.
I've changed your sample to use this instead and it works as expected:
@QueryMapping
public String getCurrentUser(@AuthenticationPrincipal User userDetails) {
if (userDetails != null) {
return String.format("Authenticated user %s with class %s", userDetails.getUsername(), userDetails.getClass()
.getName());
}
return "No authenticated user";
}
Let us know if this works for you.
Yes, that works! Thanks for the help! :)
Hi,
I'm trying the inject the principal into the methods of my GraphQL
@Controller
class but I can't seem to get it working. I've created a small project that you can use to reproduce the problem (attached below). It consists of two classes:SecurityConfig:
GraphQLController
Project: graphql-demo.zip
You can use basic authentication with "user" as the username and the password from the application logs. Here's the GraphQL query:
I always get the following response, which indicates that the injected
@AuthenticationPrincipal
is null and the one I get fromSecurityContextHolder
isn't:I'm using version 3.3.2 of the spring-boot-starter-graphql library.