标签:用户登录 style stack 清空 安全性 exception har produce property
在我们用户登录的时候,为了安全性考虑,会增加验证码的功能,这里采用的是google的kaptcha;spirngboot是轻便,独立,使得基于spring的应用开发变得特别简单。网上有很多介绍springboot的有点,这里不多说。言归正抓,讲下登陆时验证码结合springboot的用法
引入kaptcha所需要的jar包,我这里用的是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>
下面是kapcha的javaconfig
@Configuration
public class CaptchaConfig {
@Bean(name="captchaProducer")
public DefaultKaptcha getKaptchaBean(){
DefaultKaptcha defaultKaptcha=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.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;
}
}<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.textproducer.font.color">blue</prop>
<prop key="kaptcha.image.width">125</prop>
<prop key="kaptcha.image.height">45</prop>
<prop key="kaptcha.textproducer.font.size">45</prop>
<prop key="kaptcha.session.key">code</prop>
<prop key="kaptcha.textproducer.char.length">4</prop>
<prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑</prop>
</props>
</constructor-arg>
</bean>
</property>
</bean>其中构造方法中的属性参数可以根据自己的需求来设置。
配置文件已经配好,那么如何获取自己的二维码呢,我的理解是画布的概念,然后将生成的四位的验证码生成对应的画布,然后让结果write出去。代码如下:
@RequestMapping(value = "/captcha-image")
public ModelAndView getKaptchaImage(HttpServletRequest request,
HttpServletResponse response) throws Exception {
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");
String capText = captchaProducer.createText();
System.out.println("capText: " + capText);
try {
String uuid=UUIDUtils.getUUID32().trim().toString();
redisTemplate.opsForValue().set(uuid, capText,60*5,TimeUnit.SECONDS);
Cookie cookie = new Cookie("captchaCode",uuid);
response.addCookie(cookie);
} catch (Exception e) {
e.printStackTrace();
}
BufferedImage bi = captchaProducer.createImage(capText);
ServletOutputStream out = response.getOutputStream();
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
return null;
}页面使用也比较简单如下:
<div style="float: left;"> <i><img style="height:22px;" id="codeImg" alt="点击更换" title="点击更换" src="code/captcha-image" /></i> </div>
大概方法,如上,不对之处,敬请指正
标签:用户登录 style stack 清空 安全性 exception har produce property
原文地址:http://blog.csdn.net/liunian02050328/article/details/53462053