[keycloak-user] When KeycloakSecurityContext is initialized and available?

Sinan Mustafov sinan.mustafov619 at gmail.com
Tue Nov 28 07:42:34 EST 2017


Hello,

I have backend with JAX-RS REST endpoint secured with bearer token and
angular2 app as frontend (everything is the same like in the quickstarts).

What Im trying to do is to get the KeycloakSecurityContext in
ServletRequestListener, so I can get the authenticated user and initialize
my own context in the backend for the current request.

Here is the code:

@WebListener
public class RequestListener implements ServletRequestListener {

    @Override
    public void requestInitialized(ServletRequestEvent event) {
        ServletRequest servletRequest = event.getServletRequest();
        Object keycloakContext = servletRequest.getAttribute(
KeycloakSecurityContext.class.getName());
        System.out.println("INIT: " + keycloakContext);
    }

    @Override
    public void requestDestroyed(ServletRequestEvent event) {
        ServletRequest servletRequest = event.getServletRequest();
        Object keycloakContext = servletRequest.getAttribute(
KeycloakSecurityContext.class.getName());
        System.out.println("DESTROY: " + keycloakContext);
    }

}

What happens:
In requestInitialized method the context is null, but its available in
requestDestroyed method.

Do you have any idea why is this happening or when the
KeycloakSecurityContext is added to the request?

Regards.


More information about the keycloak-user mailing list