Java验证码

这里写图片描述

Servlet代码 (JcaptchaServlet.java)
/**
* 提供验证码图片的Servlet
*/
@SuppressWarnings(“serial”)
public class JcaptchaServlet extends HttpServlet {
public static final String CAPTCHA_IMAGE_FORMAT = “jpeg”;

private ImageCaptchaService captchaService;
private SessionProvider session;

@Override
public void init() throws ServletException {
    WebApplicationContext appCtx = WebApplicationContextUtils
            .getWebApplicationContext(getServletContext());
    captchaService = (ImageCaptchaService) BeanFactoryUtils
            .beanOfTypeIncludingAncestors(appCtx, ImageCaptchaService.class);
    session = (SessionProvider) BeanFactoryUtils
            .beanOfTypeIncludingAncestors(appCtx, SessionProvider.class);
}

@Override
protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    byte[] captchaChallengeAsJpeg = null;
    // the output stream to render the captcha image as jpeg into
    ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
    try {
        // get the session id that will identify the generated captcha.
        // the same id must be used to validate the response, the session id
        // is a good candidate!

        String captchaId = session.getSessionId(request);
        BufferedImage challenge = captchaService.getImageChallengeForID(
                captchaId, request.getLocale());
        // Jimi.putImage("image/jpeg", challenge, jpegOutputStream);
        ImageIO.write(challenge, CAPTCHA_IMAGE_FORMAT, jpegOutputStream);
    } catch (IllegalArgumentException e) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    } catch (CaptchaServiceException e) {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return;
    }
    // catch (JimiException e) {
    // response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    // return;
    // }

    captchaChallengeAsJpeg = jpegOutputStream.toByteArray();

    // flush it in the response
    response.setHeader("Cache-Control", "no-store");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Expires", 0);
    response.setContentType("image/" + CAPTCHA_IMAGE_FORMAT);

    ServletOutputStream responseOutputStream = response.getOutputStream();
    responseOutputStream.write(captchaChallengeAsJpeg);
    responseOutputStream.flush();
    responseOutputStream.close();
}

}

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

导入jcaptcha-my-1.0.jar

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个Java验证码登录的示例代码: ```java // 生成验证码 String verifyCode = VerifyCodeUtils.generateVerifyCode(4); // 将验证码存入session request.getSession().setAttribute("verifyCode", verifyCode); // 生成验证码图片 response.setContentType("image/png"); BufferedImage image = VerifyCodeUtils.getImage(verifyCode, 100, 40); ImageIO.write(image, "png", response.getOutputStream()); ``` 上述代码中,`VerifyCodeUtils`是一个自定义的工具类,用于生成验证码验证码图片。在生成验证码时,我们将验证码存入session中,以便后续验证。在生成验证码图片时,我们设置了图片的类型为PNG,并将图片输出到response的输出流中,以便前端页面显示。 接下来是一个验证码登录的示例代码: ```java // 获取用户输入的验证码 String userInputVerifyCode = request.getParameter("verifyCode");// 获取session中存储的验证码 String sessionVerifyCode = (String) request.getSession().getAttribute("verifyCode"); // 验证码校验 if (userInputVerifyCode == null || !userInputVerifyCode.equalsIgnoreCase(sessionVerifyCode)) { // 验证码错误 return "verifyCodeError"; } else { // 验证码正确,进行登录操作 // ... } ``` 上述代码中,我们首先获取用户输入的验证码和session中存储的验证码,然后进行验证码校验。如果验证码校验失败,我们返回一个错误页面;如果验证码校验成功,我们进行登录操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bore1994

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值