Showing posts with label loginportlet. Show all posts
Showing posts with label loginportlet. Show all posts

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

Automatic login with the login URL

If your writing unit tests for portal or you deleted the login portlet from page then it is also possible to log into the portal by using the following URL which includes the user ID and password:

http://server:port/wps/portal/cxml/04_SD9ePMtCP1I800I_KydQvyHFUBADPmuQy?userid=userid&password=password

where you need to replace the variables for server,port, userid, and password with the values set for your environment. For example, th

Delete login portlet how to get it back

If you accidently deleted login portlet from the login page, then you can use xmlaccess to restore it.

Either you can use xmlaccess to export the login page from some other environment and then import it on your server. Or you can use the xmlaccess script in this post to import login portlet.

The way xmlaccess script works is that first you locate the portlet using <web-app action="locate" element. To locate the login portlet you can use the uid for both web application(uid="login.war.webmod"), portlet(uniquename="wps.p.Login"). Portal server reads these values from the web.xml, portlet.xml so unless you went and changed the uid in your login portlet it should not be same on your environment as that used in this post. ONce the login portlet is located <component action="update" element will take care of adding that portlet on the page.




<?xml version="1.0" encoding="UTF-8"?>
<!-- IBM WebSphere Portal/6.1.0.1 build wp6101_115_01 exported on Tue Mar 31 18:32:25 PDT 2009 from sunpatil-wxp/192.168.2.105 -->
<!-- 1 [content-node 6_000000000000000000000000A0] -->
<!-- 2 [content-node 6_CGAH47L00G72502N5S2MAV00M1 uniquename=wps.Login] -->
<!-- 3 [component 7_CGAH47L00G72502N5S2MAV00M7] -->
<!-- 4 [component 7_CGAH47L00G72502N5S2MAV00E0] -->
<!-- 5 [skin K_CGAH47L00G72502N5S2MAV00C6] -->
<!-- 6 [component 7_CGAH47L00G72502N5S2MAV00E4] -->
<!-- 7 [web-app 1_CGAH47L00G72502N5S2MAV00C3] -->
<!-- 8 [portlet-app 2_CGAH47L00G72502N5S2MAV00C7] -->
<!-- 9 [servlet V_CGAH47L00G72502N5S2MAV00S0] -->
<!-- 10 [portlet 3_CGAH47L00G72502N5S2MAV00S4] -->
<!-- 11 [portletinstance 5_CGAH47L00G72502N5S2MAV00E2] -->
<request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" build="wp6101_115_01" type="update" version="6.1.0.1" xsi:noNamespaceSchemaLocation="PortalConfig_6.1.0.xsd">
<portal action="locate">
<skin action="locate" domain="rel" objectid="K_CGAH47L00G72502N5S2MAV00C6" uniquename="wps.skin.thinSkin"/>
<web-app action="locate" domain="rel" objectid="1_CGAH47L00G72502N5S2MAV00C3" uid="login.war.webmod">
<servlet action="locate" domain="rel" name="Login Portlet" objectid="V_CGAH47L00G72502N5S2MAV00S0"/>
<portlet-app action="locate" domain="rel" name="wp.ap.login" objectid="2_CGAH47L00G72502N5S2MAV00C7" uid="login.war">
<portlet action="locate" domain="rel" name="Login Portlet" objectid="3_CGAH47L00G72502N5S2MAV00S4" uniquename="wps.p.Login"/>
</portlet-app>
</web-app>
<content-node action="locate" domain="rel" objectid="6_000000000000000000000000A0" uniquename="wps.content.root"/>
<content-node action="update" active="true" allportletsallowed="true" content-parentref="6_000000000000000000000000A0" create-type="explicit" domain="rel" objectid="6_CGAH47L00G72502N5S2MAV00M1" ordinal="350" type="page" uniquename="wps.Login">
<supported-markup markup="html" update="set"/>
<localedata locale="ar">
<title>بدء الاتصال</title>
</localedata>
<localedata locale="ca">
<title>Inicia una sessió</title>
</localedata>
<localedata locale="cs">
<title>Přihlásit se</title>
</localedata>
<localedata locale="da">
<title>Logon</title>
</localedata>
<localedata locale="de">
<title>Anmelden</title>
</localedata>
<localedata locale="el">
<title>Σύνδεση</title>
</localedata>
<localedata locale="en">
<title>Login</title>
</localedata>
<localedata locale="es">
<title>Iniciar sesión</title>
</localedata>
<localedata locale="fi">
<title>Kirjaudu sisään</title>
</localedata>
<localedata locale="fr">
<title>Connexion</title>
</localedata>
<localedata locale="hu">
<title>Bejelentkezés</title>
</localedata>
<localedata locale="it">
<title>Collegamento</title>
</localedata>
<localedata locale="iw">
<title>התחברות</title>
</localedata>
<localedata locale="ja">
<title>ログイン</title>
</localedata>
<localedata locale="ko">
<title>로그인</title>
</localedata>
<localedata locale="nl">
<title>Aanmelden</title>
</localedata>
<localedata locale="no">
<title>Logg på</title>
</localedata>
<localedata locale="pl">
<title>Logowanie</title>
</localedata>
<localedata locale="pt">
<title>Iniciar sessão</title>
</localedata>
<localedata locale="pt_BR">
<title>Efetuar Login</title>
</localedata>
<localedata locale="ro">
<title>Logare</title>
</localedata>
<localedata locale="ru">
<title>Вход в систему</title>
</localedata>
<localedata locale="sk">
<title>Prihlásiť</title>
</localedata>
<localedata locale="sl">
<title>Prijava</title>
</localedata>
<localedata locale="sv">
<title>Inloggning</title>
</localedata>
<localedata locale="th">
<title>ลงชื่อเข้าใช้</title>
</localedata>
<localedata locale="tr">
<title>Oturum Aç</title>
</localedata>
<localedata locale="uk">
<title>Вхід до системи</title>
</localedata>
<localedata locale="zh">
<title>登录</title>
</localedata>
<localedata locale="zh_TW">
<title>登入</title>
</localedata>
<localedata locale="hr">
<title>Prijava</title>
</localedata>
<parameter name="RenderMode" type="string" update="set"><![CDATA[force_ssa]]></parameter>
<parameter name="com.ibm.portal.Hidden" type="string" update="set"><![CDATA[true]]></parameter>
<parameter name="com.ibm.portal.PageIcon" type="string" update="set"><![CDATA[icons/page/login.gif]]></parameter>
<parameter name="com.ibm.portal.ThemePolicy" type="string" update="set"><![CDATA[theme/SingleTopNavMinimal]]></parameter>
<access-control externalized="false" owner="undefined" private="false">
<role actionset="User" update="set">
<mapping subjectid="anonymous portal user" subjecttype="user" update="set"/>
<mapping subjectid="all authenticated portal users" subjecttype="user_group" update="set"/>
</role>
</access-control>
<component action="update" active="true" deletable="undefined" domain="rel" modifiable="undefined" objectid="7_CGAH47L00G72502N5S2MAV00M7" ordinal="100" orientation="H" skinref="undefined" type="container" width="undefined">
<component action="update" active="true" deletable="undefined" domain="rel" modifiable="undefined" objectid="7_CGAH47L00G72502N5S2MAV00E0" ordinal="99" orientation="V" skinref="undefined" type="container" width="700">
<component action="update" active="true" deletable="undefined" domain="rel" modifiable="undefined" objectid="7_CGAH47L00G72502N5S2MAV00E4" ordinal="99" skinref="K_CGAH47L00G72502N5S2MAV00C6" type="control" width="undefined">
<portletinstance action="update" domain="rel" objectid="5_CGAH47L00G72502N5S2MAV00E2" portletref="3_CGAH47L00G72502N5S2MAV00S4"/>
</component>
</component>
</component>
</content-node>
</portal>
<status element="all" result="ok"/>
</request>