如何在Play Framework 2.1.3中使用Captcha

Tig*_*991 3 captcha recaptcha playframework

我正在Play Framework 2.1.3中编写一个博客引擎,我希望使用验证码或类似的东西来避免自动垃圾邮件.我找到了一些关于验证码的帮助,但这只适用于Play 1.x,它对我不起作用.请提供一些有关如何在Play 2.1.3中使用验证码的帮助.谢谢!

小智 7

我确实尝试了谷歌的重拍,但是它很有效(这很难读).

我在Play框架2.2.1中使用Java实现了一个简单的解决方案.

  1. 下载Kaptcha,这是一个开源验证码包.把它放在lib文件夹中.

  2. Application.java中实现验证码方法.

    public static Result captcha(){
        DefaultKaptcha captchaPro=new DefaultKaptcha();
        captchaPro.setConfig(new Config(new Properties()));
        String text=captchaPro.createText();
        Logger.debug("Captcha:"+text);//U can put the text in cache.
        BufferedImage img=captchaPro.createImage(text);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try{
            ImageIO.write(img, "jpg", baos);
            baos.flush();
        }catch(IOException e){
            Logger.debug(e.getMessage());
        }
        return ok(baos.toByteArray()).as("image/jpg");
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 将路由添加到路由文件.访问http:// [yourhost]:[port]/captcha.

GET     /captcha                    controllers.Application.captcha
Run Code Online (Sandbox Code Playgroud)