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

SOLVED

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

shivama92274331
Level 2
Level 2


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;

}
}

1 Accepted Solution
shivama92274331
Correct answer by
Level 2
Level 2

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

View solution in original post

6 Replies
Anudeep_Garnepudi
Community Advisor
Community Advisor

@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

shivama92274331
Level 2
Level 2

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

Anudeep_Garnepudi
Community Advisor
Community Advisor

Hi @shivama92274331

Yes there is difference here. Your model class is already adopted to Request, which means your model class will act as request. To get the similar type (adopted to request and if you want to get request object) you should use @Self not @Inject. This will work in all envs.

KiranVedantam1992
Community Advisor
Community Advisor

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.

 

}

shivama92274331
Correct answer by
Level 2
Level 2

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

View solution in original post

KiranVedantam1992
Community Advisor
Community Advisor
Hi for slingRequest.getResourceResolver().adaptTo(Session.class); object is from httpservletrequest class.