1.首先自动配置类:CaptchaAutoConfiguration
2.验证码配置类:CaptchaConfiguration
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package org.hzero.starter.captcha.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
import javax.annotation.Nonnull;
import org.hzero.core.redis.RedisHelper;
import org.hzero.starter.captcha.domain.core.manager.CaptchaManager;
import org.hzero.starter.captcha.domain.core.manager.DefaultCaptchaManager;
import org.hzero.starter.captcha.domain.core.provider.CaptchaProvider;
import org.hzero.starter.captcha.domain.core.repository.CaptchaRepository;
import org.hzero.starter.captcha.domain.core.repository.RedisCaptchaRepository;
import org.hzero.starter.captcha.infra.builder.CaptchaBuilder.ObjectMapperSetter;
import org.hzero.starter.captcha.infra.helper.CaptchaHelper.CaptchaManagerSetter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties({CaptchaProperties.class})
@ComponentScan(
basePackages = {"org.hzero.starter.captcha.domain"}
)
public class CaptchaConfiguration {
public CaptchaConfiguration() {
}
@Bean
@ConditionalOnMissingBean({CaptchaManager.class})
public CaptchaManager captchaManager(CaptchaProperties captchaProperties, @Autowired(required = false) List<CaptchaProvider> captchaProviders) {
return new DefaultCaptchaManager(captchaProperties, captchaProviders);
}
@Bean
@ConditionalOnBean({RedisHelper.class})
@ConditionalOnMissingBean({CaptchaRepository.class})
public CaptchaRepository captchaRepository(RedisHelper redisHelper) {
return new RedisCaptchaRepository(redisHelper);
}
@Bean
public ObjectMapperSetter objectMapperSetter(@Nonnull ObjectMapper objectMapper) {
ObjectMapperSetter objectMapperSetter = new ObjectMapperSetter();
objectMapperSetter.setObjectMapper(objectMapper);
return objectMapperSetter;
}
@Bean
public CaptchaManagerSetter captchaManagerSetter(@Nonnull CaptchaManager captchaManager) {
CaptchaManagerSetter captchaManagerSetter = new CaptchaManagerSetter();
captchaManagerSetter.setCaptchaManager(captchaManager);
return captchaManagerSetter;
}
}
3.DefaultCaptchaManager验证码管理类
通过这个类获取相应类型的验证码
@Nullable
public String getCaptcha(@Nonnull CaptchaPreParameter preParameter, @Nonnull String captchaKey) {
CaptchaProvider captchaProvider = (CaptchaProvider)this.captchaProviders.get(preParameter.getCaptchaType());//根据类型获取相应的provider类
if (Objects.nonNull(captchaProvider)) {
return captchaProvider.getCaptcha(preParameter, captchaKey);
} else {
throw new CommonException("hzero.captcha.validate.unknown-provider", new Object[]{preParameter});
}
}
4.hzero提供了四种类型的验证码
email,geetest,image,sms。
每个类型都有一个实现 CaptchaProvider接口的类,用于生成相应的验证码。
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package org.hzero.starter.captcha.domain.core.provider;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.hzero.starter.captcha.domain.core.exception.ExceptionHandler;
import org.hzero.starter.captcha.domain.core.pre.CaptchaPreParameter;
import org.hzero.starter.captcha.domain.core.pre.CaptchaPreResult;
import org.hzero.starter.captcha.domain.core.type.CaptchaType;
import org.hzero.starter.captcha.domain.core.valid.CaptchaValidParameter;
import org.hzero.starter.captcha.domain.core.valid.CaptchaValidResult;
public interface CaptchaProvider {
@Nonnull
CaptchaType supportCaptchaType();
@Nonnull
ExceptionHandler getExceptionHandler();
@Nonnull
CaptchaPreResult<?> preProcess(@Nonnull CaptchaPreParameter preParameter);//生成验证码并存储
@Nullable
String getCaptcha(@Nonnull CaptchaPreParameter preParameter, @Nonnull String captchaKey);//获取存储的验证码
@Nonnull
CaptchaValidResult<?> validate(@Nonnull CaptchaValidParameter validParameter);
}