Expand my Community achievements bar.

How do we get current AEM author logged in user id in osgi service/handler

Avatar

Level 1

is there way to get current author logged in user in OSGI service/handler  where we don't have slingHttpRequest object.

sling servlet  easily current user id can get via below code (if i have logged to AEM author instance as testuser, USER id gives here testuser)

ResourceResolver resourceResolver = request.getResourceResolver();

session = resourceResolver.adaptTo(Session.class);

String userId = session.getUserID()

LOGGER.info("USER ID :" + userID);

but if same things trying to get in customhandler class which implement AuthenticationFeedbackHandler ,where we don't have slinghttpservletrequest, we have HttpServletRequest.

public boolean authenticationSucceeded(HttpServletRequest request,

HttpServletResponse response, AuthenticationInfo authInfo) {

tried below code, t's give system user id i.e test

Map<String, Object> param = new HashMap<String, Object>();

        param.put(ResourceResolverFactory.SUBSERVICE, "test");

resolver = resourceResolverFactory.getServiceResourceResolver(param);

Session session = resolver.adaptTo(Session.class);

userId = resolver.getUserID();

LOGGER.info("USER ID :" + userId);

LOGGER.info("Session USER ID :" + session.getUserID());

-------------------------------------------------------------------------------------------------------------------------

If i use getAdministractiveresoolver method, it's give user id as admin

adminResolver = resourceResolverFactory.getAdministrativeResourceResolver(null);

Session adminsession = adminResolver.adaptTo(Session.class);

userID = adminsession.getUserID();

LOGGER.info("USER ID :" + userID);

}

8 Replies

Avatar

Level 10

You identified the way --

Session session = resourceResolver.adaptTo(Session.class);
session.getUserID();

more details -- session - AEM - get current UserId - Stack Overflow

Avatar

Level 1

To use  this Session session = resourceResolver.adaptTo(Session.class);

need resourceResolver object which can get it via ResourceResolverFactory ,

below source code to get resource resolver , need to pass param object which will have system user.

resourcerResolver = resourceResolverFactory.getServiceResourceResolver(param);

Session session = resourcerResolver.adaptTo(Session.class);

session.getUserID();  // this give system user id

below code  getAdministrativeResourceResolver method to get resourceResolver,

resourceResolver  = resourceResolverFactory.getAdministrativeResourceResolver(null);

Session session = resourcerResolver.adaptTo(Session.class);

session.getUserID(); // this gives admin user id

any other alternative solution where method have HttpServletRequest

Avatar

Community Advisor

Make a call to /libs/cq/security/userinfo.json in your code after you are loggedin.

Avatar

Level 4

Were you able to figure this out ? I am also trying to figure out how to get user ID from HttpServletRequest. 

 

 

Avatar

Community Advisor

We can directly get the user id from request attribute. Try below line of code.

request.getAttribute("org.osgi.service.http.authentication.remote.user");

Hope this works!

AG

Avatar

Level 4

Hi, 

 

I am getting null for this when i try to get the User using the above method. 

 

What i have is request after OAuth authentication is completed. I had to implement custom Authentication handler and in that we have only HttpServletRequest.

 

To do some processing after the login, request comes back in to Authentication handler, i check for login-token and if it is present i need to write custom business logic. At this point i am unable to get User ID. 

 

Let me know with any thoughts

Avatar

Community Advisor

Hi @trc41594544 

The above line of code will only work for SlingHttpServletRequest. Try getting HttpSession fromHttpServletRequest and print and check if the user information is set in session or else Cookies.

AG

Avatar

Level 4

I do not see anything in Session . Only thing in cookies i see after the login is "login-token". There is no other user related information. 

 

I am also wondering if there is a way to get user id from login-token . I am not able to yet find a way to do that. 

 

Let me know with any thoughts

 

Thanks

Views