图形验证码接口及其重构思想

开发生成图形验证码接口

图片实体 ImageCode

在这里插入图片描述

public class ImageCode  {
   
    /**
     * 一个图片验证码包含三个信息
     */

    private BufferedImage image; //图片

    private String code;//code是一个随机数,图片根据这个随机数生成,这个随机数是要存入到session中的

    private LocalDateTime expireTime;//验证码图片过期时间

    /**
     *
     * @param image
     * @param code
     * @param expireIn 多少秒过期
     */
    public ImageCode(BufferedImage image, String code, int expireIn) {
   
        this.image = image;
        this.code = code;
        this.expireTime = LocalDateTime.now().plusSeconds(expireIn);
    }

    public ImageCode(BufferedImage image, String code, LocalDateTime expireTime) {
   
        this.image = image;
        this.code = code;
        this.expireTime = expireTime;
    }
    public boolean isExpried() {
   
        return LocalDateTime.now().isAfter(expireTime);
    }


图片接口 ValidateCodeController

根据随机数生成图片
将随机数存到session中
将生成的图片写到接口的响应中
在这里插入图片描述

@RestController
public class ValidateCodeController implements Serializable {
   

    private static  final  String SESSION_KEY ="SESSION_KEY_IMAGE_CODE";//key

    //spring 操作session的工具类
    private SessionStrategy sessionStrategy = new HttpSessionSessionStrategy();

    @RequestMapping("/code/image")
    private void createCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
   
        //1根据请求中的随机数生成图片
        ImageCode imageCode = createImageCode(request);
        //2将随机数放到session中
        sessionStrategy.setAttribute(new ServletWebRequest(request),SESSION_KEY,imageCode);
        //3将生成的图片写到接口的响应中
        ImageIO.write(imageCode.getImage(),"jpeg",response.getOutputStream());
    }

    private ImageCode createImageCode(HttpServletRequest request) {
   
        //生成一个图片对象
        int width = 67;
        int height =23;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        Graphics g = image.getGraphics();

        //生成干扰条纹
        Random random = new Random();
        g.setColor(getRandColor(200, 250));
        g.fillRect(0, 0, width, height);
        g.setFont(new Font("Times New Roman", Font.ITALIC, 20));
        g.setColor(getRandColor(160, 200));
        for (int i = 0; i < 155; i++) {
   
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int xl = random.nextInt(12);
            int yl = random.nextInt(12);
            g.drawLine(x, y, x + xl, y + yl);
        }

        //生成四位随机数 写入图片
        String sRand = "";
        for (int i = 0; i <4; i++) {
   
            String rand = String.valueOf(random.nextInt(10));
            sRand += rand;
            g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
            g.drawString(rand, 13 * i + 6, 16);
        }

        g.dispose();

        return new ImageCode(image, sRand, 60);



    }

在认证流程中加入图形验证码校验

登录页面

在这里插入图片描述

  <table>
        <tr>
            <td>用户名:</td>
            <td><input type="text" name="username"></td>
        </tr>
        <tr>
            <td>密码:</td>
            
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值