驗(yàn)證碼

maven

  <dependency>
      <groupId>com.bladejava</groupId>
      <artifactId>blade-patchca</artifactId>
      <version>1.0.3</version>
    </dependency>

controller

package com.kaishengit.web;

import org.patchca.color.SingleColorFactory;
import org.patchca.filter.predefined.CurvesRippleFilterFactory;
import org.patchca.font.FontFactory;
import org.patchca.service.ConfigurableCaptchaService;
import org.patchca.utils.encoder.EncoderHelper;
import org.patchca.word.RandomWordFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.io.IOException;
import java.io.OutputStream;

@WebServlet("/patchca.png")
public class PatchcaServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
        cs.setColorFactory(new SingleColorFactory(new Color(11, 32, 170)));
        cs.setFilterFactory(new CurvesRippleFilterFactory(cs.getColorFactory()));


        RandomWordFactory wordFactory = new RandomWordFactory();
        wordFactory.setMinLength(4);
        wordFactory.setMaxLength(4);
        //wordFactory.setCharacters("0123456789中了深刻的房間啦師傅拉薩擴(kuò)大就盧卡斯的flak時(shí)間的flask的距離喀什的加拉塞克地方");

        cs.setWordFactory(wordFactory);

        FontFactory factory = new FontFactory() {
            @Override
            public Font getFont(int i) {
                return new Font("微軟雅黑",Font.BOLD,34);
            }
        };

        cs.setFontFactory(factory);

        //!!!!!!!!!!!!!!! 通過Response對(duì)象獲取響應(yīng)輸出流
        OutputStream outputStream = resp.getOutputStream();
        String code = EncoderHelper.getChallangeAndWriteImage(cs,"png",outputStream);

        //將產(chǎn)生的驗(yàn)證碼放入session
        HttpSession session = req.getSession();
        session.setAttribute("patchca",code);

        outputStream.flush();
        outputStream.close();


    }
}

跳轉(zhuǎn)的頁面

package com.kaishengit.web;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/pay")
public class PayServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/views/pay.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String code = req.getParameter("code");
        String money = req.getParameter("money");

        //驗(yàn)證驗(yàn)證碼是否正確
        HttpSession session = req.getSession();
        String sessionCode = (String) session.getAttribute("patchca");

        if(code != null && code.equals(sessionCode)) {
            session.removeAttribute("patchca");
            System.out.println("支付:" + money + "元");
        } else {
            req.setAttribute("message","驗(yàn)證碼錯(cuò)誤");
            req.setAttribute("money",money);
            req.getRequestDispatcher("/WEB-INF/views/pay.jsp").forward(req,resp);
        }


    }
}

頁面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        #change {
            text-decoration: none;
            display: block;
            width: 160px;
            height: 70px;
        }
    </style>
</head>
<body>
<h3>${message}</h3>
<form action="/pay" method="post">
    金額:<input type="text" name="money" value="${money}">
    <br>
    驗(yàn)證碼:<input type="text" name="code">
    <br>
    <a href="javascript:;" id="change">
     ![](/patchca.png)
    </a>
    <br>
    <button>支付</button>
</form>

<script src="/static/js/jquery-1.11.3.min.js"></script>
<script>
    $(function () {
        $("#change").click(function () {
            $("#img").removeAttr("src").attr("src","/patchca.png?_="+new Date().getTime());
        });
    });
</script>
</body>
</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容