Expand my Community achievements bar.

SOLVED

AEM 6.3 resourceresolver.adaptto(session.class) issue

Avatar

Level 4

I am trying to create session using resourceresolver and system user. I am able to get the session object but when I try to fetch the userid from session, it shows anonymous instead of system user used. Below is the code:

resolver = resourceResolverFactory.getResourceResolver(params);

session = resolver.adaptTo(Session.class);

String userId = session.getUserID();   // returns anonymous user instead of system user

This code is a part of OSGI servlet which invokes for specific paths.

Is there something wrong in the code or it is a bug?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi,

It is not a bug.

I tired in AEM 6.3 Publish instance, its world fine and I am able to get session from subservience user.

Screen Shot 2018-08-02 at 7.11.15 PM.png

Your 3 line of code is also looks ok. May be wrong with service user permission or mapping.

You can check below sample code

aem63app-repo/SeesionInServletWithSubservice.java at master · arunpatidar02/aem63app-repo · GitHub

Thanks

Arun



Arun Patidar

View solution in original post

6 Replies

Avatar

Correct answer by
Community Advisor

Hi,

It is not a bug.

I tired in AEM 6.3 Publish instance, its world fine and I am able to get session from subservience user.

Screen Shot 2018-08-02 at 7.11.15 PM.png

Your 3 line of code is also looks ok. May be wrong with service user permission or mapping.

You can check below sample code

aem63app-repo/SeesionInServletWithSubservice.java at master · arunpatidar02/aem63app-repo · GitHub

Thanks

Arun



Arun Patidar

Avatar

Level 4

Hi Arun,

I verified the permissions of service user in user mapper and it looks ok. If there was some issue with service user permission then I guess getResourceResolver call should throw loginexception instead of creating a session with anonymous user. One difference I see in my code and your java class is that I am extending SlingAllMethodsServlet instead of SlingSafeMethodServlet otherwise rest everything looks same. And I don't feel that this might be creating any issue here.

Thanks,

Rajeev

Avatar

Level 4

Did you try in Author? Would not it work in author instance? I am trying in author.

Avatar

Level 4

Hi Arun/@smacdonald2008,

I was able to figure out the issue.

We were calling getResourceResolver method on resourceresolverfactory and this is returning anonymous user. If we call get ServiceResourceResolver then it returns proper service user and works like a charm.  Seems like former method works only with  user and password in map not with service user map.

Thanks,

Rajeev