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

Avatar

Avatar

shivama92274331

Avatar

shivama92274331

shivama92274331

12-01-2021


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;

}
}

View Entire Topic

Avatar

Avatar

KiranVedantam1992

Avatar

KiranVedantam1992

KiranVedantam1992

13-01-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{

 

@inject
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.

 

}