Friday, April 23, 2010

JCaptcha in JSF





This is a simple example of implementing JCaptcha in Web Application .The main advantage of using this is that we require only one .jsp file(Cap_Img.jsp) ,there is no need to add any jar files into your project.This examples can be implemented in Three steps.


Step 1:
Developing Captcha (Cap_Img.jsp)
a)Create a jsp file named Cap_Img.jsp

<%@ page import="java.io.*"%>
<%@ page import="java.awt.*" %>
<%@ page import="java.awt.image.*" %>
<%@ page import="javax.imageio.ImageIO" %>
<%@ page import="java.util.*" %>
<% response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-store"); %>

<% int width = 75;
int height = 35;
Random rdm = new Random();
int rl = rdm.nextInt();
String hash1 = Integer.toHexString(rl);
String capstr = hash1.substring(0, 5);
session.setAttribute("key", capstr);
Color background = new Color(204, 204, 204);
Color fbl = new Color(0, 100, 0);
Font fnt = new Font("SansSerif", 1, 17);
BufferedImage cpimg = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
Graphics g = cpimg.createGraphics();
g.setColor(background);
g.fillRect(0, 0, width, height);
g.setColor(fbl);
g.setFont(fnt);
g.drawString(capstr, 10, 25);
g.setColor(background);
g.drawLine(10, 17, 80, 17);
g.drawLine(10, 22, 80, 22);
response.setContentType("image/jpeg");
OutputStream strm = response.getOutputStream();
ImageIO.write(cpimg, "jpeg", strm);
strm.close(); %>


Step 2:
Developing GUI (JCaptcha.jsp)
1)Inside body of jsp type below code

<h:panelGrid id="pn_grd31"columns="4" >
<h:outputText id="lb_JCaptcha" value="Verification Code :"/>
<img src="<%=request.getContextPath()%>/Cap_Img.jsp" style="height:40px;font-weight: bold;" ><br><br>
<h:inputText id="tf_JCaptcha" value="#{captcha.tf_JCaptcha}" autocomplete="off" ></h:inputText>
<input type="button" value="Refresh Image" onClick="window.location.reload()">
</h:panelGrid>


Inside image source give Cap_Img.jsp path.



Step 3:
Checking Captcha value in bean class

HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
String key = (String) session.getAttribute("key");
//update captcha to another one to avoid proxy attack
Random rdm = new Random();
int rl = rdm.nextInt();
String hash1 = Integer.toHexString(rl);
String capstr = hash1.substring(0, 5);
session.setAttribute("key", capstr);
System.out.println("Debug key " + key);
String JCaptcha = getTf_JCaptcha();
System.out.println("Debug getTf_JCaptcha " + getTf_JCaptcha());
if (key.equals(JCaptcha)) {
System.out.println("Debug Entered Captcha value is same");
}else{
System.out.println("Debug Entered Captcha value is Different");
}
Step 4:
Include the below code in every jsp because some times in Linux Systems captcha images are not refreshed
   <%
                        response.setHeader("Cache-Control", "no-cache");
                        response.setHeader("Pragma", "no-cache");
                        response.setDateHeader("Expires", 0);
                        response.setHeader("Cache-Control", "no-store");
            %>


JSF Related topics: Errror handling in JSF, Customizing URL Pattern in JSF,Getting client and server sessionId in JSF,PopUp AlertMessagebox for JSF and more.....