在pom.xml中添加:
<!-- 验证码 -->
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>kaptcha</artifactId>
<version>0.0.9</version>
</dependency>
CaptchaController类:
package com.example.demo;
import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.util.UUID;
@Controller
@RequestMapping("/kaptcha")
public class CaptchaController {
@Autowired
Producer defaultKaptcha;
@RequestMapping(value = "/captcha-image")
public void getKaptchaImage(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ServletOutputStream out = null;
String code = defaultKaptcha.createText();
try {
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control",
"no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
request.getSession().setAttribute("checkCode",code);
Cookie cookie = new Cookie("captchaCode",UUID.randomUUID().toString());
response.addCookie(cookie);
BufferedImage bi = defaultKaptcha.createImage(code);
out = response.getOutputStream();
ImageIO.write(bi, "jpg", out);
out.flush();
} catch (Exception e) {
e.printStackTrace();
}finally {
out.close();
}
return ;
}
}
CaptProductor:
package com.example.demo;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
@Configuration
public class CaptProductor {
@Bean
public DefaultKaptcha captchaProducer(){
DefaultKaptcha captchaProducer =new 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","125");
properties.setProperty("kaptcha.image.height","45");
properties.setProperty("kaptcha.textproducer.font.size","45");
properties.setProperty("kaptcha.session.key","code");
properties.setProperty("kaptcha.textproducer.char.length","4");
properties.setProperty("kaptcha.textproducer.font.names","宋体,楷体,微软雅黑");
Config config=new Config(properties);
captchaProducer.setConfig(config);
return captchaProducer;
}
}
application.properties:
spring.kaptcha.properties.kaptcha.textproducer.char.string=0123456789ABCDEFGHIJKLMNPQRSTUVWXYZ
spring.kaptcha.properties.kaptcha.textproducer.char.length=4
spring.kaptcha.properties.kaptcha.border=no
spring.kaptcha.properties.kaptcha.image.width=80
spring.kaptcha.properties.kaptcha.image.height=35
spring.kaptcha.properties.kaptcha.border.color=105,179,90
spring.kaptcha.properties.kaptcha.textproducer.font.color=blue
spring.kaptcha.properties.kaptcha.textproducer.font.size=30
spring.kaptcha.properties.kaptcha.textproducer.font.names=宋体,宋体,宋体,宋体
spring.kaptcha.properties.kaptcha.obscurificator.impl=com.google.code.kaptcha.impl.WaterRipple
spring.kaptcha.properties.kaptcha.session.key=code
spring.kaptcha.properties.kaptcha.noise.color=white
spring.kaptcha.properties.kaptcha.background.clear.from=white
spring.kaptcha.properties.kaptcha.background.clear.to=white