java语言实现登录验证码功能
先上效果。
生成验证码后端逻辑
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/verificationCode")
public class VerifyCodeController {
private static final long serialVersionUID = 1L;
/**
* 这里用作存入session的名称
*/
private static final String SESSION_KEY_OF_RAND_CODE = "randCode";
/**
*
*/
private static final int count = 200;
/**
* 定义图形大小(宽)
*/
private static final int width = 105;
/**
* 定义图形大小(高)
*/
private static final int height = 35;
/**
* 干扰线的长度=1.414*lineWidth
*/
private static final int lineWidth = 1;
@RequestMapping(value= "/generate", method={ RequestMethod.POST,RequestMethod.GET })
public void VerificationCode( HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
// 设置页面不缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// response.setContentType("image/png");
// 在内存中创建图象
final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
final Graphics2D graphics = (Graphics2D) image.getGraphics();
// 设定背景颜色
graphics.setColor(Color.WHITE); // ---1.Color.WHITE为白色
graphics.fillRect(0, 0, width, height);//填充衍射
// 设定边框颜色
//graphics.setColor(getRandColor(100, 200)); // ---2.这是以数字型来设置颜色,颜色模式是指使用三种基色:红、绿、蓝,通过三种颜色的调整得出其它各种颜色,这三种基色的值范围为0~255
graphics.drawRect(0, 0, width - 1, height - 1);
final Random random = new Random();
// 随机产生干扰线,使图象中的认证码不易被其它程序探测到
for (int i = 0; i < count; i++) {
graphics.setColor(getRandColor(150, 200)); // ---3.
final int x = random.nextInt(width - lineWidth - 1) + 1; // 保证画在边框之内
final int y = random.nextInt(height - lineWidth - 1) + 1;
final int xl = random.nextInt(lineWidth);
final int yl = random.nextInt(lineWidth);
graphics.drawLine(x, y, x + xl, y + yl);
}
// 取随机产生的认证码(4位数字)
final String resultCode = exctractRandCode();
for (int i = 0; i < resultCode.length(); i++) {
int num=(int)(Math.random()*4+1);
switch (num) {
case 1:
graphics.setColor(Color.green);
break;
case 2:
graphics.setColor(Color.black);
break;
case 3:
graphics.setColor(Color.blue);
break;
case 4:
graphics.setColor(Color.red);
break;
default:
graphics.setColor(Color.green);
break;
}
graphics.setFont(new Font("Times New Roman", Font.BOLD, 24));
// 设置字符,字符间距,上边距
//System.out.print(resultCode.charAt(i));
graphics.drawString(String.valueOf(resultCode.charAt(i)), (23 * i) + 8, 26);
}
// System.out.println("直接输出:"+resultCode);
// 将认证码存入SESSION
request.getSession().setAttribute(SESSION_KEY_OF_RAND_CODE, resultCode);
// 图象生效
graphics.dispose();
// 输出图象到页面
ImageIO.write(image, "JPEG", response.getOutputStream());
}
/**
* @return 随机码
*/
private String exctractRandCode() {
int randCodeLength = 4;
return RandCodeImageEnum.ALL_CHAR.generateStr(randCodeLength);
}
/**
* 描述:根据给定的数字生成不同的颜色
* @param 这是以数字型来设置颜色,颜色模式是指使用三种基色:红、绿、蓝,通过三种颜色的调整得出其它各种颜色,这三种基色的值范围为0~255
* @param 这是以数字型来设置颜色,颜色模式是指使用三种基色:红、绿、蓝,通过三种颜色的调整得出其它各种颜色,这三种基色的值范围为0~255
* @return 描述:返回颜色
*/
private Color getRandColor(int fc, int bc) { // 取得给定范围随机颜色
final Random random = new Random();
if (fc > 255) {
fc = 255;
}
if (bc > 255) {
bc = 255;
}
final int r = fc + random.nextInt(bc - fc);
final int g = fc + random.nextInt(bc - fc);
final int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
/**
* 验证码辅助类
*/
enum RandCodeImageEnum {
/**
* 混合字符串
*/
ALL_CHAR("0123456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), // 去除小写的l和o这个两个不容易区分的字符;
/**
* 字符
*/
LETTER_CHAR("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),
/**
* 小写字母
*/
LOWER_CHAR("abcdefghijklmnopqrstuvwxyz"),
/**
* 数字
*/
NUMBER_CHAR("0123456789"),
/**
* 大写字符
*/
UPPER_CHAR("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
/**
* 待生成的字符串
*/
private String charStr;
/**
* @param charStr
*/
private RandCodeImageEnum(final String charStr) {
this.charStr = charStr;
}
/**
* 生产随机验证码
*
* @param codeLength
* 验证码的长度
* @return 验证码
*/
public String generateStr(final int codeLength) {
final StringBuffer sb = new StringBuffer();
final Random random = new Random();
final String sourseStr = getCharStr();
for (int i = 0; i < codeLength; i++) {
sb.append(sourseStr.charAt(random.nextInt(sourseStr.length())));
}
return sb.toString();
}
/**
* @return the {@link #charStr}
*/
public String getCharStr() {
return charStr;
}
}
}
前端代码:
function login(){
//window.location.href = "index.jsp";
var code= $("#verifyCode").val();
var name = $("#username").val();
var pas = $("#pas").val();
if(name==""||pas==""){
alert("用户名或密码不能为空!");
return ;
}
if(code==""){
alert("验证码不能为空!");
return ;
}
$.ajax({
type:'post',
url:'../rest/auth/login',
data:{"name":name,"pas":pas,"code":code},
success:function(data){//返回json结果
if(data==1){//登录成功
window.location.href = "index2.jsp";
}
var rad = Math.floor(Math.random() * Math.pow(10, 8));
var path="<%=basePath%>";
if(data==0){
alert("验证码错误!");
$("#randCodeImage").attr("src", path+"xtts/bpm/rest/verificationCode/generate?uuuy="+rad);
$("#VerificationCode").val("").focus(); // 清空并获得焦点
}
if(data==-1){
alert("账号或密码错误!");
$("#randCodeImage").attr("src", path+"xtts/bpm/rest/verificationCode/generate?uuuy="+rad);
$("#username").focus(); // 清空并获得焦点
}
}
});
}
/*更换验证码*/
function changeCode(){
var rad = Math.floor(Math.random() * Math.pow(10, 8));
var path="<%=basePath%>";
//uuuy是随便写的一个参数名称,后端不会做处理,作用是避免浏览器读取缓存的链接
$("#randCodeImage").attr("src", path+"xtts/bpm/rest/verificationCode/generate?uuuy="+rad);
}
登录操作controller层代码。登录前先判断验证码是否输入错误。
HttpSession session = request.getSession();
String sessionCode=session.getAttribute("randCode")==null?"":session.getAttribute("randCode").toString();
if(!sessionCode.equalsIgnoreCase(code)){//验证码错误
return 0;
}
end~
还没有评论,来说两句吧...