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

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

Avatar

Avatar
Give Back
Level 2
shivama92274331
Level 2

Likes

3 likes

Total Posts

14 posts

Correct Reply

2 solutions
Top badges earned
Give Back
Boost 3
Boost 1
Affirm 1
View profile

Avatar
Give Back
Level 2
shivama92274331
Level 2

Likes

3 likes

Total Posts

14 posts

Correct Reply

2 solutions
Top badges earned
Give Back
Boost 3
Boost 1
Affirm 1
View profile
shivama92274331
Level 2

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;

}
}

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Give Back
Level 2
shivama92274331
Level 2

Likes

3 likes

Total Posts

14 posts

Correct Reply

2 solutions
Top badges earned
Give Back
Boost 3
Boost 1
Affirm 1
View profile

Avatar
Give Back
Level 2
shivama92274331
Level 2

Likes

3 likes

Total Posts

14 posts

Correct Reply

2 solutions
Top badges earned
Give Back
Boost 3
Boost 1
Affirm 1
View profile
shivama92274331
Level 2

15-01-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

Answers (2)

Answers (2)

Avatar

Avatar
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

271 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

271 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

12-01-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

Avatar

Avatar
Validate 1
Level 7
KiranVedantam1992
Level 7

Likes

161 likes

Total Posts

159 posts

Correct Reply

48 solutions
Top badges earned
Validate 1
Establish
Give Back 3
Give Back
Boost 50
View profile

Avatar
Validate 1
Level 7
KiranVedantam1992
Level 7

Likes

161 likes

Total Posts

159 posts

Correct Reply

48 solutions
Top badges earned
Validate 1
Establish
Give Back 3
Give Back
Boost 50
View profile
KiranVedantam1992
Level 7

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.

 

}