1.pom中引入依赖
<!--kaptcha验证码-->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
2.启动类中配置验证码的相关配置【也可以添加kaptchaConfig.xml中配置 然后在启动类上在用@ImportResource(locations{"classpath:kaptchaConfig.xml"})引入即可,最后在写这种方式的引入】
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import java.util.Properties;
@SpringBootApplication()
@MapperScan("com.XXX.dao")
//@ImportResource(locations={"classpath:kaptchaConfig.xml"})
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class,args);
}
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
@Bean
public DefaultKaptcha getDefaultKaptcha(){
com.google.code.kaptcha.impl.DefaultKaptcha defaultKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha();
Properties properties = new Properties();
properties.setProperty("kaptcha.border", "yes");
properties.setProperty("kaptcha.border.color", "105,179,90");
properties.setProperty("kaptcha.textproducer.font.color", "blue");
properties.setProperty("kaptcha.image.width", "110");
properties.setProperty("kaptcha.image.height", "40");
properties.setProperty("kaptcha.textproducer.font.size", "30");
properties.setProperty("kaptcha.session.key", "code");
properties.setProperty("kaptcha.textproducer.char.length", "4");
properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
3.Controller中配置验证码的获取
@Autowired
private DefaultKaptcha captchaProducer;
@RequestMapping("/defaultKaptcha")
public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
byte[] captchaChallengeAsJpeg = null;
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
try {
//生产验证码字符串并保存到session中
String createText = captchaProducer.createText();
logger.debug("verifyCode:" + createText);
httpServletRequest.getSession().setAttribute("code", createText);
System.out.println(createText + "生成的---------------------");
//使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中
BufferedImage challenge = captchaProducer.createImage(createText);
ImageIO.write(challenge, "jpg", jpegOutputStream);
} catch (IllegalArgumentException e) {
httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
//定义response输出类型为image/jpeg类型,使用response输出流输出图片的byte数组
captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
httpServletResponse.setHeader("Cache-Control", "no-store");
httpServletResponse.setHeader("Pragma", "no-cache");
httpServletResponse.setDateHeader("Expires", 0);
httpServletResponse.setContentType("image/jpeg");
ServletOutputStream responseOutputStream =
httpServletResponse.getOutputStream();
responseOutputStream.write(captchaChallengeAsJpeg);
responseOutputStream.flush();
responseOutputStream.close();
}
4.页面中加载验证码即可【注意访问路径 按照自己项目修改】
$(function () {
// 刷新验证码
$("#verification").bind("click", function () {
$(this).hide().attr('src', '/verification?random=' + Math.random()).fadeIn();
});
});
<img id="verification" src="/verification" style="cursor: pointer;" title="看不清?换一张" />
Other:另一种配置方式使用xml配置kaptcha 新建kaptchaConfig.xml 在启动类前加注解
@ImportResource(locations={"classpath:kaptchaConfig.xml"})即可不用配置Bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">
<property name="config">
<bean class="com.google.code.kaptcha.util.Config">
<constructor-arg type="java.util.Properties">
<props>
<!--是否使用边框-->
<prop key = "kaptcha.border ">no</prop>
<!--边框颜色-->
<prop key="kaptcha.border.color">105,179,90</prop>
<!--验证码字体颜色-->
<prop key="kaptcha.textproducer.font.color">black</prop>
<!--验证码图片的宽度-->
<prop key="kaptcha.image.width">100</prop>
<!--验证码图片的高度-->
<prop key="kaptcha.image.height">40</prop>
<!--验证码字体的大小-->
<prop key="kaptcha.textproducer.font.size">27</prop>
<!--验证码保存在session的key-->
<prop key="kaptcha.session.key">code</prop>
<!--验证码输出的字符长度-->
<prop key="kaptcha.textproducer.char.length">4</prop>
<!--验证码的字体设置-->
<prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑</prop>
<!--验证码的取值范围-->
<prop key="kaptcha.textproducer.char.string">0123456789</prop>
<!--图片的样式-->
<prop key="kaptcha.obscurificator.impl">com.google.code.kaptcha.impl.WaterRipple</prop>
<!--干扰颜色,合法值: r,g,b 或者 white,black,blue.-->
<!-- <prop key="kaptcha.noise.color">white</prop>-->
<!--干扰实现类-->
<prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.DefaultNoise</prop>
<!--背景颜色渐变,开始颜色-->
<prop key="kaptcha.background.clear.from">185,56,213</prop>
<!--背景颜色渐变,结束颜色-->
<prop key="kaptcha.background.clear.to">white</prop>
<!--文字间隔-->
<prop key="kaptcha.textproducer.char.space">3</prop>
</props>
</constructor-arg>
</bean>
</property>
</bean>
</beans>