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);
}
Views
Replies
Total Likes
You identified the way --
Session session = resourceResolver.adaptTo(Session.class);
session.getUserID();
more details -- session - AEM - get current UserId - Stack Overflow
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
Make a call to /libs/cq/security/userinfo.json in your code after you are loggedin.
Were you able to figure this out ? I am also trying to figure out how to get user ID from HttpServletRequest.
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
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
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
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
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies