CaptchaController.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package com.yihu.wlyy.web.common.util;
  2. import com.google.code.kaptcha.impl.DefaultKaptcha;
  3. import com.google.code.kaptcha.util.Config;
  4. import com.yihu.wlyy.web.BaseController;
  5. import io.swagger.annotations.Api;
  6. import io.swagger.annotations.ApiOperation;
  7. import org.springframework.http.MediaType;
  8. import org.springframework.web.bind.annotation.*;
  9. import sun.misc.BASE64Encoder;
  10. import javax.imageio.ImageIO;
  11. import java.awt.image.BufferedImage;
  12. import java.io.ByteArrayOutputStream;
  13. import java.io.IOException;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. import java.util.Properties;
  17. import java.util.UUID;
  18. import java.util.concurrent.ConcurrentHashMap;
  19. /**
  20. * 患者端验证码生成控制器。验证码生成后保存到Redis中,并将Redis的Key与图片路径返回。
  21. * 客户端获取图片,并由用户输入后,与redis的key一块返回,用于验证是否出错。
  22. *
  23. * @author Sand
  24. * @created 2016/09/28
  25. */
  26. @Api(description = "验证码")
  27. @RestController
  28. @RequestMapping(value = "/patient/captcha", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  29. public class CaptchaController extends BaseController {
  30. private Map<String, String> captchaCache = new ConcurrentHashMap<>();
  31. private DefaultKaptcha captchaProducer = new DefaultKaptcha();
  32. public CaptchaController() {
  33. Properties properties = new Properties();
  34. properties.put("kaptcha.textproducer.font.color", "blue");
  35. properties.put("kaptcha.textproducer.font.size", "45");
  36. properties.put("kaptcha.textproducer.char.length", "4");
  37. properties.put("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
  38. Config config = new Config(properties);
  39. captchaProducer.setConfig(config);
  40. }
  41. @RequestMapping(method = RequestMethod.GET)
  42. @ApiOperation("生成验证码,用于第一次请求")
  43. public String createCaptcha(){
  44. try{
  45. return generateCaptcha();
  46. } catch (Exception e){
  47. return error(500, e.getMessage());
  48. }
  49. }
  50. @RequestMapping(value = "/{legacy_key}", method = RequestMethod.POST)
  51. @ApiOperation("刷新验证码,需提供第一次生成验证码返回的key")
  52. public String refreshCaptcha(@PathVariable("legacy_key") String legacyKey){
  53. try{
  54. cleanCaptcha(legacyKey);
  55. return generateCaptcha();
  56. } catch (Exception e){
  57. return error(500, e.getMessage());
  58. }
  59. }
  60. @RequestMapping(value = "/{key}", method = RequestMethod.GET)
  61. @ApiOperation("校验证码,提供key及用户输入的验证码")
  62. public String verifyCaptcha(@PathVariable("key") String key,
  63. @RequestParam("text") String text){
  64. try{
  65. boolean pass = false;
  66. String captcha = captchaCache.get(key);
  67. if (captcha != null && captcha.equals(text)){
  68. pass = true;
  69. cleanCaptcha(key);
  70. }
  71. return write(200, "ok", "pass", pass);
  72. } catch (Exception e){
  73. return error(500, e.getMessage());
  74. }
  75. }
  76. private String generateCaptcha() throws IOException {
  77. String captchaText = captchaProducer.createText();
  78. BufferedImage image = captchaProducer.createImage(captchaText);
  79. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  80. ImageIO.write(image, "png", outputStream);
  81. String base64Img = new BASE64Encoder().encode(outputStream.toByteArray());
  82. String key = "captcha:" + UUID.randomUUID().toString() + ":text";
  83. Map<String, String> data = new HashMap<>();
  84. data.put("key", key);
  85. data.put("image", base64Img);
  86. data.put("format", "png");
  87. captchaCache.put(key, captchaText);
  88. return write(200, "ok", "data", data);
  89. }
  90. private void cleanCaptcha(String key){
  91. captchaCache.remove(key);
  92. }
  93. }