Monday, May 9, 2016

Captcha use and validation using session in liferay

In this blog we will see how to use of captcha and validate that captcha in liferay custom portlet.

In this example for validation we will get loaded captcha text value from session and matched with user entered captcha entered value.

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="Message" message="Invalid captcha text. Please re-Enter"/>

<aui:form action="<%=validateCaptchaURL %>" method="post" name="fm">  
   <liferay-ui:captcha url="<%=loadCaptchaURL%>">
   </liferay-ui:captcha>
   <aui:button-row>
  <aui:button type="submit" value="Validate Captcha"/>
   </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.CaptchaUtil;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.util.bridges.mvc.MVCPortlet;

import java.io.IOException;
import java.util.Enumeration;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletRequest;
import javax.portlet.PortletSession;
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...");

      String userEnteredCaptcha = ParamUtil.getString(actionRequest, "captchaText");
      System.out.println("userEnteredCaptcha value:::"+userEnteredCaptcha);
      try {
          boolean captchaMatched = checkCaptcha(actionRequest, userEnteredCaptcha);
          if(!captchaMatched){
          SessionErrors.add(actionRequest, "Message");
          }else{
          System.out.println("Captcha Validated Successfully");
          }
         } catch (Exception e) {
  e.printStackTrace();
}
}

/*code for matching userEntered captcha text with loaded captcha text, if matched return true else false*/
private boolean checkCaptcha(PortletRequest request,String enteredCaptchaText) throws Exception {
boolean captchaMatched = false;
PortletSession session = request.getPortletSession();
    String captchaTextFromSession = getCaptchaValueFromSession(session);
   
       if (Validator.isNotNull(captchaTextFromSession)) {
           if(enteredCaptchaText.equals(captchaTextFromSession)){
           captchaMatched = true;
       }
       }
      return captchaMatched;
 }

      /*code for getting loaded captcha text from session*/
 private String getCaptchaValueFromSession(PortletSession session) {
       Enumeration<String> atNames = session.getAttributeNames();
       while (atNames.hasMoreElements()) {
           String name = atNames.nextElement();
           if (name.contains("CAPTCHA_TEXT")) {
               String captchaValueFromSession = (String) session.getAttribute(name);
            return captchaValueFromSession;
           }
       }
       return null;
 }

/*Code inside serve resource method for loading captcha...*/
@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

0 comments:

Post a Comment