前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >jcaptcha使用

jcaptcha使用

作者头像
全栈程序员站长
发布2021-05-19 15:49:50
9460
发布2021-05-19 15:49:50
举报
文章被收录于专栏:全栈程序员必看

jcaptcha是做图片验证码的,主要说下在springboot 里面的使用方法.

pom.xml里面的配置

xml

<dependency>

<groupId>com.octo.captcha</groupId>

<artifactId>jcaptcha</artifactId>

<version>1.0</version>

</dependency>

两个java类

CaptchaService 处理图片

代码语言:javascript
复制
import com.octo.captcha.component.image.backgroundgenerator.GradientBackgroundGenerator;
import com.octo.captcha.component.image.color.SingleColorGenerator;
import com.octo.captcha.component.image.fontgenerator.RandomFontGenerator;
import com.octo.captcha.component.image.textpaster.NonLinearTextPaster;
import com.octo.captcha.component.image.wordtoimage.ComposedWordToImage;
import com.octo.captcha.component.word.wordgenerator.RandomWordGenerator;
import com.octo.captcha.engine.GenericCaptchaEngine;
import com.octo.captcha.image.gimpy.GimpyFactory;
import com.octo.captcha.service.captchastore.FastHashMapCaptchaStore;
import com.octo.captcha.service.image.DefaultManageableImageCaptchaService;
import com.octo.captcha.service.image.ImageCaptchaService;

import java.awt.*;


public class CaptchaService {
    private static class SingletonHolder {
        private static ImageCaptchaService imageCaptchaService = new DefaultManageableImageCaptchaService(
                new FastHashMapCaptchaStore(),
                new GenericCaptchaEngine(
                        new GimpyFactory[]{new GimpyFactory(
                                new RandomWordGenerator("123456789ABCE"),
                                new ComposedWordToImage(
                                        new RandomFontGenerator(20, 20, new Font[]{new Font("Arial", 20, 20)}),
                                        new GradientBackgroundGenerator(90, 30, new SingleColorGenerator(new Color(235, 255, 255)), new SingleColorGenerator(new Color(255, 195, 230))),
                                        new NonLinearTextPaster(4, 4, new Color(11, 11, 11))
                                )
                        )}
                ),
                180,
                180000,
                20000
        );
    }

    private CaptchaService() {
    }

    public static ImageCaptchaService getInstance() {
        return SingletonHolder.imageCaptchaService;
  }
}

CaptchaController 处理页面请求

代码语言:javascript
复制
import com.octo.captcha.service.CaptchaServiceException;
import com.smspai.sms.website.config.CaptchaService;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;


@Controller
@RequestMapping("/component")
public class CaptchaController {
	// slf4j logger
	private final static Logger logger = LoggerFactory.getLogger(CaptchaController.class);

	@ResponseBody
	@RequestMapping(value = "/captcha") //captcha captchaImage
	public void getJCaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache, must-revalidate");
		response.addHeader("Cache-Control", "post-check=0, pre-check=0");
		response.setHeader("Pragma", "no-cache");
		response.setContentType("image/jpeg");
		BufferedImage bi = CaptchaService.getInstance().getImageChallengeForID(request.getSession(true)
				.getId());
		ServletOutputStream out = response.getOutputStream();
		ImageIO.write(bi, "jpg", out);
		try {
			out.flush();
		} finally {
			out.close();
		}
		return;
	}

	//    经验:@Param("code") String code 这两个名字必须相同,都要叫code,这是绑定的
	@ResponseBody
	@RequestMapping(value = "/check", method = RequestMethod.POST) // check validate
	@ResponseStatus(HttpStatus.OK)
	public Object validate(@Param("code") String code, HttpServletRequest request) {
		ModelMap modelMap = new ModelMap();
		boolean isCaptchaCorrect = false;
		try {
			isCaptchaCorrect = CaptchaService.getInstance().validateResponseForID(request.getSession().getId(), code.toUpperCase());
//        if (isCaptchaCorrect) {
//
//        }
		} catch (CaptchaServiceException exception) {
			logger.debug(">>>>>>>[CaptchaController.validate]:\n "  + exception.getMessage());
			modelMap.addAttribute("refresh", true);
		}
		modelMap.addAttribute("success", isCaptchaCorrect);

		logger.debug(">>>>>>>[CaptchaController.validate]: \n" + code + ":" + isCaptchaCorrect);

		return modelMap;
	}
}

页面设置 及 js

代码语言:javascript
复制
<div class="field-container">
	<input  name="checkcode" type="text"  data-placeholder="验证码" placeholder="验证码"/>
	<img style="float:left;cursor: pointer;border: 1px solid #ddd;margin-left: 10px;" src="/component/captcha" id="J_captcha" title="点击刷新验证码" border="0" height="30"/>
</div>

检查验证码

代码语言:javascript
复制
$(".field[name=checkcode]").keyup(function (event) {

	var _this = $(this);
	var val = $.trim(_this.val());
	if (val.length == 4) {
		$.ajax({
			type: 'POST',
			dataType: 'json',
			url: '/component/check',
			data: {
				code: val
			},
			success: function (result) {
				if (result.success) {

					$(".ccstatus").show();
				} else if (result.refresh) {
					$("#J_captcha").click();
				} else {
					$(".ccstatus").hide();
				}
			},
			error: function () {
				$(".ccstatus").hide();
			}

		});

	} else {
		$(".ccstatus").hide();
	}

});

验证码刷新

代码语言:javascript
复制
$("#J_captcha").click(function () {
	var _this = $(this);
	var ts = new Date().getTime();
	_this.attr('src', '/component/captcha?ts=' + ts);
	$(".field[name=checkcode]").val('');
	$(".field[name=checkcode]").focus();
});

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/100412.html原文链接:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
验证码
腾讯云新一代行为验证码(Captcha),基于十道安全栅栏, 为网页、App、小程序开发者打造立体、全面的人机验证。最大程度保护注册登录、活动秒杀、点赞发帖、数据保护等各大场景下业务安全的同时,提供更精细化的用户体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档