ssm 验证码生成

来源:互联网 发布:二十八星宿正确算法 编辑:程序博客网 时间:2024/06/07 08:02
1.在工具包下编写ImageCode.java
public class ImageCode {private static Random random = new Random();public static Map<String, BufferedImage> getImage(int width, int height,int n, int charSize) {BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);Graphics2D g = image.createGraphics();g.setColor(new Color(174, 221, 129));g.fillRect(0, 0, width, height);g.setColor(getColor());// 画干扰线for (int i = 0; i < 10; i++) {g.drawLine(random.nextInt(width), random.nextInt(height), random.nextInt(width) + 5, random.nextInt(height) + 5);}/** * 获取验证字符 */String[] code = getCheckString(n);StringBuilder builder = new StringBuilder();/** * 将字符画到图片上去 */g.setFont(new Font(null, Font.BOLD + Font.ITALIC, charSize));for (int i = 0; i < code.length; i++) {g.setColor(getColor());g.drawString(code[i], i * width / n, height*2/3);builder.append(code[i]);}Map<String, BufferedImage> img = new HashMap<String, BufferedImage>();img.put(builder.toString(), image);return img;}private static String[] getCheckString(int n) {char[] chs1 = { '2', '3', '4', '5', '6', '7', '8', '9' };char[] chs = Arrays.copyOf(chs1, chs1.length + 26);String[] chs2 = new String[n];for (int i = 0; i < 26; i++) {chs[chs1.length + i] = (char) ('A' + i);}int i = 0;while (true) {if (i < n) {chs2[i] = String.valueOf(chs[random.nextInt(chs.length)]);i++;} else {break;}}return chs2;}private static Color getColor() {return new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));}

2.在Controller下写Image.java

@RequestMapping("/check_code")public void validPic(HttpServletResponse response) throws Exception{try {Map<String,BufferedImage> map = ImageCode.getImage(80, 42, 4,20);String checkCode = map.keySet().iterator().next();getSession().setAttribute("imageCode", checkCode);BufferedImage image = map.values().iterator().next();// 禁止图像缓存。  response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0);   response.setContentType("image/jpeg");                     ImageIO.write(image, "jpeg", 
response.getOutputStream()
); } catch (Exception e) {log.error("check code error:", e);throw e;}}

3.页面调用

 <form method="post" class="am-form" action="login">        <div class="am-form-group ">      <div class="field am-input-group am-input-group-primary">      <input type="text" name="em" id="em" value="" placeholder="账号/邮箱" data-validate="required:账号不能为空">      <span class="am-input-group-label"><i class="am-icon-user am-icon-fw"></i></span>      </div>      </div>            <div class="am-form-group ">      <div class="field am-input-group am-input-group-primary">      <input type="password" name="pd" id="pd" value="" placeholder="登陆密码" data-validate="required:密码不能为空">      <span class="am-input-group-label"><i class="am-icon-lock am-icon-fw"></i></span>      </div>      </div>            <div class="am-form-group">      <div class="field am-input-group am-input-group-primary">      <input type="text" name="cod" id="cod" value="" placeholder="验证码" data-validate="required:验证码不能为空">      <span class="am-input-group-btn"><img id="captchaImage"  class="captchaImage" src="check_code" /></span>      </div>      </div>      <br />      <div class="am-cf">        <input type="submit"  value="登 录" class="am-btn am-btn-primary am-btn-sm am-fl" ><%@include file="/WEB-INF/jsp/public/tooltip.jsp" %>        <a href="findPwd01" class="am-fr">忘记密码 ^_^?</a>      </div>    </form>








原创粉丝点击