package com.koobietech.eas.config; import com.anji.captcha.model.common.Const; import com.anji.captcha.service.CaptchaCacheService; import com.anji.captcha.service.CaptchaService; import com.anji.captcha.service.impl.CaptchaServiceFactory; import com.anji.captcha.util.ImageUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.DependsOn; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.util.Base64Utils; import org.springframework.util.FileCopyUtils; import java.util.HashMap; import java.util.Map; import java.util.Properties; /** * @author lc */ @Configuration public class CaptchaConfiguration { @Bean(name = "AjCaptchaCacheService") public CaptchaCacheService captchaCacheService() { return CaptchaServiceFactory.getCache("local"); } @Bean @DependsOn("AjCaptchaCacheService") public CaptchaService captchaService() { Properties config = new Properties(); config.put(Const.CAPTCHA_CACHETYPE, "local"); config.put(Const.CAPTCHA_WATER_MARK, "爱扣钉"); config.put(Const.CAPTCHA_FONT_TYPE, "宋体"); config.put(Const.CAPTCHA_TYPE, "default"); config.put(Const.CAPTCHA_INTERFERENCE_OPTIONS, "0"); config.put(Const.ORIGINAL_PATH_JIGSAW, "classpath:images/jigsaw"); config.put(Const.ORIGINAL_PATH_PIC_CLICK, "classpath:images/pic-click"); config.put(Const.CAPTCHA_SLIP_OFFSET, "5"); config.put(Const.CAPTCHA_AES_STATUS, "true"); config.put(Const.CAPTCHA_WATER_FONT, "宋体"); config.put(Const.CAPTCHA_CACAHE_MAX_NUMBER, "1000"); config.put(Const.CAPTCHA_TIMING_CLEAR_SECOND, "180"); boolean jigsawNotBlank = StringUtils.isNotBlank(config.getProperty(Const.ORIGINAL_PATH_JIGSAW)); boolean picNotBlank = StringUtils.isNotBlank(config.getProperty(Const.ORIGINAL_PATH_PIC_CLICK)); if ( jigsawNotBlank && picNotBlank ) { config.put(Const.CAPTCHA_INIT_ORIGINAL, "true"); initializeBaseMap(config.getProperty(Const.ORIGINAL_PATH_JIGSAW), config.getProperty(Const.ORIGINAL_PATH_PIC_CLICK)); } CaptchaService captchaService = CaptchaServiceFactory.getInstance(config); return captchaService; } private static void initializeBaseMap(String jigsaw, String picClick) { ImageUtils.cacheBootImage(getResourcesImagesFile(jigsaw + "/original/*.png"), getResourcesImagesFile(jigsaw + "/slidingBlock/*.png"), getResourcesImagesFile(picClick + "/*.png")); } public static Map getResourcesImagesFile(String path) { Map imgMap = new HashMap<>(); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); try { Resource[] resources = resolver.getResources(path); for (Resource resource : resources) { byte[] bytes = FileCopyUtils.copyToByteArray(resource.getInputStream()); String string = Base64Utils.encodeToString(bytes); String filename = resource.getFilename(); imgMap.put(filename, string); } } catch (Exception ignored) {} return imgMap; } }