Friday, 20 November 2015

Captcha to be shown in case of 3 times password failed

Captcha should  be show if password faild 3 times password failed

 This code we need to write inside public class UserLoginActionValidate extends BaseStrutsPortletAction in hook.

String enteredCaptchaText = ParamUtil.getString(actionRequest,"captchaText");
HttpSession httpSession = PortalUtil.getHttpServletRequest(actionRequest).getSession();
int loginAttemptsCount = 0;
try{
String count = (String) httpSession.getAttribute("LIFERAY_SHARED_count");
System.out.println("helloooo=---"+count);
System.out.println("login count-------"+count);
loginAttemptsCount = Integer.parseInt(count);
}
catch(Exception e){

}
System.out.println("login count outside loop-------"+loginAttemptsCount);
try {
PortletSession session = actionRequest.getPortletSession();
session.setAttribute("captcha",0, PortletSession.PORTLET_SCOPE);

if(loginAttemptsCount>3)
{
String captchaText = getCaptchaValueFromSession(session);
if (Validator.isNull(captchaText)) {
// throw new
// Exception("Internal Error! Captcha text not found in session");
SessionErrors.add(actionRequest, "invalid-captcha");
return;
}
if (!captchaText.equals(enteredCaptchaText)) {
System.out.println("IS eroorrrrrr");
// throw new Exception("Invalid captcha text. Please reenter.");
SessionErrors.add(actionRequest, "text-verification-failed");
return;

}}


}/*}*/ catch (NoSuchUserException e) {
SessionErrors.add(actionRequest, "invalid-authentication");
loginAttemptsCount = loginAttemptsCount+1;
System.out.println("loginAttemptsCount"+loginAttemptsCount);
httpSession.setAttribute("LIFERAY_SHARED_count",String.valueOf(loginAttemptsCount));
return;
} catch (CaptchaException e) {
loginAttemptsCount = loginAttemptsCount+1;
System.out.println("loginAttemptsCount"+loginAttemptsCount);
httpSession.setAttribute("LIFERAY_SHARED_count",String.valueOf(loginAttemptsCount));
SessionErrors.add(actionRequest, "invalid-authentication");
return;
}


originalStrutsPortletAction.processAction(originalStrutsPortletAction,
portletConfig, actionRequest, actionResponse);
}


Step2-some code we need to write in login.jsp

<%
int loginfailedcount = 0;
try{
HttpSession httpSession = request.getSession(false);
String count = (String) httpSession.getAttribute("LIFERAY_SHARED_count");
System.out.println("helloooo=---"+count);
System.out.println("login count-------"+count);
loginfailedcount = Integer.parseInt(count);
}
catch(Exception e){}
%>


  <%
  System.out.println("themeDisplay.getUser().getFailedLoginAttempts()-----"+themeDisplay.getUser().getFailedLoginAttempts());
               if(loginfailedcount > 3){ %>
<portlet:resourceURL var="captchaURL">
<portlet:param name="struts_action" value="/login/captcha"/>
</portlet:resourceURL>

<liferay-ui:captcha url="<%= captchaURL %>" />
<%} %>

No comments:

Post a Comment