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;
}
}
Solved! Go to Solution.
Views
Replies
Total Likes
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
Try getting ResourceResolver from request object.
@Self
SlingHttpServletRequest request;
@PostConstruct
protected void initialize() {
Session session = request.getResourceResolver().adaptTo(Session.class);
userId = session.getUserID();
}
-AG
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
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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.
}
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
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies