jcr session is returning 'anonymous' user or some else session's data | Community
Skip to main content
Level 2
January 13, 2021
Solved

jcr session is returning 'anonymous' user or some else session's data

  • January 13, 2021
  • 3 replies
  • 3049 views


Hi,

I am geting the jcr session clash/exchanged with other sessions, the logged in user details are replaced with other user's details intermittently or userid is coming as 'anonymous'. Any help for this issue? below is the code i am using in sling model class.

@Model(adaptables = { SlingHttpServletRequest.class })
public class UserLoggedInModel

@SlingObject
ResourceResolver resourceResolver;

@PostConstruct
protected void initialize()
{
String firstNamePath = "./profile/firstName";
String lastNamePath = "./profile/lastName";
try
{

UserManager userManager = resourceResolver.adaptTo(UserManager.class);
Session session = resourceResolver.adaptTo(Session.class);

String userId = session.getUserID(); //returns 'anonymous' userid

User user = (User)userManager.getAuthorizable(userId);

firstName = user.getProperty(firstNamePath)!=null ? user.getProperty(firstNamePath)[0].getString() : "";

lastName = user.getProperty(lastNamePath)!=null ? user.getProperty(lastNamePath)[0].getString() : "";

if(!"anonymous".equals(userId) && !"saml-anonymous-user".equals(userId))
{
isLoggedIn = true;

}
}

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by shivama92274331

Hi, KiranVedantam1992,

with your solution you have adapted resources class also in @adaptables{ Resource.class,
SlingHttpServletRequest.class}

For below request object is it creating from resources class or httpservletrequest class?

 

 slingRequest.getResourceResolver().adaptTo(Session.class); 

 

Thanks,

Shivam

3 replies

Anudeep_Garnepudi
Community Advisor
Community Advisor
January 13, 2021

@shivama92274331 

Try getting ResourceResolver from request object.

@Self SlingHttpServletRequest request; @PostConstruct protected void initialize() { Session session = request.getResourceResolver().adaptTo(Session.class); userId = session.getUserID(); }

-AG

Level 2
January 15, 2021

Hi @anudeep_garnepudi,

I tried it earlier to get the ressourceresolver object from request but it didn't work, this time i made one change i used @self annotation for request object earlier i was using @inject for request object, good part is its worked on stage environment and bad part is it didn't work on Prod environment. No 2 question arises.

 

1) will it really make any difference if we use @self annotation instead @inejct

2) how the same configuration is not woring on Prod while it worked on stage.

 

Along with this change I have added /stickyConnectionsFor "/content/myproect/en-us" configuration , the corresponding "renderid" cookie is generating on stage but not on prod , any clues for it?

 

Regards,

Shivam

Kiran_Vedantam
Community Advisor
Community Advisor
January 13, 2021

To get the user session

slingRequest.getResourceResolver().adaptTo(Session.class);

 

Try this, it is working for me:

 

@Model(adaptables = { Resource.class,
SlingHttpServletRequest.class }, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class UserLoggedInModel{

 

@586265
private ResourceResolver resourceResolver;

 

@PostConstruct
private void init() {
try {
Session session = resourceResolver.adaptTo(Session.class);
userId = session.getUserID();
} catch (Exception e) {
LOG.debug("Error");
}

}

 

Thanks,

Kiran Vedantam.

 

}

shivama92274331AuthorAccepted solution
Level 2
January 15, 2021

Hi, KiranVedantam1992,

with your solution you have adapted resources class also in @adaptables{ Resource.class,
SlingHttpServletRequest.class}

For below request object is it creating from resources class or httpservletrequest class?

 

 slingRequest.getResourceResolver().adaptTo(Session.class); 

 

Thanks,

Shivam

Kiran_Vedantam
Community Advisor
Community Advisor
January 29, 2021
Hi for slingRequest.getResourceResolver().adaptTo(Session.class); object is from httpservletrequest class.