Java Web實(shí)現(xiàn)登錄頁面驗(yàn)證碼驗(yàn)證功能
一、驗(yàn)證碼
驗(yàn)證碼本質(zhì)上是一張圖片,圖片內(nèi)容會隨著程序的運(yùn)行而隨機(jī)生成
驗(yàn)證碼的作用:防止應(yīng)用惡意發(fā)送數(shù)據(jù),一定程度上避免了惡意程序?qū)W(wǎng)站的攻擊。
驗(yàn)證碼本質(zhì)上是一張圖片,圖片內(nèi)容的準(zhǔn)確解析不容易用程序來實(shí)現(xiàn)。
驗(yàn)證碼的繪制:繪制驗(yàn)證碼圖片不僅僅需要隨機(jī)生成要繪制的內(nèi)容,同時(shí)要配合Java中與繪圖有關(guān)的一套API來完成。
二、效果演示
驗(yàn)證碼Demo
三、給出完整代碼
(1)服務(wù)器端代碼ActionServlet.java
package session;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 服務(wù)器端合并Servlet
*
* @author QianliangGuo
*/
public class ActionServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
// 設(shè)置編碼
request.setCharacterEncoding("utf-8");
// 獲得session
HttpSession session = request.getSession();
//設(shè)置session超時(shí)時(shí)間為10秒
// session.setMaxInactiveInterval(10);
// 獲得請求路徑
String uri = request.getRequestURI();
// 拆分路徑,只保留login.do中的login
String action = uri.substring(uri.lastIndexOf("/") + 1,uri.lastIndexOf("."));
// 判斷請求路徑是否為登錄
if (action.equals("login")) {
String uname = request.getParameter("uname");
String pwd = request.getParameter("pwd");
//獲得用戶提交的驗(yàn)證碼字符
String vcode = request.getParameter("vcode");
//獲得session中存儲的最新驗(yàn)證碼字符
String code = session.getAttribute("code").toString();
if (code.equals(vcode) &&uname.equals("123") && pwd.equals("123") ) {
// 將登錄的用戶綁定到session
session.setAttribute("uname", uname);
// 重定向到index.jsp
// response.sendRedirect("index.jsp");
//如果禁用了Cookie,使用URL重寫
response.sendRedirect(response.encodeRedirectURL("index.jsp"));
} else {
// 登錄失敗,就轉(zhuǎn)發(fā)到login.jsp
request.setAttribute("msg", "輸入有誤,請重新登錄!");
request.getRequestDispatcher("login.jsp").forward(request,response);
}
}else if(action.equals("logout")){
//使session失效
session.invalidate();
response.sendRedirect("login.jsp");
}
}
}
(2)繪制驗(yàn)證碼CodeServlet.java
package session;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 繪制驗(yàn)證碼
*
* @author QianliangGuo
*/
public class CodeServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
//驗(yàn)證碼的servlet
//0.創(chuàng)建空白圖片
BufferedImage image = new BufferedImage(100,30,BufferedImage.TYPE_INT_RGB);
//1.獲取圖片畫筆
Graphics g = image.getGraphics();
Random r = new Random();
//2.設(shè)置畫筆顏色
g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
//3.繪制矩形的背景
g.fillRect(0, 0, 100, 30);
//4.調(diào)用自定義的方法,獲取長度為5的字母數(shù)字組合的字符串
String number = getNumber(5);
//獲得session
HttpSession session = request.getSession();
//設(shè)置sesssion失效時(shí)間為30秒
// session.setMaxInactiveInterval(30);
//將這5個(gè)隨機(jī)字符綁定到session中
session.setAttribute("code", number);
g.setColor(new Color(0,0,0));
g.setFont(new Font(null,Font.BOLD,24));
//5.設(shè)置顏色字體后,繪制字符串
g.drawString(number, 5, 25);
//6.繪制8條干擾線
for(int i=0;i<8;i++){
g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255),r.nextInt(255)));
g.drawLine(r.nextInt(100), r.nextInt(30), r.nextInt(100), r.nextInt(30));
}
response.setContentType("img/jpeg");
OutputStream ops = response.getOutputStream();
ImageIO.write(image,"jpeg",ops);
ops.close();
}
private String getNumber(int size) {
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
String number = "";
Random r = new Random();
for(int i=0;i<size;i++){
number+=str.charAt(r.nextInt(str.length()));
}
return number;
}
}
(2)登錄頁面login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!-- 登錄顯示的頁面 -->
<%
Object msg = request.getAttribute("msg");
if(msg!=null){
%>
<%=msg.toString() %>
<%} %>
<html>
<head>
</head>
<body>
<form action="login.do" method="post">
用戶名:<input name="uname"/></br>
密碼:<input name = "pwd" type="password"/> </br>
驗(yàn)證碼:<input name="vcode"/>
<img src="code" onclick="this.src='code?'+Math.random();"
class="s1" title="點(diǎn)擊更換"/><br/>
<input type="submit" value="登錄"/>
</form>
</body>
</html>
(3)展示驗(yàn)證碼的頁面validateCode.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!-- 展示驗(yàn)證碼的頁面 -->
<html>
<head>
<title>驗(yàn)證碼</title>
<!-- 鼠標(biāo)移入圖片,變成手狀 -->
<style type="text/css">
.s1{
cursor:pointer;
}
</style>
</head>
<body>
<!-- 單擊時(shí),重新向code發(fā)送請求,并添加隨機(jī)數(shù),欺騙瀏覽器為不同的地址 -->
<img src="code" onclick="this.src='code?'+Math.random();"
class="s1" title="點(diǎn)擊更換"/>
</body>
</html>
(5)index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!-- 登錄成功后的頁面 -->
<%
//小腳本:session驗(yàn)證
Object uname = session.getAttribute("uname");
if(uname == null){
//重定向到login.jsp
response.sendRedirect("login.jsp");
return;
}
%>
<html>
<head>
</head>
<body>
<h1>歡迎登錄:<%=uname.toString() %></h1>
<a href="logout.do" rel="external nofollow" >退出</a>
</body>
</html>
總結(jié)
以上所述是小編給大家介紹的Java Web實(shí)現(xiàn)登錄頁面驗(yàn)證碼驗(yàn)證功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
自定義application.yml配置項(xiàng)方式
這篇文章主要介紹了自定義application.yml配置項(xiàng)方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
在SpringBoot中使用MongoDB完成數(shù)據(jù)存儲
本文主要介紹了在SpringBoot中如惡化使用MongoDB完成數(shù)據(jù)存儲,接下來這篇我們將圍繞MongoDB進(jìn)行,MongoDB是一個(gè)開源的,面向文檔的NoSQL數(shù)據(jù)庫管理系統(tǒng),使用類似JSON的BSON(二進(jìn)制JSON)格式來存儲數(shù)據(jù),具有靈活的數(shù)據(jù)模型和強(qiáng)大的查詢功能,需要的朋友可以參考下2023-11-11
JAVA?biginteger類bigdecimal類的使用示例學(xué)習(xí)
這篇文章主要為大家介紹了JAVA?biginteger類bigdecimal類的使用示例學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
ElasticSearch創(chuàng)建后索引修改數(shù)據(jù)類型方法步驟
Elasticsearch存儲數(shù)據(jù)之前需要先創(chuàng)建索引,類似于結(jié)構(gòu)型數(shù)據(jù)庫建庫建表,創(chuàng)建索引時(shí)定義了每個(gè)字段的索引方式和數(shù)據(jù)類型,這篇文章主要給大家介紹了關(guān)于ElasticSearch創(chuàng)建后索引修改數(shù)據(jù)類型的方法步驟,需要的朋友可以參考下2023-09-09
mybatis 批量將list數(shù)據(jù)插入到數(shù)據(jù)庫的實(shí)現(xiàn)
這篇文章主要介紹了mybatis 批量將list數(shù)據(jù)插入到數(shù)據(jù)庫的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07

