在Adobe Experience Manager中,可以通过以下代码示例获取用户信息:
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.SlingHttpServletRequest;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.Page;
import com.adobe.granite.security.user.UserProperties;
public String getCurrentUserId(SlingHttpServletRequest request, ResourceResolverFactory resolverFactory) {
ResourceResolver resolver = null;
try {
resolver = resolverFactory.getAdministrativeResourceResolver(null);
UserProperties userProperties = ((JackrabbitSession) resolver.adaptTo(Session.class)).getUserManager().getAuthorizable(request.getRemoteUser()).getProperties();
return userProperties.get("rep:userId", String.class);
} catch (Exception e) {
// Handle exception
} finally {
if (resolver != null && resolver.isLive()) {
resolver.close();
}
}
return null;
}
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.SlingHttpServletRequest;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.Page;
import com.adobe.granite.security.user.UserProperties;
public ValueMap getCurrentUserProperties(SlingHttpServletRequest request, ResourceResolverFactory resolverFactory) {
ResourceResolver resolver = null;
try {
resolver = resolverFactory.getAdministrativeResourceResolver(null);
UserProperties userProperties = ((JackrabbitSession) resolver.adaptTo(Session.class)).getUserManager().getAuthorizable(request.getRemoteUser()).getProperties();
return userProperties;
} catch (Exception e) {
// Handle exception
} finally {
if (resolver != null && resolver.isLive()) {
resolver.close();
}
}
return null;
}
使用这些代码示例,你可以在Adobe Experience Manager中获取当前用户的用户ID和其他属性。请确保在使用这些代码示例之前已正确配置和初始化Adobe Experience Manager环境。