JavaWeb中驗證碼的實現

Web程序中,驗證碼是常常使用的技術之一。Web程序永遠面臨未知用戶和未知程序的探測。爲了防止惡意腳本的執行,驗證碼技術無疑是首選方案之一。本文將討論如何在JSPServlet中使用驗證碼技術。 web

驗證碼的產生思路很簡單,在Servlet中隨機產生驗證碼字符序列,並計入session中,JSP中以圖片的形式進行顯示。當用戶在JSP表單中輸入驗證碼並提交時,在相應的Servlet中驗證是否與session中保存的驗證碼一致。下面經過代碼,一次演示驗證碼產生和實現的驗證的過程。 瀏覽器



1.驗證碼的產生 緩存



咱們須要建立一個名爲ValcodeServletservlet並在其doGet()方法中完成驗證碼的產生。首先經過隨機數的產生類Random隨機產生一個4位的驗證碼,並將其存入session;而後使用BufferedImageGraphics類把驗證碼轉爲圖片,固然爲了起到較好的效果,咱們須要添加一些干擾線;最後使用ImageIO將圖片輸出。詳細代碼以下: session





 

 

protectedvoiddoGet(HttpServletRequest request, HttpServletResponse
  response)
throwsServletException, IOException { app


 

       // 告知瀏覽看成圖片處理 dom


 

       response.setContentType("image/jpeg"); jsp


 


 

       // 告訴瀏覽器不緩存 post


 

       response.setHeader("pragma", "no-cache"); this


 

       response.setHeader("cache-control", "no-cache"); url


 

       response.setHeader("expires", "0");


 


 

       // 產生由4位數字構成的驗證碼


 

       int length = 4;


 

       String valcode
  =
"";


 

       Random rd =
 
new Random();


 

       for(int i=0;
  i<length; i++)


 

           valcode+=rd.nextInt(10);


 


 

       // 把產生的驗證碼存入到Session


 

       HttpSession
  session = request.getSession();


 

       session.setAttribute("valcode", valcode);


 


 

       // 產生圖片


 

       int width = 80;


 

       int height = 25;


 

       BufferedImageimg
  =
newBufferedImage(width, height,BufferedImage.TYPE_INT_RGB);


 


 

       // 獲取一個Graphics


 

       Graphics g
  = img.getGraphics();


 


 

       // 填充背景色


 

       g.setColor(Color.WHITE);


 

       g.fillRect(0,
  0, width, height);


 


 

       // 填充干擾線50


 

       for(int i=0;
  i<50; i++){


 

           g.setColor(new
  Color(rd.nextInt(100)+155,rd.nextInt(100)+155,rd.nextInt(100)+155));


 

           g.drawLine(rd.nextInt(width),
  rd.nextInt(height),rd.nextInt(width), rd.nextInt(height));


 

       }


 


 

       // 繪製邊框


 

       g.setColor(Color.GRAY);


 

       g.drawRect(0,
  0, width-1, height-1);


 


 

       // 繪製驗證碼


 

       Font[]
  fonts = {
new Font("隸書",Font.BOLD,18),new Font("楷體",Font.BOLD,18),new Font("宋體",Font.BOLD,18),new Font("幼圓",Font.BOLD,18)};


 

       for(int i=0;
  i<length; i++){


 

           g.setColor(new
  Color(rd.nextInt(150),rd.nextInt(150),rd.nextInt(150)));


 

           g.setFont(fonts[rd.nextInt(fonts.length)]);


 

           g.drawString(valcode.charAt(i)+"", width/valcode.length()*i+2, 18);


 

       }


 


 

       // 輸出圖像


 

       g.dispose();


 

       ImageIO.write(img,
 
"jpeg", response.getOutputStream());


 

    }


 

上面的代碼只是產生了一個常規的驗證碼,咱們能夠根據本身的須要對驗證碼的產生策略和干擾線進行調整。Servlet編寫完畢,別忘了在web.xml中進行配置以便能在JSP中調用,其代碼以下:





 

 

<servlet>


 

<description></description>


 

<display-name>ValcodeServlet</display-name>


 

<servlet-name>ValcodeServlet</servlet-name><servlet-class>org.icer.jee.valcode.servlet.ValcodeServlet</servlet-class>


 

</servlet>


 

<servlet-mapping>


 

<servlet-name>ValcodeServlet</servlet-name>


 

<url-pattern>/ValcodeServlet</url-pattern>


 

</servlet-mapping>


 




2.驗證碼的顯示

產生驗證碼的servlet編寫完畢,而且已經web.xml中進行了配置,那麼咱們在input.jsp中使用<img />標記以圖片的方式調用servlet便可顯示驗證碼。


 


固然爲了能起到驗證效果,本例中還包含了簡單的表單。爲了放置驗證碼沒法識別,此處還提供了看不清點擊換一張功能,用戶點擊圖片時從新加載驗證碼圖片(問號是爲了放置瀏覽器緩存而不能實現從新請求圖片)。JSP中表單部分代碼以下:





 

 

<formname="form1"method="post"action="LoginServlet">


 

驗證碼:


 

<inputname="vcode"type="text"class="input02"id="vcode">


 

<imgsrc="ValcodeServlet"align="absmiddle"title="看不清,點擊換一張"onClick="this.src=this.src+'?'"/>


 

<inputtype="submit"name="button"id="button"value="
 
提交 ">


 

</form>


 

3.實現驗證功能

  當表單提交到CheckServlet時,對用戶填寫的驗證碼和session中存儲的驗證碼進行比對,根據結果給出不一樣提示。代碼以下:





 

 

protectedvoiddoPost(HttpServletRequest request, HttpServletResponse
  response)
throwsServletException, IOException {


 

       // 獲取驗證碼


 

       String
  valcode = request.getSession().getAttribute(
"valcode").toString();


 

       // 獲取用戶填寫的驗證碼


 

       String
  vcode = request.getParameter(
"vcode");


 

       // 進行驗證


 

       if(!valcode.equals(vcode))


 

           System.out.println(">>>驗證碼錯誤!");


 

       else


 

           System.out.println(">>>驗證碼正確!");


 

    }


 

上面只是根據驗證狀況在控制檯進行了輸出,使用時根據實際的業務邏輯需求進行修改便可。

    總起來講,驗證碼技術本質上就是利用Java繪圖技術把隨機產生的驗證碼字符圖形化,並在JSP中以圖形調用,最後在用戶提交表單後在對應的servlet中進行驗證。本文只是提供驗證碼的基本實現思路,但願你們能靈活應用。

做者:中軟卓越天津ETC

相關文章
相關標籤/搜索