Java實現驗證碼

最終效果

實現思路

  1. 使用BufferedImage用於在內存中存儲生成的驗證碼圖片
  2. 使用Graphics來進行驗證碼圖片的繪製,並將繪製在圖片上的驗證碼存放到session中用於後續驗證
  3. 最後通過ImageIO將生成的圖片進行輸出
  4. 通過頁面提交的驗證碼和存放在session中的驗證碼對比來進行校驗


算術驗證碼

控制層實現:

public static final String SESSION_CODE_KEY_1 = "SESSION_KEY_IMAGE_CODE_1";
/**  * 模擬用戶登錄時的數字算數驗證碼  * @param response  * @return  */ @RequestMapping(value="/verifyCode", method= RequestMethod.GET)
@ResponseBody
public void getVerifyCode(HttpServletRequest request,HttpServletResponse response) {
    try {
        ImageCode imageCode = testService.createVerifyCode(request);
        //存入session  request.getSession().setAttribute(SESSION_CODE_KEY_1,imageCode);

        ImageIO.write(imageCode.getImage(), "JPEG", response.getOutputStream());
    }catch(Exception e) {}
}

服務層實現:

/**  * Created by xingyuchao on 2018/4/23.  */ @Service
public class TestService {

    @Autowired
    CodeProperties codeProperties;

    //定義運算符  private static char[] ops = new char[] {'+', '-', '*'};

    /**  * 生成驗證碼  * @return  */  public ImageCode createVerifyCode(HttpServletRequest request) {

        //create the image  BufferedImage image = new BufferedImage(codeProperties.getWidth(), codeProperties.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        // set the background color  g.setColor(new Color(0xDCDCDC));
        g.fillRect(0, 0, codeProperties.getWidth(), codeProperties.getHeight());
        // draw the border  g.setColor(Color.black);
        g.drawRect(0, 0, codeProperties.getWidth() - 1, codeProperties.getHeight() - 1);
        // create a random instance to generate the codes  Random rdm = new Random();
        // make some confusion  for (int i = 0; i < 50; i++) {
            int x = rdm.nextInt(codeProperties.getWidth());
            int y = rdm.nextInt(codeProperties.getHeight());
            g.drawOval(x, y, 0, 0);
        }
        // generate a random code  String verifyCode = generateVerifyCode(rdm);
        g.setColor(new Color(0, 100, 0));
        g.setFont(new Font("Candara", Font.BOLD, 24));
        g.drawString(verifyCode, 8, 24);
        g.dispose();
        //計算驗證碼結果  int rnd = calc(verifyCode);
        //將運算結果存到redis中  //redisService.set("verifyCode:"+user.getId(),60, rnd);  //輸出圖片  return new ImageCode(image, rnd+"", codeProperties.getExpireIn());
    }

    
    /**  * 4+2-7 組合算數  * + - *  * */  private static String generateVerifyCode(Random rdm) {
        int num1 = rdm.nextInt(10);
        int num2 = rdm.nextInt(10);
        int num3 = rdm.nextInt(10);
        char op1 = ops[rdm.nextInt(3)];
        char op2 = ops[rdm.nextInt(3)];
        String exp = ""+ num1 + op1 + num2 + op2 + num3;
        return exp;
    }



    /**  * 計算結果  * @param exp  * @return  */  private static int calc(String exp) {
        try {
            ScriptEngineManager manager = new ScriptEngineManager();
            ScriptEngine engine = manager.getEngineByName("JavaScript");
            return (Integer)engine.eval(exp);
        }catch(Exception e) {
            return 0;
        }
    }



    public static void main(String[] args) {
        //System.out.println(calc("8*8"));   System.out.println(generateVerifyCode(new Random()));
    }
    
}

通用屬性設置:

/**  * 配置文件信息  * 默認配置  */ @Data
@Component
@ConfigurationProperties(prefix = "dxh.code")
public class CodeProperties {

    /*圖片驗證碼寬度*/  private int width = 70;

    /*圖片驗證碼高度*/  private int height = 30;

    /*圖片驗證碼長度4位*/  private int length = 4;

    /*圖片驗證碼默認過期時間*/  private int expireIn = 60;
}

普通驗證碼

控制層實現:

public static final String SESSION_CODE_KEY_2 = "SESSION_KEY_IMAGE_CODE_2";
/**  * 普通驗證碼  * @param request  * @param response  * @throws IOException  */ @GetMapping("/image")
public void createCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
    //生成圖片驗證碼  ImageCode imageCode = testService.generate(request);

    //存入session  request.getSession().setAttribute(SESSION_CODE_KEY_2,imageCode);

    //將生成的圖片寫到響應中  ImageIO.write(imageCode.getImage(),"JPEG",response.getOutputStream());
}

服務層實現:

public ImageCode generate(HttpServletRequest request) {
    int width = ServletRequestUtils.getIntParameter(request, "width",
            codeProperties.getWidth());
    int height = ServletRequestUtils.getIntParameter(request, "height",
            codeProperties.getHeight());
    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 < codeProperties.getLength(); 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, codeProperties.getExpireIn());
}

