项目简单验证码

小灰灰 2022-06-07 13:47 261阅读 0赞

一:作用
我们使用验证码,是确保系统的使用必须要进行登录成功之后,才能使用,避免用户直接在地址栏

中输入要访问的页面也就是说,使用验证码,就强制用户用户必须先从登录界面登录

二:验证实现方式
用到两个关键类,这两个类跟图片的输出是有关系的

BufferedImage im = new BufferedImage(60,20,BufferedImage.TYPE_INT_RGB);
//第一个参数im表示一个图片对象
//JPG表示图片输出类型
//response.getOutputStream()代表一个响应的输出流,也就是说,你访问这个servlet.该

servlet就会图片显示给你
ImageIO.write(im, “JPG”,response.getOutputStream());

三.实现步骤
1.使用BufferedImage产生一个图片,然后使用ImageIO输出,并指定为JPG格式
BufferedImage im = new BufferedImage(60,20,BufferedImage.TYPE_INT_RGB);
//第一个参数im表示一个图片对象
//JPG表示图片输出类型
//response.getOutputStream()代表一个响应的输出流,也就是说,你访问这个servlet.该servlet就会图片显示给你
ImageIO.write(im, “JPG”,response.getOutputStream());

2.获取图片绘图对象
Graphics g = im.getGraphics();

3.填充绘图区域
Random rm = new Random();
Color c = new Color(rm.nextInt(255),rm.nextInt(255),rm.nextInt(255));
g.setColor(c);
//填充整个图片的颜色
g.fillRect(0, 0, 60, 20);

4.向图片中输出数字
g.setColor(new Color(rm.nextInt(255),rm.nextInt(255),rm.nextInt(255)));
g.setFont(new Font(“华文隶书”,Font.BOLD|Font.ITALIC,28));
g.drawString(“8”, 1, 18);

5.随机4位数字

//随机产生4位数字
for(int i=0;i<4;i++){
g.setColor(new Color(rm.nextInt(255),rm.nextInt(255),rm.nextInt(255)));
g.setFont(new Font(“Gungsuh”,Font.BOLD|Font.ITALIC,22));
g.drawString(“”+rm.nextInt(10), (i*15)+2, 18);
}

6.随机产生中文
String str = “胸有激雷而面如平湖者可拜上将军”;
for(int i=0;i<4;i++){
g.setColor(new Color(rm.nextInt(255),rm.nextInt(255),rm.nextInt(255)));
g.setFont(new Font(“Gungsuh”,Font.BOLD|Font.ITALIC,15));
g.drawString(“”+str.charAt(rm.nextInt(str.length())), (i*15)+2, 18);
}

7.在页面中如何来引入该验证码:
验证码

8.保存数字,以便进行登录比较
//将得到的四个数字保存到session中,以便当用户登录的时候,用来比较
request.getSession().setAttribute(“piccode”, sbf.toString());

9.登录验证
首先,需要验证该用户在数据库中是否存在,如果存在,还需要验证输入的验证码是否一致.
验证成功后,需要转发到相关的操作页面.

