|
@@ -0,0 +1,71 @@
|
|
|
|
+package com.mooctest.crowd.site.controller;
|
|
|
|
+
|
|
|
|
+import com.google.code.kaptcha.impl.DefaultKaptcha;
|
|
|
|
+import com.mooctest.crowd.domain.util.EncryptionUtil;
|
|
|
|
+import org.apache.shiro.SecurityUtils;
|
|
|
|
+import org.apache.shiro.session.Session;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
|
+import org.springframework.web.servlet.ModelAndView;
|
|
|
|
+
|
|
|
|
+import javax.imageio.ImageIO;
|
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @author sean
|
|
|
|
+ * @date 2017-03-21.
|
|
|
|
+ */
|
|
|
|
+@RestController
|
|
|
|
+public class CaptchaController extends BaseController {
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private DefaultKaptcha captchaProducer;
|
|
|
|
+
|
|
|
|
+// @Autowired
|
|
|
|
+// private VerificationLogic verificationLogic;
|
|
|
|
+
|
|
|
|
+ @RequestMapping(value = "/api/common/captcha")
|
|
|
|
+ public ModelAndView getKaptchaImage(HttpServletRequest request,
|
|
|
|
+ HttpServletResponse response) throws Exception {
|
|
|
|
+
|
|
|
|
+ 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");
|
|
|
|
+
|
|
|
|
+ String capText = captchaProducer.createText();
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ Session session = SecurityUtils.getSubject().getSession();
|
|
|
|
+ String md5Captcha = EncryptionUtil.encryptMD5Hex(capText);
|
|
|
|
+ session.setAttribute("captchaCode" , md5Captcha);
|
|
|
|
+
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ LOG.error("", e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ BufferedImage bi = captchaProducer.createImage(capText);
|
|
|
|
+ System.out.println("capText " + capText);
|
|
|
|
+ ServletOutputStream out = response.getOutputStream();
|
|
|
|
+ ImageIO.write(bi, "jpg", out);
|
|
|
|
+ try {
|
|
|
|
+ out.flush();
|
|
|
|
+ } finally {
|
|
|
|
+ out.close();
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+// @RequestMapping(value = UrlConstants.API_COMMON + "captcha/mobile/{mobile:\\d+}", method = RequestMethod.POST)
|
|
|
|
+// public void sendMobileCaptcha(@PathVariable("mobile") String mobile) {
|
|
|
|
+// verificationLogic.sendMobileVerfication(mobile);
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+}
|