一、生成驗證碼圖片
在Web應用程序中,為了防止機器人程序的攻擊,我們經常使用驗證碼技術。驗證碼就是一張帶有隨機數、字母或圖形的圖片,用戶需要輸入正確的驗證碼才能通過驗證。下面我們就來看一下如何用JAVA生成驗證碼圖片。
1.1 寫一個驗證碼工具類
首先,我們需要編寫一個驗證碼工具類,這個類主要是生成驗證碼圖片並輸出到頁面上。
public class VerifyCodeUtil {
public static void outputImage(int width, int height, OutputStream os, String code) throws IOException {
int verifySize = code.length();
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Random rand = new Random();
Graphics2D g2 = image.createGraphics();
g2.setColor(Color.WHITE);
g2.fillRect(0,0,width,height);
Font font = new Font("宋體",Font.BOLD,18);
g2.setFont(font);
for(int i=0;i<200;i++) {
int x = rand.nextInt(width);
int y = rand.nextInt(height);
int red = rand.nextInt(255);
int green = rand.nextInt(255);
int blue = rand.nextInt(255);
g2.setColor(new Color(red,green,blue));
g2.drawLine(x,y,x+1,y+1);
}
g2.setColor(Color.BLACK);
int size = width / verifySize;
for(int i = 0;i<verifySize;i++) {
String s = code.substring(i,i+1);
int x = rand.nextInt(3);
int y = rand.nextInt(5);
g2.drawString(s,(i+1)*x ,y + 15);
}
g2.dispose();
ImageIO.write(image,"JPEG",os);
}
}
1.2 調用工具類生成驗證碼圖片
有了工具類,我們就可以在項目中調用這個類生成驗證碼圖片。以下是一個簡單的示例代碼:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpeg");
String verifyCode = VerifyCodeUtil.generateVerifyCode(4);
HttpSession session=request.getSession();
session.setAttribute("verify_code", verifyCode);
int w = 100, h = 30;
VerifyCodeUtil.outputImage(w, h, response.getOutputStream(), verifyCode);
}
其中generateVerifyCode()函數是用來生成隨機驗證碼字符串,我們可以根據需要設置驗證碼長度。以上代碼將生成的驗證碼保存在session中,以便在提交表單時進行校驗。
二、生成驗證碼BLOB數據
我們還可以生成驗證碼的BLOB數據,將其保存到數據庫中。以下是與生成驗證碼BLOB數據相關的內容。
2.1 將生成的驗證碼圖片轉化為BLOB數據
public byte[] getVerifyCodeData(int width, int height, String verifyCode) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
VerifyCodeUtil.outputImage(width, height, baos, verifyCode);
} catch (IOException e) {
e.printStackTrace();
}
return baos.toByteArray();
}
2.2 將驗證碼BLOB數據寫入數據庫
public void saveVerifyCode(String code) throws SQLException {
byte[] verifyCodeData = getVerifyCodeData(80,30,code);
Connection conn = null;
PreparedStatement ps = null;
String sql = "insert into verify_code (code_data) values(?)";
try{
conn = getConnection();
ps = conn.prepareStatement(sql);
ps.setBytes(1,verifyCodeData);
ps.executeUpdate();
}finally {
closeConnection(conn, ps, null);
}
}
三、驗證碼校驗
在使用驗證碼的時候,我們需要對驗證碼進行校驗。以下是驗證碼校驗相關的內容。
3.1 比較驗證碼字符串
在提交表單時,我們需要獲取用戶輸入的驗證碼,然後與生成的驗證碼字符串進行比較,判斷是否正確。
public boolean verifyCode(HttpSession session, String input) {
String code = (String)session.getAttribute("verify_code");
if (StringUtils.isNotBlank(code) && StringUtils.isNotBlank(input) && input.toLowerCase().equals(code.toLowerCase())) {
return true;
}
return false;
}
3.2 比較驗證碼BLOB數據
我們還可以將生成的驗證碼BLOB數據保存到數據庫中,在進行校驗時直接比較兩個BLOB數據是否一致。
public boolean verifyCode(String input) throws SQLException{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select code_data from verify_code where id = (select max(id) from verify_code)";
byte[] verifyCodeData = null;
try{
conn = getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
if(rs.next()){
verifyCodeData = rs.getBytes(1);
}
}finally {
closeConnection(conn, ps, rs);
}
byte[] inputCodeData = getVerifyCodeData(80,30,input);
if(Arrays.equals(verifyCodeData, inputCodeData)){
return true;
}
return false;
}
四、小結
本文詳細介紹了JAVA生成驗證碼圖片、生成驗證碼BLOB數據和驗證驗證碼的操作。在開發Web應用程序時,驗證碼是一個不可或缺的部分。本文中給出的代碼示例可以用來生成簡單的驗證碼,讀者可以根據需要進行修改和擴展。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/254440.html