jfinal + Patchca 验证码实现

下载 captcha 包
</pre><pre name="code" class="java">public class RegistController  extends Controller {  
@ClearInterceptor(ClearLayer.ALL)
  public void authImg() {
    render(new PatchcaRender());
  }
}

/**
 * 
* @author
* 功能: 验证码图片渲染
* 版本:1
* 修改日期:2014-06-27
* 说明:
 *
 */
public class PatchcaRender extends Render{
	@Override
	public void render() {
		MyCaptchaService cs = new MyCaptchaService();
		response.setContentType(Resource.HttpHead.ImagePng);
		response.setHeader(Resource.HttpHead.Key_Cache,Resource.HttpHead.Key_Cache_Value);
		HttpSession session = request.getSession(true);
		try {
			OutputStream os = response.getOutputStream();
			String patchca= EncoderHelper.getChallangeAndWriteImage(cs, "png", os);
			//写入验证码到session
			session.setAttribute(Resource.Patchca.SessionKey, patchca);
			os.flush();
			os.close();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
		}
	}
  public static boolean validCheckCode(Controller c,String validCode){
    String validCodeSession = c.getSessionAttr(Resource.Patchca.SessionKey);
    if (validCodeSession == null || !validCodeSession.equals(validCode)) {
      return false;
    } else {
      return true;
    }
    
  }
}


/** 验证码相关常量 **/
  public class Patchca {
    public static final String SessionKey = "PATCHCA";
    public static final String FormKey = "captcha";
  }
  
  /** http 请求头相关常量 **/
  public class HttpHead {
    public static final String ImagePng = "image/png";
    public static final String ImageJPEG = "image/jpeg";
    public static final String Key_Cache = "cache";
    public static final String Key_Cache_Value = "no-cache";
  }

/**
 * 
* @author 
* 功能: 验证码服务
* 版本:1
* 修改日期:2014-06-27
* 说明:
 *
 */
public class MyCaptchaService extends AbstractCaptchaService {
	public MyCaptchaService() {
		//文本内容
		wordFactory = new MyWordFactory();
		//字体
		List<String> fonts = new ArrayList<String>();
		fonts.add("Verdana");
		fonts.add("Tahoma");
		fontFactory = new RandomFontFactory(20,fonts);
		//效果
		textRenderer = new BestFitTextRenderer();
		//背景
		backgroundFactory = new SingleColorBackgroundFactory();
		//字体颜色
		colorFactory = new SingleColorFactory(new Color(25, 60, 170));
		//样式(曲线波纹加干扰线)
		filterFactory = new WobbleRippleFilterFactory ();
		//图片长宽
		width = 119;
		height = 35;
	}
}


/**
 * 
* @author 
* 功能: 验证码文字生成工厂
* 版本:1
* 修改日期:2014-06-27
* 说明:
 *
 */
public class MyWordFactory extends RandomWordFactory {
	public MyWordFactory() {
		   //规定验证码出现的文字
		   characters = "0123456789";
		   minLength = 4;
		   maxLength = 3; 
	}
	}
	


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值