kaptcha是一个基于SimpleCaptcha的验证码开源项目。官网地址:http://code.google.com/p/kaptcha/ ,一般web 应用中都离不开验证码的使用,kaptcha的使用比较方便,只需添加jar包依赖之后简单地配置就可以使用了。
今天简要记录如何在spring boot 中 使用kaptcha组件
1.添加maven依赖
<dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> <exclusions> <exclusion> <artifactId>javax.servlet-api</artifactId> <groupId>javax.servlet</groupId> </exclusion> </exclusions> </dependency>2.在application.properties 添加相应配置
#kaptcha.properties kaptcha.border=yes kaptcha.border.color=105,179,90 kaptcha.textproducer.font.color=blue kaptcha.image.width=125 kaptcha.image.height=45 kaptcha.session.key=code kaptcha.textproducer.char.length=6 kaptcha.textproducer.font.names=宋体,楷体,微软雅黑3.创建配置类
@Configuration public class CaptchaConfig { @Value("${kaptcha.border}") private String border; @Value("${kaptcha.border.color}") private String borderColor; @Value("${kaptcha.textproducer.font.color}") private String fontColor; @Value("${kaptcha.image.width}") private String imageWidth; @Value("${kaptcha.image.height}") private String imageHeight; @Value("${kaptcha.session.key}") private String sessionKey; @Value("${kaptcha.textproducer.char.length}") private String charLength; @Value("${kaptcha.textproducer.font.names}") private String fontNames; @Bean(name="captchaProducer") public DefaultKaptcha getKaptchaBean(){ DefaultKaptcha defaultKaptcha=new DefaultKaptcha(); Properties properties=new Properties(); properties.setProperty("kaptcha.border", border); properties.setProperty("kaptcha.border.color", borderColor); properties.setProperty("kaptcha.textproducer.font.color", fontColor); properties.setProperty("kaptcha.image.width", imageWidth); properties.setProperty("kaptcha.image.height", imageHeight); properties.setProperty("kaptcha.session.key", sessionKey); properties.setProperty("kaptcha.textproducer.char.length", charLength); properties.setProperty("kaptcha.textproducer.font.names", fontNames); Config config=new Config(properties); defaultKaptcha.setConfig(config); return defaultKaptcha; }4.启动
5.访问
可通过调节application.properties中相应配置,来提供验证码的复杂度