<div class="tip verifyCode-box">
<input class="verifyCode" name="verifyCode" type="text" id="verifyCode" maxlength="4" title="验证码" value="" nullmsg="请输入验证码!" placeholder="请输入验证码" />
<img id="code" alt="verifyCode" src="/ntkoysb/loginAction.do?getVerifyCode" >
</div>
window.onload = function () {
var img = document.getElementById("code");
img.onclick = function() {
var date = new Date().getTime();
img.src = "/a/login.do?getCode&time=" + date;
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.http.HttpSession;
public class VerifyCodeUtil {
public static final String SESSION_KEY = "verifyCode";
public static final String BUFFIMG_KEY = "buffImg";
private static int width = 100;
public static final long VERIFYCODE_TIMEOUT = 60*1000;
private static int height = 30;
private static int codeCount = 4;
private static int fontHeight;
private static int interLine = 12;
private static int codeX;
private static int codeY;
static char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
public static Map<String, Object> getVerifyCode(){
Map<String, Object> result = new HashMap<>();
codeX = (width-4) / (codeCount+1);
fontHeight = height - 10;
codeY = height - 7;
BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D gd = buffImg.createGraphics();
Random random = new Random();
gd.setColor(Color.WHITE);
gd.fillRect(0, 0, width, height);
Font font = new Font("Times New Roman", Font.PLAIN, fontHeight);
gd.setFont(font);
gd.setColor(Color.BLACK);
gd.drawRect(0, 0, width - 1, height - 1);
gd.setColor(Color.gray);
for (int i = 0; i < interLine; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
gd.drawLine(x, y, x + xl, y + yl);
}
StringBuffer randomCode = new StringBuffer();
int red = 0, green = 0, blue = 0;
for (int i = 0; i < codeCount; i++) {
String strRand = String.valueOf(codeSequence[random.nextInt(36)]);
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);
gd.setColor(new Color(red,green,blue));
gd.drawString(strRand, (i + 1) * codeX, codeY);
randomCode.append(strRand);
}
result.put(BUFFIMG_KEY, buffImg);
result.put(SESSION_KEY, randomCode.toString());
return result;
}
public static void removeAttrbute(final HttpSession session) {
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
session.removeAttribute(SESSION_KEY);
timer.cancel();
}
}, VERIFYCODE_TIMEOUT);
}
}
@RequestMapping(params = "getVerifyCode")
public void getVerifyCode(HttpServletRequest request, HttpServletResponse response){
Map<String, Object> map = VerifyCodeUtil.getVerifyCode();
HttpSession session = request.getSession();
session.setAttribute(VerifyCodeUtil.SESSION_KEY, map.get(VerifyCodeUtil.SESSION_KEY));
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
try {
ServletOutputStream sos = response.getOutputStream();
ImageIO.write((RenderedImage) map.get(VerifyCodeUtil.BUFFIMG_KEY), "jpeg", sos);
sos.close();
VerifyCodeUtil.removeAttrbute(session);
} catch (IOException e) {
e.printStackTrace();
}
}
@RequestMapping(params = "getVerifyCodeContent")
public String getVerifyCodeContent(String inputVerifyCode){
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
HttpSession session = request.getSession();
String va = "";
String verifyCode = (String)session.getAttribute(VerifyCodeUtil.SESSION_KEY);
if(null == verifyCode || verifyCode.isEmpty()){
System.out.println("验证码过期请重新验证");
va = "3";
return va;
}
verifyCode = verifyCode.toLowerCase();
inputVerifyCode = inputVerifyCode.toLowerCase();
System.out.println(verifyCode+":"+inputVerifyCode);
if (verifyCode.equals(inputVerifyCode)) {
va = "1";
}else {
va = "2";
}
return va;
}