Custom Login Portlet

Starting from WPS 6.1.0.1, IBM WebSPhere Portal Server provides a Login Service that you can use to create a custom login portlet. You may want to create a custom login portlet to IBM WebSphere Portal if the base portlet is not in the format you want, or if you want to add additional checking to the login.

In order to demonstrate how you can use Login Service i did built a CustomLoginPortlet, it shows a Login form to user and when you enter user and password on the form, it will login into portal for you. You can download this sample portlet from here

This is how my CustomLoginPortlet.java looks like

package com.webspherenotes.services;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

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.RenderRequest;
import javax.portlet.RenderResponse;
import javax.security.auth.login.LoginException;

import com.ibm.portal.auth.exceptions.AuthenticationException;
import com.ibm.portal.auth.exceptions.AuthenticationFailedException;
import com.ibm.portal.auth.exceptions.PasswordInvalidException;
import com.ibm.portal.auth.exceptions.PortletLoginDisabledException;
import com.ibm.portal.auth.exceptions.SessionTimeOutException;
import com.ibm.portal.auth.exceptions.SystemLoginException;
import com.ibm.portal.auth.exceptions.UserAlreadyLoggedInException;
import com.ibm.portal.auth.exceptions.UserIDInvalidException;
import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.portlet.service.login.LoginHome;
import com.ibm.portal.portlet.service.login.LoginService;
import com.ibm.websphere.security.WSSecurityException;

public class CustomLoginPortlet extends GenericPortlet{
protected void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
System.out.println("Entering CustomLoginPortlet.doView()");
response.setContentType("text/html");
getPortletContext().getRequestDispatcher("/login.jsp").include(request, response);
System.out.println("Exiting CustomLoginPortlet.doView()");
}

LoginHome loginHome;
public void init() throws PortletException {
System.out.println("Entering CustomLoginPortlet.init()");
try {
InitialContext ctx = new InitialContext();
PortletServiceHome psh = (PortletServiceHome) ctx.lookup(LoginHome.JNDI_NAME);
loginHome = (LoginHome) psh.getPortletService(LoginHome.class);
//loginHome =(LoginHome) ctx.lookup(LoginHome.JNDI_NAME);
} catch (NamingException e) {
e.printStackTrace();
}
System.out.println("Exiting CustomLoginPortlet.init()");
}

public void processAction(ActionRequest request, ActionResponse response)
throws PortletException, IOException {
try {
System.out.println("Entering CustomLoginPortlet.processAction()");
String userId = request.getParameter("USER_NAME");
System.out.println("User Id " + userId);
String password = request.getParameter("PASSWORD");
System.out.println("Password " + password);
LoginService loginService = loginHome.getLoginService(request, response);
System.out.println("Login Service " + loginService);
Map contextMap = new HashMap();
contextMap.put(LoginService.DO_RESUME_SESSION_KEY, new Boolean(false));
loginService.login(userId, password.toCharArray(), contextMap, null);
System.out.println("Exiting CustomLoginPortlet.processAction()");
response.sendRedirect("/wpcert/mydemo");
} catch (PasswordInvalidException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UserIDInvalidException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AuthenticationFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AuthenticationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SessionTimeOutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (PortletLoginDisabledException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UserAlreadyLoggedInException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SystemLoginException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LoginException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WSSecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (com.ibm.portal.auth.exceptions.LoginException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


In the doView() method of the CustomLoginPortlet, i am forwarding control to login.jsp, which displays a form to user. Once user enters user id and password and submits the form, in the processAction() method i am reading those values and calling login() method of the LoginService. The login() method will first login user into the portal and then redirect him to the logged in page.

Once your portlet is ready, deploy it on portal and add User role to anonymous users. Then you have two options either you can change the login page of the portal to add this portlet or create a new page, allow anonymous users access to that page and may be create friendly URL for that page. Then you can try login portlet by opening a new browser instance and accessing the custom login page Url directly and entering userid an password on the custom login portlet

6 comments:

VENKAT iNTURI said...

Sunil,
i have a requirement, where i have login portlet on all the portal pages and couple of other portlets. i implemented a custom login portlet for this . but the problem is once the user logs in, the user is being directed to the default landing page of the portal, my requirement is if the user logs in he should be on the same page, and the login portlet should say welcome XXXX acknowledging that the user is logged in.

any thoughts/suggestions would be greatly appreciated.

PK said...

I think you can use authentication filter for this purpose.

Unknown said...

Can you please elaborate a bit more on how to deploy this custom login portlet on to the server. Any links etc are appreciated. Thanks

ADmin said...

event of informative content that is everything that awful.www.bestcustomessay.org/essays Qualified information has dependably been

Unknown said...

Hi Sunil,
I am new to websphere portal and I have to create customiz login portlet in websphere portlet 8.
Could you please help me out. Drop me a note in my mail id : meetsunilkr@gmail.com

Abhi said...

Thanks for info....
SEO Company in Bangalore