SpringBoot账号密码及验证码完成登录

1、生成验证码

整合Captcha生成图形验证码


@Resource(name = "选择缓存方式")
CaptchaCache captchaCache;

@Autowired
Producer kaptchaProducer;

@Override
public Map<String, String> getCaptcha(String key) throws Exception {
	// 生成验证码
	String code = kaptchaProducer.createText();//随机生成四个验证码
	BufferedImage kaptchaImage = kaptchaProducer.createImage(code);//根据验证码生成图片
	// 生成BASE64
	ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
	ImageIO.write(kaptchaImage, "jpg", outputStream);
	String base64Img = Base64.getEncoder().encodeToString(outputStream.toByteArray());
	//返回给用户的验证码图片
	Map<String, String> imgMap = new HashMap<>();
	imgMap.put(key, "data:image/jpeg;base64," + base64Img);
	Map<String, String> cacheMap = new HashMap<>();
	cacheMap.put(CODE, code.toUpperCase());
	//服务端存储校验信息
	captchaCache.putCaptcha(key, cacheMap);
	return imgMap;
}

2、验证码校验

SpringBoot 通过实现HandlerInterceptor接口实现拦截器,通过实现WebMvcConfigurer接口实现一个配置类,在配置类中注入拦截器,最后再通过 @Configuration 注解注入配置

@Configuration
public class SpringMvcConfiguration implements WebMvcConfigurer  {

	@Bean
	public CaptchaHandlerInterceptor captchaHandlerInterceptor() {
		return new CaptchaHandlerInterceptor();
	}
	
	@Override
	public void addInterceptors(InterceptorRegistry registry){

		WebMvcConfigurer.super.addInterceptors(registry);
		registry.addInterceptor(captchaHandlerInterceptor());

	}
}

实现HandlerInterceptor接口
实现HandlerInterceptor接口需要实现 1个方法:preHandle。preHandle负责在调用login登录接口前判断验证码是否正确。

public class CaptchaHandlerInterceptor implements HandlerInterceptor {

	private final Logger log = LoggerFactory.getLogger(CaptchaHandlerInterceptor.class);
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {

		if (handler instanceof HandlerMethod) {
			
			BiyiCaptcha biyiCaptcha = ((HandlerMethod) handler).getMethodAnnotation(BiyiCaptcha.class);
			
			// controller没有添加BiyiCaptcha注解
			if (biyiCaptcha == null) {
				return true;
			}
			//获取BiyiCaptcha注解中的属性
			String ruleName = biyiCaptcha.rule();
			log.debug(ruleName);
			String serviceName = biyiCaptcha.service();
			log.debug(serviceName);
			
			//根据注解中的bean名获取spring容器中的bean
			ValidateRule rule = (ValidateRule) SpringUtil.getBean(ruleName);
			CaptchaService service = (CaptchaService) SpringUtil.getBean(serviceName);
			
			log.debug(request.getHeader(CaptchaConstant.CAPTCHA_KEY));
			log.debug(request.getHeader(CaptchaConstant.CAPTCHA));
			
			//如果请求header中带有key和captcha
			if (StringUtil.isNotEmpty(request.getHeader(CaptchaConstant.CAPTCHA_KEY))
					&& StringUtil.isNotEmpty(request.getHeader(CaptchaConstant.CAPTCHA))) {
				
				String key = request.getHeader(CaptchaConstant.CAPTCHA_KEY);
				log.debug(key);	
				String captchaJson = request.getHeader(CaptchaConstant.CAPTCHA);
				log.debug(captchaJson);
				
				@SuppressWarnings("unchecked")
				Map<String,String> captcha = (Map<String,String>)JacksonUtil.json2Bean(captchaJson, Map.class);
				
				log.debug(captcha.toString());
				
				//比较验证码
				if(rule.accordWihtRule(key)) {
					if(service.validateCaptcha(key,captcha)) {
						log.debug("Captcha validate success");
						return true;
					}else {//验证码认证失败
						throw new CaptchaValidateException(request.getRequestURI());
					}
				}
				
			} else {//请求header中没有
				throw new BadRequestAlertException(ErrorConstants.CAPTCHA_TYPE, "The information of captcha is missing", "captcha.header", "missing");
			}
		}
		return true;
	}

}

3、生成token

通过JWT工具类生成token

//通过各种操作判断这个判断那个然后生成token
String jwt = this.tokenProvider.createToken(authentication, rememberMe);

将token返给客户端

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小陈09

还请各位靓仔多多打赏呀~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值