Sample Impersonation portlet

WebSphere Portal 6.1.5 has a feature called Impersonation that you can use to create a portlet that will let support person impersonate other users.

I built a sample portlet to demonstrate how to use impersonation service. This sample portlet has a form where you can enter uid of the user that you want to impersonate, once you enter uid and click submit, it will impersonate that user. Once your done and want to switch back to the original user you can click on Switch back to original user link. You can download this sample portlet from here

Important Note: I followed the documentation to build this portlet and i could get impersonation part working but switching back to original user is not working. Even after calling ImpersonationService.loginOriginalUser() method it still keeps showing impersonated user. But when i click on logout, it starts showing the original user. I got this problem on WebSphere Portal Version wp6103_201_01 2009-11-07. I am planning to apply fixpack to my portal and see if it helps

This is how my looks like

package com.webspherenotes.misc;


import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.ProcessAction;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;


public class ImpersonationPortlet extends GenericPortlet{

PortletServiceHome psh;
public void init() throws PortletException {
System.out.println("Entering ImpersonationPortlet.init()");
try {
InitialContext context = new InitialContext();
psh= (PortletServiceHome)context.lookup(ImpersonationService.JNDI_NAME);
} catch (NamingException e) {
System.out.println("Exiting ImpersonationPortlet.init()");

protected void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
System.out.println("Entering ImpersonationPortlet.init()");

getPortletContext().getRequestDispatcher("/index.jsp").include(request, response);
System.out.println("Exiting ImpersonationPortlet.init()");

public void switchUser(ActionRequest request, ActionResponse response)
throws PortletException, IOException {
System.out.println("Entering ImpersonationPortlet.switchUser()");
try {
String userId = request.getParameter("USER_NAME");
System.out.println("Value of USER_NAME " + userId);
ImpersonationService impersonationService = psh.getPortletService(ImpersonationService.class);
impersonationService.doImpersonate(request, response, userId);
} catch (ImpersonationException e) {
System.out.println("Exiting ImpersonationPortlet.switchUser()");

public void originalUser(ActionRequest request, ActionResponse response) throws PortletException, IOException{
System.out.println("Entering ImpersonationPortlet.originalUser()");
try {

ImpersonationService impersonationService = psh.getPortletService(ImpersonationService.class);
System.out.println("Switching back to original user " + impersonationService.getOriginalUser());
System.out.println("Is User Impersonated " + impersonationService.isUserImpersonated());
impersonationService.loginOriginalUser(request, response);
System.out.println("After switching back to original user " + request.getRemoteUser());
} catch (ImpersonationException e) {
System.out.println("Exiting ImpersonationPortlet.originalUser()");

The ImpersonationPortlet has following two methods that can handle the Action Request,

  1. switchUser: method will get called whenever user enters a uniqueName for the user that you want to impersonate and click submit. In this method i am reading name the value submitted by user and then calling impersonationService.doImpersonate() method with userName submitted by the user. THis method will switch the user and redirect you to the home page for that user

  2. originalUser: THis method will get called when user clicks on Switch back to original user link, at this point i am calling impersonationService.loginOriginalUser(request, response) method to switch back to the original user.

This is the jsp that gets displayed to the user in the VIEW mode and has methods for impersonating and switching back the user.

<%@page language="java" %>
<%@taglib uri="" prefix="portlet"%>
<portlet:defineObjects />

String userId = request.getRemoteUser();
<portlet:actionURL var="switchUserUrl">
<portlet:param name="javax.portlet.action" value="switchUser" />
<h4>Current User - <%=userId %></h4>
<h4>Enter name of the user to impersonate</h4>
<form method="post" action="<%=switchUserUrl %>">
<td>User Id</td>
<td><input type="text" name="USER_NAME" /></td>

<td><input type="submit" name="submit" /></td>


<portlet:actionURL var="originalUserUrl">
<portlet:param name="javax.portlet.action" value="originalUser" />

<h4>Click on URL to switch back to Original User</h4>
<a href='<%=originalUserUrl %>' >Switch back to original user</a>

This is the screen shot of the impersonation portlet


Vikas Chaudhary said...

Battery Mantra is Authorized exide car battery dealer in Noida and Greater Noida. We are providing our service in Indirapuram, Delhi, Ashok Nagar.

Exide Battery Dealer in Noida
Battery Dealer in Noida
Authorized Battery Dealer in Noida
Car Battery Dealer in Noida
Car Battery Dealer
Exide Battery Dealer

srjwebsolutions said...

We are leading responsive website designing and development company in Noida.
We are offering mobile friendly responsive website designing, website development, e-commerce website, seo service and sem services in Noida.

Responsive Website Designing Company in Noida
Website Designing Company in Noida
SEO Services in Noida
SMO Services in Noida

EG MEDI said... is online medical store pharmacy in laxmi nagar Delhi. You can Order prescription/OTC medicines online.
Cash on Delivery available. Free Home Delivery

Online Pharmacy in Delhi
Buy Online medicine in Delhi
Online Pharmacy in laxmi nagar
Buy Online medicine in laxmi nagar
Onine Medical Store in Delhi
Online Medical store in laxmi nagar
Online medicine store in delhi
online medicine store in laxmi nagar
Purchase Medicine Online
Online Pharmacy India
Online Medical Store