验证码

作者: Czw_hacker | 来源:发表于2016-11-30 15:12 被阅读77次

验证码

需要用到的开源项目patchca
https://github.com/pusuo/patchca

产生验证码的Servlet代码

@WebServlet("/patchca.png")
public class PatchcaServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException {
        //通过patchca中的ConfigurableCaptchaService类创建对象
        ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
        //通过对象设置验证码的颜色
        cs.setColorFactory(new SingleColorFactory(new Color(25, 60, 170)));
        cs.setFilterFactory(new CurvesRippleFilterFactory(cs.getColorFactory()));

        //设置验证码的最小长度和最大长度
        RandomWordFactory wordFactory = new RandomWordFactory();
        wordFactory.setMinLength(4);
        wordFactory.setMaxLength(4);
        //设置验证码从这些字符中产生,可以是中文,不设置默认是字母和数字
        wordFactory.setCharacters("0123456789");
        cs.setWordFactory(wordFactory);

        try {
            //响应输出流,通过response获取
            OutputStream outputStream = resp.getOutputStream();
            //获取验证码内容返回为String类型,EncoderHelper来自于org.patchca.utils.encoder.EncoderHelper;
            //第一个参数为ConfigurableCaptchaService对象cs,第二个参数为输出的格式(图片),第三个为输出流
            String patchca = EncoderHelper.getChallangeAndWriteImage(cs, "png", outputStream);
            //把验证码内容放入session中用来验证用户输入验证码
            HttpSession session = req.getSession();
            session.setAttribute("patchca",patchca);

            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

jsp代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <style>
        #img{
            text-decoration: none;
            width: 160px;
            height: 70px;
            display: block;
        }

    </style>
</head>
<body>
<h3>${message}</h3>
<form action="/pay" method="post">
    金额 <input type="text" name = "money" value="${money}">
    <br>
    验证码 <input type="text" name = "code">
    <br>
    <%--把验证码放入a标签中,达到点击刷新验证码的效果,点击的时候执行javascript--%>
    <a href="javascript:;" id="change"><img src="/patchca.png" alt="" id="img"></a>
    <br>
    <button>支付</button>

</form>
<script src="/static/js/jquery-1.11.3.min.js"></script>
<script>

    $(function () {
        $("#change").click(function () {
            /*每点击一次执行一次GET请求获取一次验证码
            * ?后面键值对无实际意义是为了区分每次点击不同
            * 如不加浏览器会认为每次点击的请求是一样的,造成刷新不成功
            * */
            $("#img").removeAttr().attr("src","/patchca.png?_="+new Date().getTime())
        });
    });
</script>
</body>
</html>

验证用户输入验证码是否正确的Servlet代码

@WebServlet("/pay")
public class PayServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String code = req.getParameter("code");
        String money = req.getParameter("money");

        //获取session中存放的验证码的值
        HttpSession session = req.getSession();
        String sessionStr = (String) session.getAttribute("patchca");

        if(sessionStr != null && sessionStr.equals(code) ){
            System.out.println("转账"+money+"成功");
            //验证通过删除session中的验证码,确保验证码只一次有效
            session.removeAttribute("patchca");
        }else{
            req.setAttribute("message","验证码错误");
            req.setAttribute("money",money);
            req.getRequestDispatcher("/patchca.jsp").forward(req,resp);
        }
    }
}

相关文章

网友评论

    本文标题:验证码

    本文链接:https://www.haomeiwen.com/subject/twrtmttx.html