前言
说到生成验证码,我们第一个想到的就是kaptcha包,因为这个包已经存在很久了,比较稳定,用的人也多网上的示例代码多,而且很多项目脚手架做验证时也都是用这个框架的。
代码
添加依赖
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>${kaptcha.version}</version>
</dependency>
配置
@Bean(name = "captchaProducer")
public DefaultKaptcha getKaptchaBean() {
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
Properties properties = new Properties();
properties.setProperty(KAPTCHA_BORDER, "yes");
properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "black");
properties.setProperty(KAPTCHA_IMAGE_WIDTH, "160");
properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "60");
properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "38");
properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCode");
properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");
properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier");
properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy");
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
控制器
import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
@RestController
public class KaptchaController {
@Autowired
private DefaultKaptcha captchaProducer;
@GetMapping("/img")
public void getKaptcha(HttpServletResponse response) throws IOException {
String text = captchaProducer.createText();
BufferedImage image = captchaProducer.createImage(text);
ImageIO.write(image, "jpg", response.getOutputStream());
}
}
访问测试
访问:http://localhost:8080/img
关于配置的分析
properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCode");
上面这段配置代码是把生成的字符串文本存到session里,但是我们写个控制器访问获取试试
@GetMapping("/getCode")
public String getCode(HttpSession session) {
return ((String) session.getAttribute("kaptchaCode"));
}
根本没有,他确实写进session里了,但是并不是这么使用的。
让session生效的配置
使用kaptcha自带的servlet,要注册进spring中
@Bean
ServletRegistrationBean captchaServlet() {
ServletRegistrationBean<Servlet> bean = new ServletRegistrationBean<>();
bean.setServlet(new KaptchaServlet());
bean.addUrlMappings("/img2");
Properties properties = new Properties();
properties.setProperty(KAPTCHA_BORDER, "yes");
properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "black");
properties.setProperty(KAPTCHA_IMAGE_WIDTH, "160");
properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "60");
properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "38");
properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCode");
properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");
properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier");
properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy");
bean.setInitParameters(new HashMap<>(((Map) properties)));
return bean;
}
这个时候访问 OK,成功,具体代码可参考若依。
|