Monday, May 9, 2016

Captcha use and validation using liferay API in liferay

In this blog we will see how to use of captcha and validate that captcha in liferay custom portlet.
Liferay provides necessary API as well as tag library to implement CAPTCHA.

In this example we will validate  Captcha  using liferay API.
(CaptchaUtil.check(actionRequest);)

STEPS:

Step1: Create one liferay plugin portlet project, name in this example: CaptchaValidation-portlet
and portlet name: Captcha.

Step 2: Copy below code in view.jsp page,

In below jsp code we have created one  form with captcha field and create one action url for form submission for validating captcha.
Before validating captcha we will load captcha image in that captcha input field so that user can provide captcha value for validation.
For loading captcha we are calling one serve resource url that will load captcha image.

view.jsp

<%@ 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/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>

<portlet:defineObjects />
<liferay-theme:defineObjects />

<!-- Serve resource url for loading captcha -->
<portlet:resourceURL var="loadCaptchaURL"/>

<!-- action url for submitting form for doing captcha validation... -->
<portlet:actionURL  var="validateCaptchaURL" name="validateCaptcha"/>

<liferay-ui:error key="errorMessage" message="Enter correct data as shown in the image"/>

<aui:form action="<%=validateCaptchaURL %>" method="post" name="fm">
   <liferay-ui:captcha url="<%=loadCaptchaURL%>" />
   <aui:button-row>
  <aui:button type="submit"/>
   </aui:button-row>

</aui:form>

Step 3: Paste below code in portlet controller.(java file)

Captcha.java

package com.test;

import com.liferay.portal.kernel.captcha.CaptchaException;
import com.liferay.portal.kernel.captcha.CaptchaUtil;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.util.bridges.mvc.MVCPortlet;

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

/**
 * Portlet implementation class Captcha
 */
public class Captcha extends MVCPortlet {


public void validateCaptcha(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {
      System.out.println("inside validateCaptcha method...");
try {
            CaptchaUtil.check(actionRequest);
            System.out.println("CAPTCHA validated successfully");
            } catch (CaptchaException e) {
            SessionErrors.add(actionRequest, "errorMessage");
         }
}

@Override
public void serveResource(ResourceRequest resourceRequest,
ResourceResponse resourceResponse) throws IOException,
PortletException {
try {
           CaptchaUtil.serveImage(resourceRequest, resourceResponse);
           System.out.println("captcha loaded...");
       } catch (Exception e) {
           System.out.println(e.getMessage());
       }
// TODO Auto-generated method stub
super.serveResource(resourceRequest, resourceResponse);
}

}


Output:
Captcha loaded




 Entered correct captcha value






1 comment:

  1. Wow, en seulement trois étapes, c'est fait... incroyable. J'aime ce poste. Nous proposons également les services Liferay Java et Liferay Angular. Contactez-nous pour plus de détails.

    ReplyDelete