Quarkus 1.7.2 security-jpa NullPointerException on SecurityContext if injected as parameter

Viewed 187

Following the https://quarkus.io/guides/security-jpa guide.

I could not get the @Context SecurityContext when passed as a parameter in my controller.

The build succeeded, The NullPointerException on SecurityContext only happens on hitting the endpoint and when the credentials are valid.

But it worked when I declared it above. Not null anymore. Is this a bug? or is there an explanation of something I missed?

@RestController
@RequestMapping(value = "security" , produces = MediaType.APPLICATION_JSON, consumes = {
    MediaType.APPLICATION_JSON })
public class SecurityDemoController {
  
  
  @GetMapping("/public")
  public String publicPath() {
      return "hello public";
  }

  @Secured("admin")
  @GetMapping("/admin")
  public String adminPath() {
      return "hello admin";
  }


  // worked this way
  @Context
  private SecurityContext sec;


  // did not work when passing it as parameter, was emitting a NullPointerException
  @Secured("user")
  @GetMapping("/me")
  public String me(
  //   @Context
  // SecurityContext securityContext
  ) {
    SecurityContext scheme = sec;
    System.out.println(scheme);
    Principal principal = sec.getUserPrincipal();
    return "hello " + principal.getName();
  }

}

You can see I am using quarkus spring-web and spring-security extensions but I don't think that should pose such problem.

0 Answers
Related