1.什么是驗(yàn)證碼?
驗(yàn)證碼是一種區(qū)別用戶是計(jì)算機(jī)仍是人的公共全自動(dòng)程序。短時(shí)間是無(wú)法退出人類舞臺(tái)的,現(xiàn)在僅僅盡量提升用戶體驗(yàn)。
效果
- 賬號(hào)安全
- 反作弊
- 反爬蟲
- 防論壇灌水
- 防歹意注冊(cè)
分類
- 圖形驗(yàn)證碼
- Gif動(dòng)畫驗(yàn)證碼
- 手機(jī)短信驗(yàn)證碼
- 手機(jī)語(yǔ)音驗(yàn)證碼
- 視頻驗(yàn)證碼
- web2.0驗(yàn)證碼
2.kaptcha驗(yàn)證碼組件
kaptcha 是一個(gè)非常有用的驗(yàn)證碼生成東西;有了它,你能夠生成各種款式的驗(yàn)證碼,由于它是可裝備的。
常見裝備
- 驗(yàn)證碼的字體
- 驗(yàn)證碼字體的巨細(xì)
- 驗(yàn)證碼字體的字體色彩
- 驗(yàn)證碼內(nèi)容的規(guī)模(數(shù)字,字母,中文漢字)
- 驗(yàn)證碼圖片的巨細(xì),邊框,邊框粗細(xì),邊框色彩
- 驗(yàn)證碼的攪擾線(能夠自己承繼com.google.code.kaptcha.NoiseProducer寫一個(gè)自定義的攪擾線)
- 驗(yàn)證碼的款式(魚眼款式、3D、一般含糊……當(dāng)然也能夠承繼com.google.code.kaptcha.GimpyEngine自定義款式)
引進(jìn)maven裝備
- com.github.axet
- kaptcha
- 0.0.9
kaptcha.properties
- kaptcha.textproducer.font.color=red
- kaptcha.image.width=130
- kaptcha.image.height=44
- kaptcha.textproducer.font.size=35
- kaptcha.textproducer.char.length=4
- kaptcha.textproducer.font.names=\\u5B8B\\u4F53,\\u6977\\u4F53,\\u5FAE\\u8F6F\\u96C5\\u9ED1
- kaptcha.noise.color=gray
- kaptcha.obscurificator.impl=com.google.code.kaptcha.impl.WaterRipple
用filter過(guò)濾器來(lái)生成驗(yàn)證碼,具體步驟如下:
1、web.xml
- KaptchaFilter
- com.xxoo.admin.ui.filter.KaptchaFilter
- KaptchaFilter
- /kaptcha.jpg
闡明:驗(yàn)證碼過(guò)濾器需求放到Shiro之后,由于Shiro將包裝HttpSession.如果不,可能形成兩次的sesisonid不一樣。
2、圖片驗(yàn)證碼類
- public class CaptchaService {
- private static ImageCaptchaService instance = null;
- static {
- instance = new KaptchaImageCaptchaService();
- }
- public synchronized static ImageCaptchaService getInstance() {
- return instance;
- }
- public synchronized static boolean validate(HttpServletRequest httpServletRequest, String input) throws Exception {
- String text = instance.getText(httpServletRequest);
- boolean result = text.equalsIgnoreCase(input);
- instance.removeKaptcha(httpServletRequest);
- return result;
- }
- }
3、依據(jù)Kaptcha的驗(yàn)證碼圖片實(shí)現(xiàn)
- public class KaptchaImageCaptchaService implements ImageCaptchaService {
- private Logger logger = LoggerFactory.getLogger(getClass());
- public KaptchaImageCaptchaService() {
- }
- public static Config getConfig() throws IOException {
- Properties p = new Properties();
- p.load(new DefaultResourceLoader().getResource("kaptcha.properties").getInputStream());
- Config config = new Config(p);
- return config;
- }
- @Override
- public void create(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
- httpServletResponse.setDateHeader("Expires", 0L);
- httpServletResponse.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
- httpServletResponse.addHeader("Cache-Control", "post-check=0, pre-check=0");
- httpServletResponse.setHeader("Pragma", "no-cache");
- httpServletResponse.setContentType("image/jpeg");
- Config config = getConfig();
- Producer producer = config.getProducerImpl();
- String capText = producer.createText();
- if(logger.isDebugEnabled()){
- logger.info("create captcha:" + capText + ":" + config.getSessionKey() );
- }
- httpServletRequest.getSession().setAttribute(config.getSessionKey(), capText);
- httpServletRequest.getSession().setAttribute(config.getSessionDate(), new Date());
- BufferedImage bi = producer.createImage(capText);
- ServletOutputStream out = httpServletResponse.getOutputStream();
- ImageIO.write(bi, "jpg", out);
- out.flush();
- out.close();
- }
- @Override
- public String getText(HttpServletRequest httpServletRequest) throws Exception {
- return (String)httpServletRequest.getSession().getAttribute(getConfig().getSessionKey());
- }
- @Override
- public void removeKaptcha(HttpServletRequest httpServletRequest) throws Exception {
- httpServletRequest.getSession().removeAttribute(getConfig().getSessionKey());
- httpServletRequest.getSession().removeAttribute(getConfig().getSessionDate());
- }
- }
4、驗(yàn)證碼東西類
- public class CaptchaService {
- private static ImageCaptchaService instance = null;
- static {
- instance = new KaptchaImageCaptchaService();
- }
- public synchronized static ImageCaptchaService getInstance() {
- return instance;
- }
- public synchronized static boolean validate(HttpServletRequest httpServletRequest, String input) throws Exception {
- String text = instance.getText(httpServletRequest);
- boolean result = text.equalsIgnoreCase(input);
- instance.removeKaptcha(httpServletRequest);
- return result;
- }
- }
5、生成驗(yàn)證碼過(guò)濾器
- public class KaptchaFilter extends OncePerRequestFilter {
- private Logger logger = LoggerFactory.getLogger(getClass());
- @Override
- protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
- try {
- CaptchaService.getInstance().create(httpServletRequest,httpServletResponse);
- } catch (Exception e) {
- logger.info("create captcha error.",e);
- }
- }
- }
6、驗(yàn)證碼校驗(yàn)
- private boolean doCaptchaValidate(HttpServletRequest request, String code) {
- //比對(duì)
- try {
- if (code == null || !CaptchaService.validate(request, code)) {
- return false;
- } else {
- return true;
- }
- } catch (Exception e) {
- logger.warn("captcha check error!");
- return false;
- }
- }
總結(jié)
本文首要講述了kaptcha圖形化驗(yàn)證碼的運(yùn)用和介紹,小伙伴能夠依據(jù)自己的需求進(jìn)行引進(jìn)。
注:文章內(nèi)容和圖片均來(lái)源于網(wǎng)絡(luò),只起到信息的傳遞,不是用于商業(yè),如有侵權(quán)請(qǐng)聯(lián)系刪除!