新增CaptchaModule模块类

新增一个类 net.wendal.nutzbook.module.CaptchaModule

  1. package net.wendal.nutzbook.module;
  2. import java.awt.image.BufferedImage;
  3. import javax.servlet.http.HttpSession;
  4. import net.wendal.nutzbook.util.Toolkit;
  5. import org.nutz.ioc.loader.annotation.IocBean;
  6. import org.nutz.mvc.annotation.At;
  7. import org.nutz.mvc.annotation.Ok;
  8. import org.nutz.mvc.annotation.Param;
  9. import cn.apiclub.captcha.Captcha;
  10. import cn.apiclub.captcha.backgrounds.GradiatedBackgroundProducer;
  11. import cn.apiclub.captcha.gimpy.FishEyeGimpyRenderer;
  12. @IocBean
  13. @At("/captcha")
  14. public class CaptchaModule {
  15. @At
  16. @Ok("raw:png")
  17. public BufferedImage next(HttpSession session, @Param("w") int w, @Param("h") int h) {
  18. if (w * h < 1) { //长或宽为0?重置为默认长宽.
  19. w = 200;
  20. h = 60;
  21. }
  22. Captcha captcha = new Captcha.Builder(w, h)
  23. .addText().addBackground(new GradiatedBackgroundProducer())
  24. .gimp(new FishEyeGimpyRenderer())
  25. .build();
  26. String text = captcha.getAnswer();
  27. session.setAttribute(Toolkit.captcha_attr, text);
  28. return captcha.getImage();
  29. }
  30. }

关键点

  • raw代表RawView
  • png是RawView中对image/png的缩写,是数据mime的描述
  • 返回值是BufferedImage,且这是image/png, 所以会转为图片显示. 还支持jpg/webp等格式,详情参考RawView的源码吧.
  • Captcha有N多的组合和配置,自行选择啦.