驗證碼 帶干擾線
阿新 • • 發佈:2019-02-14
index
<form action="<s:url namespace="/" action="login"/>">
<input type="text" name="username" placeholder="使用者名稱">
<input type="text" name="password" placeholder="密碼">
<input type="text" name="code" placeholder="驗證碼">
<img id="img" src="<s:url namespace="/" action="image"/>" alt="">
<input type="submit">
</form>
<script src="/js/jquery-3.3.1.min副本%202.js"></script>
<script>
<%--在網頁中, 地址不變 不會發起新的請求--%>
$(function ()
$("#img").click(function () {
$("#img").attr("src", "/image.action?a=" + new Date().getTime());
});
})
</script>
struts
<package name="image" namespace="/" extends="json-default">
<action name="image" class="com.lanou.controller.ImageAction" method="image">
<result name="success" type="stream">
<param name="contentType">
image/jpeg
<!--image/jpg-->
</param>
<param name="inputStream">
inputStream
</param>
<param name="fileName">
fileName
</param>
</result>
</action>
<action name="login" class="com.lanou.controller.ImageAction" method="login">
<result name="success">
/WEB-INF/jsp/show.jsp
</result>
</action>
</package>
image
package com.lanou.util;
import javax.imageio.ImageIO;import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class ImageUtil {
//驗證碼的隨機範圍
private static final char[] CHARS = {'0', '1', '2', '3', '4', '5', 'a', 'b', 'c', 'd','f','k','y','w','B','X'
,'Q','T','L','V','M','6','7'};
//驗證碼的位數
private static final int SIZE = 4;
//圖片寬
private static final int WIDTH = 80;
//圖片高
private static final int HEIGHT = 35;
//字型大小
private static final int FONT_SIZE = 30;
// 干擾線的數量
private static final int LINE_COUNT = 5;
//返回驗證碼和圖片
public static Map<String, BufferedImage> createCode() {
//畫圖片
//1. 建立圖片
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
//2. 獲取圖片的畫筆
Graphics graphics = image.getGraphics();
//3. 畫底色
graphics.setColor(Color.lightGray);
graphics.fillRect(0, 0, WIDTH, HEIGHT);
//4. 畫驗證碼
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < SIZE; i++) {
// 隨機顏色
Color color = randomColor();
graphics.setColor(color);
//隨機文字
int index = random.nextInt(CHARS.length);
char c = CHARS[index];
sb.append(c);
//設定字型
Font fonts[]=new Font[5];
fonts[0] = new Font("Aharoni", Font.PLAIN, 40);
fonts[1] = new Font("Book Antiqua", Font.PLAIN, 30);
fonts[2] = new Font("Calibri", Font.PLAIN, 25);
fonts[3] = new Font("Lucida Console", Font.PLAIN,35);
fonts[4] = new Font("DilleniaUPC", Font.PLAIN, 20);
// fonts [5] = new Font("宋體", Font.BOLD + Font.ITALIC, FONT_SIZE);
// return fonts[random.nextInt(5)];
graphics.setFont(fonts[random.nextInt(5)]);
//繪製
graphics.drawString(c + "", WIDTH / SIZE * i, 30);
}
//5. 畫干擾線
for (int i = 0; i < LINE_COUNT; i++) {
graphics.setColor(randomColor());
int x1 = random.nextInt(WIDTH);
int x2 = random.nextInt(WIDTH);
int y1 = random.nextInt(HEIGHT);
int y2 = random.nextInt(HEIGHT);
graphics.drawLine(x1,x2,y1,y2);
}
Map<String, BufferedImage> map = new HashMap<>();
map.put(sb.toString(), image);
return map;
}
// BufferedImage -> InputStream
public static InputStream imageToStream(BufferedImage image) throws IOException {
//把圖片寫入流中
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(outputStream);
ImageIO.write(image, "jpg", imageOutputStream);
//把輸出流轉成輸入流
byte[] bytes = outputStream.toByteArray();
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
return inputStream;
}
// 隨機顏色
private static Color randomColor(){
Random random = new Random();
int r = random.nextInt(256);
int g = random.nextInt(256);
int b = random.nextInt(256);
Color color = new Color(r,g,b);
return color;
}
}
action
package com.lanou.controller;
import com.lanou.util.ImageUtil;import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
public class ImageAction extends ActionSupport {
private InputStream inputStream;
private String fileName;
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String image() throws IOException {
Map<String, BufferedImage> map = ImageUtil.createCode();
// 獲取驗證碼
String code = map.keySet().iterator().next();
// 存到Session中
HttpSession session = ServletActionContext.getRequest().getSession();
session.setAttribute("code", code);
// 獲取圖片
BufferedImage image = map.get(code);
inputStream = ImageUtil.imageToStream(image);
return SUCCESS;
}
//登入
private String username;
private String password;
private String code;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String login() {
HttpSession session = ServletActionContext.getRequest().getSession();
String rightCode = (String) session.getAttribute("code");
if (code.equals(rightCode)){
System.out.println("驗證碼正確");
if (username.equals("admin") && password.equals("123")){
System.out.println("登入成功");
}else {
System.out.println("登入失敗");
}
}else {
System.out.println("驗證碼錯誤");
}
return SUCCESS;
}
}