OAuth2AuthenticatedPrincipal not loaded after introspect is executed

  Kiến thức lập trình

I have created a Spring Security 6 project with opaque token configuration implemented in resource server. I already have an authorization server deployed both to test/prod environments.
My introspector is invoked from a custom class below:

@Slf4j
public class CustomAuthoritiesOpaqueTokenIntrospector implements OpaqueTokenIntrospector {
    private OpaqueTokenIntrospector delegate;

    public CustomAuthoritiesOpaqueTokenIntrospector(String oauthServerUrl, String clientId, String clientSecret) {
        this.delegate = new NimbusOpaqueTokenIntrospector(oauthServerUrl, clientId, clientSecret);
    }

    public OAuth2AuthenticatedPrincipal introspect(String token) {
        OAuth2AuthenticatedPrincipal principal = this.delegate.introspect(token);
        return new DefaultOAuth2AuthenticatedPrincipal(
                principal.getName(), principal.getAttributes(), extractAuthorities(principal));
    }

    private Collection<GrantedAuthority> extractAuthorities(OAuth2AuthenticatedPrincipal principal) {
        List<String> scopes = principal.getAttribute("scope");
        return scopes.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
    }
}

which I copied from this documentation

For this process, token is fetched from the authorization server, via link http://localhost:9696/oauth/token
which is called separately, in my case via Postman. After I get a token, I call another service by pasting this token to the authorization header.

For example, I call this endpoint: http://localhost:9090/v1/lead-management/get-details/18410 and this endpoint is private and executes only with tokens.
After I execute it, the first thing happens is my introspect is triggered by calling http://localhost:9696/oauth/http://localhost:9696/oauth/check_token in order to validate my token, and then it calls the final endpoint. The token validation is successful, I got all details necessary to prove that my user is authenticated: enter image description here
However, the final endpoint throws 500 error. And here I don’t have a clue what the problem is about and it seems to me that Principal wasn’t mapped or loaded correctly.

I have been dealing with this mess for about 1 month now and still can’t seem to find a solution. I hope you can help to solve this problem. Thanks!

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT