最近项目需要图形验证码,于是乎着手写了一个,代码如下:
/**
省略import
*/
@Controller
public class CaptchaController {
private static final Logger logger = Logger
.getLogger(CaptchaController.class);
private static final String appCaptchaCookieKey = "APPCAPTCHACOOKIE";
@Autowired
private Producer captchaProducer;
@Autowired
private RedisComponent redisComponent;
@Value("${CAPTCHA_EXPIRE_TIME}")
private int captchaExpireTime;
/**
* @param request
* @param response
* @throws IOException
*/
@RequestMapping(value = "/captcha.jpg", method = RequestMethod.GET)
public void getCaptcha(HttpServletRequest request,
HttpServletResponse response) throws IOException {
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control",
"no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
ServletOutputStream out = null;
try {
String uniqCode = UUID.randomUUID().toString().replaceAll("-", "");
logger.info("生成的图形验证码唯一键:"+uniqCode);
String capText = captchaProducer.createText();
// 存储验证码文字到redis
logger.info("生成的验证码文字:"+capText);
redisCompone