Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

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

Avatar

Avatar
Boost 1
Level 1
rajank91603111
Level 1

Like

1 like

Total Posts

2 posts

Correct Reply

0 solutions
Top badges earned
Boost 1
View profile

Avatar
Boost 1
Level 1
rajank91603111
Level 1

Like

1 like

Total Posts

2 posts

Correct Reply

0 solutions
Top badges earned
Boost 1
View profile
rajank91603111
Level 1

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
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,408 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,408 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

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
Boost 1
Level 1
rajank91603111
Level 1

Like

1 like

Total Posts

2 posts

Correct Reply

0 solutions
Top badges earned
Boost 1
View profile

Avatar
Boost 1
Level 1
rajank91603111
Level 1

Like

1 like

Total Posts

2 posts

Correct Reply

0 solutions
Top badges earned
Boost 1
View profile
rajank91603111
Level 1

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
Validate 1
Level 2
rawvarun
Level 2

Likes

11 likes

Total Posts

38 posts

Correct Reply

5 solutions
Top badges earned
Validate 1
Give Back 5
Give Back 3
Give Back
Ignite 3
View profile

Avatar
Validate 1
Level 2
rawvarun
Level 2

Likes

11 likes

Total Posts

38 posts

Correct Reply

5 solutions
Top badges earned
Validate 1
Give Back 5
Give Back 3
Give Back
Ignite 3
View profile
rawvarun
Level 2

04-03-2019

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

Avatar

Avatar
Validate 1
Level 3
trc41594544
Level 3

Likes

16 likes

Total Posts

43 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Ignite 5
Ignite 3
Ignite 1
Give Back 5
View profile

Avatar
Validate 1
Level 3
trc41594544
Level 3

Likes

16 likes

Total Posts

43 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Ignite 5
Ignite 3
Ignite 1
Give Back 5
View profile
trc41594544
Level 3

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
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

274 likes

Total Posts

320 posts

Correct Reply

87 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

274 likes

Total Posts

320 posts

Correct Reply

87 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
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
Validate 1
Level 3
trc41594544
Level 3

Likes

16 likes

Total Posts

43 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Ignite 5
Ignite 3
Ignite 1
Give Back 5
View profile

Avatar
Validate 1
Level 3
trc41594544
Level 3

Likes

16 likes

Total Posts

43 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Ignite 5
Ignite 3
Ignite 1
Give Back 5
View profile
trc41594544
Level 3

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
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

274 likes

Total Posts

320 posts

Correct Reply

87 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

274 likes

Total Posts

320 posts

Correct Reply

87 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
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
Validate 1
Level 3
trc41594544
Level 3

Likes

16 likes

Total Posts

43 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Ignite 5
Ignite 3
Ignite 1
Give Back 5
View profile

Avatar
Validate 1
Level 3
trc41594544
Level 3

Likes

16 likes

Total Posts

43 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Ignite 5
Ignite 3
Ignite 1
Give Back 5
View profile
trc41594544
Level 3

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