/**
|
* Copyright (c) 2018 人人开源 All rights reserved.
|
*
|
* https://www.renren.io
|
*
|
* 版权所有,侵权必究!
|
*/
|
|
package com.zt.security.service;
|
|
import com.wf.captcha.ArithmeticCaptcha;
|
import com.zt.common.utils.CacheUtils;
|
import org.springframework.stereotype.Service;
|
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.IOException;
|
|
/**
|
* 验证码
|
*
|
* @author Mark sunlightcs@gmail.com
|
*/
|
@Service
|
public class CaptchaService {
|
|
/**
|
* 图片验证码
|
*/
|
public void create(HttpServletResponse response, String uuid) throws IOException {
|
response.setContentType("image/gif");
|
response.setHeader("Pragma", "No-cache");
|
response.setHeader("Cache-Control", "no-cache");
|
response.setDateHeader("Expires", 0);
|
|
// 生成验证码
|
ArithmeticCaptcha captcha = new ArithmeticCaptcha(150, 40);
|
captcha.setLen(2);
|
captcha.getArithmeticString();
|
captcha.out(response.getOutputStream());
|
|
// 保存到缓存
|
CacheUtils.put("captchaCache", uuid, captcha.text());
|
}
|
|
/**
|
* 验证码效验
|
*
|
* @param uuid
|
* uuid
|
* @param code
|
* 验证码
|
* @return true:成功 false:失败
|
*/
|
public boolean validate(String uuid, String code) {
|
// 获取验证码
|
String captcha = (String) CacheUtils.get("captchaCache", uuid);
|
|
// 效验成功
|
if (code.equalsIgnoreCase(captcha)) {
|
return true;
|
}
|
|
return false;
|
}
|
}
|