CaptchaConfiguration.java 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package com.koobietech.eas.config;
  2. import com.anji.captcha.model.common.Const;
  3. import com.anji.captcha.service.CaptchaCacheService;
  4. import com.anji.captcha.service.CaptchaService;
  5. import com.anji.captcha.service.impl.CaptchaServiceFactory;
  6. import com.anji.captcha.util.ImageUtils;
  7. import org.apache.commons.lang3.StringUtils;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.context.annotation.DependsOn;
  11. import org.springframework.core.io.Resource;
  12. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  13. import org.springframework.core.io.support.ResourcePatternResolver;
  14. import org.springframework.util.Base64Utils;
  15. import org.springframework.util.FileCopyUtils;
  16. import java.util.HashMap;
  17. import java.util.Map;
  18. import java.util.Properties;
  19. /**
  20. * @author lc
  21. */
  22. @Configuration
  23. public class CaptchaConfiguration {
  24. @Bean(name = "AjCaptchaCacheService")
  25. public CaptchaCacheService captchaCacheService() {
  26. return CaptchaServiceFactory.getCache("local");
  27. }
  28. @Bean
  29. @DependsOn("AjCaptchaCacheService")
  30. public CaptchaService captchaService() {
  31. Properties config = new Properties();
  32. config.put(Const.CAPTCHA_CACHETYPE, "local");
  33. config.put(Const.CAPTCHA_WATER_MARK, "爱扣钉");
  34. config.put(Const.CAPTCHA_FONT_TYPE, "宋体");
  35. config.put(Const.CAPTCHA_TYPE, "default");
  36. config.put(Const.CAPTCHA_INTERFERENCE_OPTIONS, "0");
  37. config.put(Const.ORIGINAL_PATH_JIGSAW, "classpath:images/jigsaw");
  38. config.put(Const.ORIGINAL_PATH_PIC_CLICK, "classpath:images/pic-click");
  39. config.put(Const.CAPTCHA_SLIP_OFFSET, "5");
  40. config.put(Const.CAPTCHA_AES_STATUS, "true");
  41. config.put(Const.CAPTCHA_WATER_FONT, "宋体");
  42. config.put(Const.CAPTCHA_CACAHE_MAX_NUMBER, "1000");
  43. config.put(Const.CAPTCHA_TIMING_CLEAR_SECOND, "180");
  44. boolean jigsawNotBlank = StringUtils.isNotBlank(config.getProperty(Const.ORIGINAL_PATH_JIGSAW));
  45. boolean picNotBlank = StringUtils.isNotBlank(config.getProperty(Const.ORIGINAL_PATH_PIC_CLICK));
  46. if ( jigsawNotBlank && picNotBlank ) {
  47. config.put(Const.CAPTCHA_INIT_ORIGINAL, "true");
  48. initializeBaseMap(config.getProperty(Const.ORIGINAL_PATH_JIGSAW),
  49. config.getProperty(Const.ORIGINAL_PATH_PIC_CLICK));
  50. }
  51. CaptchaService captchaService = CaptchaServiceFactory.getInstance(config);
  52. return captchaService;
  53. }
  54. private static void initializeBaseMap(String jigsaw, String picClick) {
  55. ImageUtils.cacheBootImage(getResourcesImagesFile(jigsaw + "/original/*.png"),
  56. getResourcesImagesFile(jigsaw + "/slidingBlock/*.png"),
  57. getResourcesImagesFile(picClick + "/*.png"));
  58. }
  59. public static Map<String, String> getResourcesImagesFile(String path) {
  60. Map<String, String> imgMap = new HashMap<>();
  61. ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
  62. try {
  63. Resource[] resources = resolver.getResources(path);
  64. for (Resource resource : resources) {
  65. byte[] bytes = FileCopyUtils.copyToByteArray(resource.getInputStream());
  66. String string = Base64Utils.encodeToString(bytes);
  67. String filename = resource.getFilename();
  68. imgMap.put(filename, string);
  69. }
  70. } catch (Exception ignored) {}
  71. return imgMap;
  72. }
  73. }