Liferay Captcha Plugin Portlet

Objective:

 Use liferay Captcha in Plugin portlet.

Environment:

Liferay 6.2 +Tomcat 7.x+MySQL 5.1
Note:

The code will work for any portal version.

Download Liferay Captcha  portlet from following location
You can find source and war file


Portlet Screen:




Procedure for deploy portlet:

You can use war file and directly place in your portal deploy folder and test or you can also use source to deploy portlet.

Once portlet is deployed successfully you can see the portlet in sample category name as Liferay Captcha Action

Some time we need captcha to submit forms in liferay portlet development. Liferay already have captcha generated library from which we can generate captcha and verify the captcha.

Liferay have inbuilt tag library to display captcha in JSP page. And liferay captcha tag needs URL as parameter form that it will get captcha image.

Liferay have one of the java class called CaptchaUtilso that we can generate image and we can verify the captcha image.

The following are the two methods which we use in the portlet

serveImagemethod to generate captcha Image

try {
CaptchaUtil.serveImage(resourceRequest, resourceResponse);
}
catch (Exception e) {
_log.error(e);
}

Checkmethod to verify the image

try{
CaptchaUtil.check(actionRequest);
}catch(Exception e){
if (e instanceof CaptchaTextException || e instanceof CaptchaMaxChallengesException ){
SessionErrors.add(actionRequest, e.getClass(), e);
                                   
}else{
System.out.println("Captcha verification success::");
}
                       
}

The following is complete code example

The following is example code in JSP page


<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %>
<liferay-theme:defineObjects />
<portlet:defineObjects />
<%@page import="com.liferay.portal.kernel.captcha.
CaptchaTextException"%>
<%@page import="com.liferay.portal.kernel.captcha.
CaptchaMaxChallengesException"%>
<%@ include file="init.jsp" %>
<liferay-ui:error exception="<%= CaptchaTextException.class %>" message="text-verification-failed" />
<liferay-ui:error exception="<%= CaptchaMaxChallengesException.class %>" message="maximum-number-of-captcha-attempts-exceeded" />
<portlet:actionURL  var="testCaptchaActionURL" name="testCaptcha">
</portlet:actionURL>
<aui:form action="<%= testCaptchaActionURL %>" method="post" name="fm">
<aui:input name="firstName" value=""/>
<aui:input  name="lastName" value=""/>
<portlet:resourceURL var="captchaURL">
</portlet:resourceURL>
<liferay-ui:captcha url="<%=captchaURL%>" />
<aui:button-row>
<aui:button type="submit" />
</aui:button-row>
</aui:form>



The following is example code in Action Class to Generate and Verify the Captcha image



package com.meera.liferay.captch;
import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import com.liferay.portal.kernel.captcha.CaptchaMaxChallengesException;
import com.liferay.portal.kernel.captcha.CaptchaTextException;
import com.liferay.portal.kernel.captcha.CaptchaUtil;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class LiferayCaptchaAction extends MVCPortlet {
      public void testCaptcha(ActionRequest actionRequest, ActionResponse actionResponse)throws IOException, PortletException {
      String firstName=ParamUtil.getString(actionRequest,"firstName");
      String lastName=ParamUtil.getString(actionRequest,"lastName");
      System.out.println("firstName::"+firstName);
      System.out.println("lastName::"+lastName);
      try{
      CaptchaUtil.check(actionRequest);
      }catch(Exception e){
            if (e instanceof CaptchaTextException || e instanceof CaptchaMaxChallengesException ){
                  SessionErrors.add(actionRequest, e.getClass(), e);
                 
            }else{
                  System.out.println("Captcha verification success::");
            }
           
      }
     
      }
     
      @Override
      public void serveResource(ResourceRequest resourceRequest,
                  ResourceResponse resourceResponse)
            throws  IOException, PortletException {

            try {
                  CaptchaUtil.serveImage(resourceRequest, resourceResponse);
            }
            catch (Exception e) {
                  //_log.error(e);
            }
      }

      protected boolean isCheckMethodOnProcessAction() {
            return _CHECK_METHOD_ON_PROCESS_ACTION;
      }
      private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;

}


Author

Meera Prince
Share on Google Plus

About Meera Prince

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment