Search code examples
spring-bootspring-securityspring-security-oauth2

How to get OidcUser in spring-resource-server instead just Jwt in @AuthenticationPrincipal?


I have a spring-boot(2.5.0 version) application with org.springframework.boot:spring-boot-starter-oauth2-resource-server dependency and application.properties: spring.security.oauth2.resourceserver.jwt.jwk-set-uri=https://www.googleapis.com/oauth2/v3/certs. When I use @AuthenticationPrincipal annotation it injects org.springframework.security.oauth2.jwt.Jwt. Instead how to get OidcUser?


Solution

  • If in your security configuration the oauth2Login() client is configured with an OIDC provider you can just inject it using @AuthenticationPrincipal.

    @GetMapping("/user")
    public String userInfo(@AuthenticationPrincipal OidcUser oidcUser) {
        ...
    }