这里只介绍前后端分离时,kaptcha技术生成图片验证码,ssession不处理的情况下,拿到和生成session当中存储的图片验证码。其思路就是:在项目运行的时期,往session当中生成验证码和拿到验证码,赋给定义好的全局变量。
1.在sping配置文件中配置bean,:代码如下:
<bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">
<property name="config">
<bean class="com.google.code.kaptcha.util.Config">
<constructor-arg>
<props>
<prop key="kaptcha.border">yes</prop>
<prop key="kaptcha.border.color">105,179,90</prop>
<prop key="kaptcha.image.width">160</prop>
<prop key="kaptcha.image.height">50</prop>
<prop key="kaptcha.session.key">code</prop>
<prop key="kaptcha.textproducer.font.color">red</prop>
<prop key="kaptcha.textproducer.font.size">35</prop>
<prop key="kaptcha.textproducer.char.space">3</prop>
<prop key="kaptcha.textproducer.char.length">6</prop>
<!-- <prop key="kaptcha.textproducer.char.string">1234567890</prop> -->
<!--配置中文<prop key="kaptcha.textproducer.impl">com.google.code.kaptcha.text.impl.ChineseTextProducer</prop> -->
<prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑</prop>
</props>
</constructor-arg>
</bean>
</property>
</bean>
<property name="config">
<bean class="com.google.code.kaptcha.util.Config">
<constructor-arg>
<props>
<prop key="kaptcha.border">yes</prop>
<prop key="kaptcha.border.color">105,179,90</prop>
<prop key="kaptcha.image.width">160</prop>
<prop key="kaptcha.image.height">50</prop>
<prop key="kaptcha.session.key">code</prop>
<prop key="kaptcha.textproducer.font.color">red</prop>
<prop key="kaptcha.textproducer.font.size">35</prop>
<prop key="kaptcha.textproducer.char.space">3</prop>
<prop key="kaptcha.textproducer.char.length">6</prop>
<!-- <prop key="kaptcha.textproducer.char.string">1234567890</prop> -->
<!--配置中文<prop key="kaptcha.textproducer.impl">com.google.code.kaptcha.text.impl.ChineseTextProducer</prop> -->
<prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑</prop>
</props>
</constructor-arg>
</bean>
</property>
</bean>
2.kaptcha的各个属性的相关配置,可以百度。
3.在controller层注入bean,如:
private Producer captchaProducer = null;
@Autowired
public void setCaptchaProducer(Producer captchaProducer) {
this.captchaProducer = captchaProducer;
}
@Autowired
public void setCaptchaProducer(Producer captchaProducer) {
this.captchaProducer = captchaProducer;
}
4.往session当中生成图片验证码,并去除图片验证码,赋给全局变量,代码如:
/**
* 生成验证码(6位数字)
*
*/
@RequestMapping(value = "/veriCode", method = RequestMethod.GET)
public TransferResult veriCode(HttpServletResponse response, HttpServletRequest request) throws IOException {
response.setDateHeader("Expires", 0);// 禁止server端缓存
// 设置标准的 HTTP/1.1 no-cache headers.
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
// 设置IE扩展 HTTP/1.1 no-cache headers (use addHeader).
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");// 设置标准 HTTP/1.0 不缓存图片
response.setContentType("image/jpeg");// 返回一个 jpeg 图片,默认是text/html(输出文档的MIMI类型)
String capText = captchaProducer.createText();// 为图片创建文本
// 将文本保存在session中。这里就使用包中的静态变量吧
request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
BufferedImage bi = captchaProducer.createImage(capText); // 创建带有文本的图片
ServletOutputStream out = response.getOutputStream();
// 图片数据输出
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
* 生成验证码(6位数字)
*
*/
@RequestMapping(value = "/veriCode", method = RequestMethod.GET)
public TransferResult veriCode(HttpServletResponse response, HttpServletRequest request) throws IOException {
response.setDateHeader("Expires", 0);// 禁止server端缓存
// 设置标准的 HTTP/1.1 no-cache headers.
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
// 设置IE扩展 HTTP/1.1 no-cache headers (use addHeader).
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");// 设置标准 HTTP/1.0 不缓存图片
response.setContentType("image/jpeg");// 返回一个 jpeg 图片,默认是text/html(输出文档的MIMI类型)
String capText = captchaProducer.createText();// 为图片创建文本
// 将文本保存在session中。这里就使用包中的静态变量吧
request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
BufferedImage bi = captchaProducer.createImage(capText); // 创建带有文本的图片
ServletOutputStream out = response.getOutputStream();
// 图片数据输出
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
Const.code = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
return TransferResult.build(0,"验证码生成成功",true);
return TransferResult.build(0,"验证码生成成功",true);
}