现在的位置: 首页 > 综合 > 正文

jfinal + Patchca 验证码实现

2018年05月24日 ⁄ 综合 ⁄ 共 2062字 ⁄ 字号 评论关闭
下载 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; 
	}
	}
	

抱歉!评论已关闭.