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

Avatar

Avatar

rajank91603111

Avatar

rajank91603111

rajank91603111

04-03-2019

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);

}

Replies

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Reply

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Reply

2.3K
smacdonald2008

04-03-2019

You identified the way --

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

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

Avatar

Avatar

rajank91603111

Avatar

rajank91603111

rajank91603111

04-03-2019

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

Avatar

rawvarun

Avatar

rawvarun

rawvarun

04-03-2019

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

Avatar

Avatar

trc41594544

Avatar

trc41594544

trc41594544

14-11-2020

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

 

 

Avatar

Avatar

Anudeep_Garnepudi

MVP

Avatar

Anudeep_Garnepudi

MVP

Anudeep_Garnepudi
MVP

15-11-2020

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

Avatar

trc41594544

Avatar

trc41594544

trc41594544

16-11-2020

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

Avatar

Anudeep_Garnepudi

MVP

Avatar

Anudeep_Garnepudi

MVP

Anudeep_Garnepudi
MVP

16-11-2020

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

Avatar

trc41594544

Avatar

trc41594544

trc41594544

16-11-2020

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