代码实例:

  1. /** * @author: John * @related module:登录 * @desc: 验证码图片 */ @Controller
  2. @RequestMapping("/code")
  3. public class SecCodeController {
  4. @RequestMapping
  5. public void generate(HttpServletResponse response) {
  6. ByteArrayOutputStream output = new ByteArrayOutputStream();
  7. String code = drawImg(output);
  8. Subject currentUser = SecurityUtils.getSubject();
  9. Session session = currentUser.getSession();
  10. //将生成的数保存到session
  11. session.setAttribute(Const.SESSION_SECURITY_CODE, code);
  12. try {
  13. ServletOutputStream out = response.getOutputStream();
  14. output.writeTo(out);
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. private String drawImg(ByteArrayOutputStream output) {
  20. String code = "";
  21. for (int i = 0; i < 4; i++) {
  22. code += randomChar();
  23. }
  24. int width = 70;
  25. int height = 25;
  26. //在内存中创建一张图片
  27. BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
  28. Font font = new Font("Times New Roman", Font.PLAIN, 20);
  29. //创建一个图形(画布)
  30. Graphics2D g = bi.createGraphics();
  31. //设置写入数据的字体,颜色
  32. g.setFont(font);
  33. Color color = new Color(66, 2, 82);
  34. g.setColor(color);
  35. g.setBackground(new Color(226, 226, 240));
  36. g.clearRect(0, 0, width, height);
  37. FontRenderContext context = g.getFontRenderContext();
  38. Rectangle2D bounds = font.getStringBounds(code, context);
  39. double x = (width - bounds.getWidth()) / 2;
  40. double y = (height - bounds.getHeight()) / 2;
  41. double ascent = bounds.getY();
  42. double baseY = y - ascent;
  43. //向图片上写数据
  44. g.drawString(code, (int) x, (int) baseY);
  45. g.dispose();
  46. try {
  47. //把写好的数据的图片输出给浏览器
  48. ImageIO.write(bi, "jpg", output);
  49. } catch (IOException e) {
  50. e.printStackTrace();
  51. }
  52. return code;
  53. }
  54. private char randomChar() {
  55. Random r = new Random();
  56. String s = "ABCDEFGHJKLMNPRSTUVWXYZ0123456789";
  57. return s.charAt(r.nextInt(s.length()));
  58. }
  59. }
  60. //基于规则表达式的替换,在用,mbfw,隔开返回数组
  61. //前端var code = "qq123456789mbfw"+loginname+",mbfw,"+password+"QQ123456789mbfw"+",mbfw,"+$("#code").val();
  62. String KEYDATA[] = pd.getString("KEYDATA").replaceAll("qq123456789mbfw", "").replaceAll("QQ123456789mbfw", "").split(",mbfw,");
  63. if (null != KEYDATA && KEYDATA.length == 3) {
  64. // shiro管理的session
  65. Subject currentUser = SecurityUtils.getSubject();
  66. Session session = currentUser.getSession();
  67. String sessionCode = (String) session.getAttribute(Const.SESSION_SECURITY_CODE); // 获取session中的验证码
  68. //获取session中的验证码
  69. String code = KEYDATA[2];
  70. if (null == code || "".equals(code)) {
  71. errInfo = "nullcode"; // 验证码为空
  72. } else {
  73. String USERNAME = KEYDATA[0];
  74. String PASSWORD = KEYDATA[1];
  75. pd.put("USERNAME", USERNAME);
  76. if (Tools.notEmpty(sessionCode) && sessionCode.equalsIgnoreCase(code)) {
  77. String passwd = new SimpleHash("SHA-1", USERNAME, PASSWORD).toString(); // 密码加密
  78. pd.put("PASSWORD", passwd);
  79. pd = userService.getUserByNameAndPwd(pd);
  80. if (pd != null) {
  81. pd.put("LAST_LOGIN", DateUtil.getTime().toString());
  82. userService.updateLastLogin(pd);
  83. User user = new User();
  84. user.setUSER_ID(pd.getString("USER_ID"));
  85. user.setUSERNAME(pd.getString("USERNAME"));
  86. user.setPASSWORD(pd.getString("PASSWORD"));
  87. session.removeAttribute(Const.SESSION_SECURITY_CODE);
  88. // shiro加入身份验证
  89. Subject subject = SecurityUtils.getSubject();
  90. UsernamePasswordToken token = new UsernamePasswordToken(USERNAME, PASSWORD);
  91. try {
  92. subject.login(token);
  93. } catch (AuthenticationException e) {
  94. errInfo = "身份验证失败!";
  95. }
  96. } else {
  97. errInfo = "usererror"; // 用户名或密码有误
  98. }
  99. } else {
  100. errInfo = "codeerror"; // 验证码输入有误
  101. }

发表评论

表情:
评论列表 (有 0 条评论,261人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java制作简单验证验证程序

    > 在平时,我们登录一些网站或者软件时经常会让我们填写验证码,通过输入验证码功能可以防止:恶意破解密码、刷票、论坛灌水,有效防止某个黑客对某一个特定注册用户用特定程序暴力破解方

    相关 项目简单验证

    一:作用 我们使用验证码,是确保系统的使用必须要进行登录成功之后,才能使用,避免用户直接在地址栏 中输入要访问的页面也就是说,使用验证码,