SSM实现登录验证码功能(附源码)如果想⽤框架来实现验证码,可以看我的另外⼀篇:
Kaptcha 框架的使⽤很简单的。新⼿必备。
⼀、效果图
⼆、实现代码
第⼀步:先建⼀个 RandomValidateCode.java 作为⼯具类。把下⾯的代码直接复制粘贴即可。package com.utils;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class RandomValidateCode {
public static final String RANDOMCODEKEY = "randomcode_key";//放到session中的key
private Random random = new Random();
private String randString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";//随机产⽣的字符串
private int width = 80;//图⽚宽
private int height = 26;//图⽚⾼
private int lineSize = 40;//⼲扰线数量
private int stringNum = 4;//随机产⽣字符数量
/**
* ⽣成随机图⽚
*/
public void getRandcode(HttpServletRequest request,
HttpServletResponse response) {
HttpSession session = Session();
//BufferedImage类是具有缓冲区的Image类,Image类是⽤于描述图像信息的类
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
//产⽣Image对象的Graphics对象,改对象可以在图像上进⾏各种绘制操作
Graphics g = Graphics();
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman",Font.ROMAN_BASELINE,18));
g.setColor(getRandColor(160, 200));
//绘制⼲扰线
for(int i=0;i<=lineSize;i++){
drowLine(g);
}
//绘制随机字符
String randomString = "";
for(int i=1;i<=stringNum;i++){
randomString=drowString(g,randomString,i);
}
session.setAttribute(RANDOMCODEKEY, randomString);
g.dispose();
try {
//将内存中的图⽚通过流动形式输出到客户端
ImageIO.write(image, "JPEG", OutputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 获得字体
*/
private Font getFont(){
return new Font("Fixedsys",Font.CENTER_BASELINE,18);
}
/*
* 获得颜⾊
*/
private Color getRandColor(int fc,int bc){
if(fc > 255)
fc = 255;
if(bc > 255)
bc = 255;
int r = fc + Int(bc-fc-16);
int g = fc + Int(bc-fc-14);
int b = fc + Int(bc-fc-18);
return new Color(r,g,b);
}
/*
* 绘制字符串
*/
private String drowString(Graphics g,String randomString,int i){
g.setFont(getFont());
g.setColor(new Int(101),Int(111),Int(121)));  String rand = String.valueOf(Int(randString.length())));  randomString +=rand;
g.drawString(rand, 13*i, 16);
return randomString;
}
/*
* 绘制⼲扰线
*/
private void drowLine(Graphics g){
int x = Int(width);
int y = Int(height);
ssm框架实现登录功能int xl = Int(13);
int yl = Int(15);
g.drawLine(x, y, x+xl, y+yl);
}
/*
* 获取随机的字符
*/
public String getRandomString(int num){
return String.valueOf(randString.charAt(num));
}
}
第⼆步:在 Controller 类写前端 UI 的接⼝
/**
* 获取⽣成验证码显⽰到 UI 界⾯
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
@RequestMapping(value="/checkCode")
public void checkCode(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
//设置相应类型,告诉浏览器输出的内容为图⽚
response.setContentType("image/jpeg");
//设置响应头信息,告诉浏览器不要缓存此内容
response.setHeader("pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expire", 0);
RandomValidateCode randomValidateCode = new RandomValidateCode();
try {
} catch (Exception e) {
e.printStackTrace();
}
}
第三步:jsp 页⾯获取验证码
<img src="checkCode" alt="" width="100" height="32" class="passcode" onclick="this.src=this.src+'?'">
第四步:我的是在 SSM 环境下实现的,如果不是的话可以⾃⼰改写下。
总结
以上所述是⼩编给⼤家介绍的SSM 实现登录验证码功能,希望对⼤家有所帮助,如果⼤家有任何疑问请给我留⾔,⼩编会及时回复⼤家的。在此也⾮常感谢⼤家对⽹站的⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。