/**  * 生成隨機背景條紋  *  * @param fc  * @param bc  * @return  */ private Color getRandColor(int fc, int bc) {
    Random random = new Random();
    if (fc > 255) {
        fc = 255;
    }
    if (bc > 255) {
        bc = 255;
    }
    int r = fc + random.nextInt(bc - fc);
    int g = fc + random.nextInt(bc - fc);
    int b = fc + random.nextInt(bc - fc);
    return new Color(r, g, b);
}

html:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1 align="center">驗證碼測試</h1>
    <h3 align="center">算數驗證碼測試</h3>
    <form action="/code/checkCode" method="post">
        <table align="center">
            <tr>
                <td>算數驗證碼:
                <input type="text" name="code">
                <input type="hidden" name="status" value="1">
                <img src="/code/verifyCode?+"&timestamp="+new Date().getTime()">
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2"><button type="submit">登錄</button></td>
            </tr>
        </table>
    </form>

    <hr><hr>
    <h3 align="center">普通驗證碼測試</h3>
    <form action="/code/checkCode" method="post">
        <table align="center">
            <tr>
                <td>普通驗證碼:
                    <input type="text" name="code">
                    <input type="hidden" name="status" value="2">
                    <img src="/code/image?+"&timestamp="+new Date().getTime()">
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2"><button type="submit">登錄</button></td>
            </tr>
        </table>
    </form>
</body>
</html>

---------------------------------------------------------------------------------------

上面的代碼寫的有點垃圾,不可擴展,簡單的做了下修改


增加校驗器接口,一個項目中可以這段時間使用這個驗證碼,過段時間又換了一種驗證碼

/**  * 校驗碼生成器接口  * Created by xingyuchao on 2017/12/19.  *  */ public interface ValidateCodeGenerator {

   /**  * 生成校驗碼  * @param request  * @return  */  ImageCode generate(HttpServletRequest request);
   
}

將兩種驗證碼實現方式抽離出來

/**  * Created by xingyuchao on 2018/4/26.  * 算術驗證碼, 默認生成方式  */ @Component("codeGenerator")
public class ArithmeticCodeGenerator implements ValidateCodeGenerator {

    @Autowired
    private CodeProperties codeProperties;

    //定義運算符  private static char[] ops = new char[] {'+', '-', '*'};

    /**  * 生成驗證碼  * @return  */  @Override
    public ImageCode generate(HttpServletRequest request) {

        //create the image  BufferedImage image = new BufferedImage(codeProperties.getWidth(), codeProperties.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        // set the background color  g.setColor(new Color(0xDCDCDC));
        g.fillRect(0, 0, codeProperties.getWidth(), codeProperties.getHeight());
        // draw the border  g.setColor(Color.black);
        g.drawRect(0, 0, codeProperties.getWidth() - 1, codeProperties.getHeight() - 1);
        // create a random instance to generate the codes  Random rdm = 0xDCDCDC));
        g.fillRect(0, 0, codeProperties.getWidth(), codeProperties.getHeight());
        // draw the border  g.setColor(Color.black);
        g.drawRect(0, 0, codeProperties.getWidth() - 1, codeProperties.getHeight() - 1);
        // create a random instance to generate the codes  Random rdm = new Random();
        // make some confusion  for (int i = 0; i < 50; i++) {
            int x = rdm.nextInt(codeProperties.getWidth());
            int y = rdm.nextInt(codeProperties.getHeight());
            g.drawOval(x, y, 0, 0);
        }
        // generate a random code  String verifyCode = generateVerifyCode(rdm);
        g.setColor(new Color(0, 100, 0));
        g.setFont(new Font("Candara", Font.BOLD, 24));
        g.drawString(verifyCode, 8, 24);
        g.dispose();
        //計算驗證碼結果  int rnd = calc(verifyCode);
        //將運算結果存到redis中  //redisService.set("verifyCode:"+user.getId(),60, rnd);  //輸出圖片  return new ImageCode(image, rnd+"", codeProperties.getExpireIn());
    }


    /**  * 4+2-7 組合算數  * + - *  * */  private static String generateVerifyCode(Random rdm) {
        int num1 = rdm.nextInt(10);
        int num2 = rdm.nextInt(10);
        int num3 = rdm.nextInt(10);
        char op1 = ops[rdm.nextInt(3)];
        char op2 = ops[rdm.nextInt(3)];
        String exp = ""+ num1 + op1 + num2 + op2 + num3;
        return exp;
    }



    /**  * 計算結果  * @param exp  * @return  */  private static int calc(String exp) {
        try {
            ScriptEngineManager manager = new ScriptEngineManager();
            ScriptEngine engine = manager.getEngineByName("JavaScript");
            return (Integer)engine.eval(exp);
        }catch(Exception e) {
            return 0;
        }
    }


    public static void main(String[] args) {
        //System.out.println(calc("8*8"));  System.out.println(generateVerifyCode(new Random()));
    }
}
/**  * Created by xingyuchao on 2018/4/26.  * 普通驗證碼  */ public class ImageCodeGenerator implements ValidateCodeGenerator {

    @Autowired
    CodeProperties codeProperties;

    @Override
    public ImageCode generate(HttpServletRequest request) {
        int width = ServletRequestUtils.getIntParameter(request, "width",
                codeProperties.getWidth());
        int height = ServletRequestUtils.getIntParameter(request, "height",
                codeProperties.getHeight());
        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(
相關文章
相關標籤/搜索