May 10, 2020
Estimated Post Reading Time ~

Get UserId of current user in AEM using SlingHttpServletRequest

How to get the current user in AEM using SlingHttpServletRequest:
AEM (Adobe Experience Manager) helps us to manage the content of our site.
It has a user management system that can be accessed under http://localhost:4502/useradmin.
Time to time it becomes a requirement to know the user who is logged in. This is mostly required when
presenting personalized content to the users.

There are two scenarios in this:

1. How to get the user id of the logged-in user in AEM.
2. How to get the user NODE of the logged-in user in AEM.

How to get the user id of the logged-in user in AEM
This is done very easily by using the help of the SlingHttpServletRequest object.
SlingHttpServletRequest object is available for access in component JSP as well as servlets (as a back end OSGI Service).

From Component JSP we can do this:

We can create a component and from the component JSP we can use the SlingHttpServletRequest implicit object "slingRequest" to achieve this.

<%@include file="/libs/foundation/global.jsp"%>
<%@page session="false" %>
<%@page import="org.apache.sling.api.resource.ResourceResolver" %>

<%
ResourceResolver resolver=slingRequest.getResourceResolver();
Session session=resolver.adaptTo(Session.class);
String userId=session.getUserID();
out.println("User Id is: "+userId);
%>

From Servlet:
We can write a servlet/sevice in the OSGI bundle.
Servlet code:

package sample.codermag.net;

import java.io.IOException;
import javax.jcr.Session;
import javax.servlet.ServletException;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;

@Component(metatype = true, immediate = true, label = "CoderMagnet Servlet")
@Service
@Properties({

@Property(name = "sling.servlet.methods", value = { "POST", "GET" }),
@Property(name = "sling.servlet.paths", value = { "/services/userService" }) })
public class UserServlet extends SlingAllMethodsServlet {

@Override
protected void doPost(SlingHttpServletRequest request,
SlingHttpServletResponse response) throws ServletException,
IOException {

String userId = getCurrentUserId(request);
response.getWriter().write(userId);

}

public String getCurrentUserId(SlingHttpServletRequest request) {
ResourceResolver resolver = request.getResourceResolver();
Session session = resolver.adaptTo(Session.class);
String userId = session.getUserID();

return userId;

}
}

How to get the user NODE of the logged-in user in AEM
We may want to go further and get the user node instead of just the ID.
This can also be done both from JSP and Servlet.

From JSP:

<%@include file="/libs/foundation/global.jsp"%>
<%@page session="false" %>

<%@page import="org.apache.jackrabbit.api.security.user.User" %>
<%@page import="org.apache.jackrabbit.api.security.user.UserManager" %>
<%@page import="org.apache.sling.jcr.base.util.AccessControlUtil" %>

<%
UserManager userManager;
Session session = slingRequest.getResourceResolver().adaptTo(Session.class);
userManager = AccessControlUtil.getUserManager(session);

User currentUser = (User) userManager.getAuthorizable(session.getUserID());

out.println(currentUser.getPath());
%>

From Servlet:
package sample.codermag.net;

import java.io.IOException;

import javax.jcr.Session;
import javax.servlet.ServletException;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.jcr.base.util.AccessControlUtil;

@Component(metatype = true, immediate = true, label = "CoderMagnet Servlet")
@Service
@Properties({

@Property(name = "sling.servlet.methods", value = { "POST", "GET" }),
@Property(name = "sling.servlet.paths", value = { "/services/userService" }) })
public class UserServlet extends SlingAllMethodsServlet {

@Override
protected void doPost(SlingHttpServletRequest request,
SlingHttpServletResponse response) throws ServletException,
IOException {

UserManager userManager;
Session session = request.getResourceResolver().adaptTo(Session.class);
userManager = AccessControlUtil.getUserManager(session);

User currentUser = (User) userManager.getAuthorizable(session.getUserID());

// TODO: Code goes here
}

}


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.