Authentication null in custom RealmResourceProvider

I have implemented a very basic hello-world level resource provider, which can be found here:

It is based on the example code found in ExampleRestResource and HelloResourceProvider from the keycloak source code.

I can deploy it and request the resource w/o issue via curl, but when I add an authorization header like so (token truncated):
curl -H “Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5…” https://<my keycloak server>/auth/realms/<my realm>/hello

The response includes a null username, as if the authentication was not evaluated? I think I’m missing something basic somewhere. What did I do wrong?

No longer having issues with this code.

What did you do to resolve? I am not currently also not able to get the user’s current information when passing in the bearer token.

I think the issue was less with the code, and more with user error on my parth, though it’s been a few months…

How are you trying to get the current authentication?

Currently, inside my resource, I use:

   AuthenticationManager.AuthResult auth = new AppAuthManager().authenticateBearerToken(session, session.getContext().getRealm());
    String username = null;
    if (auth != null) {
        username = auth.getUser().getUsername();
    }

Which seems to work for what I need.

I was trying to authenticate against the bearer token and ended up doing something similar. I guess I thought it would have been automatic if that token was presented.