当前位置: 移动技术网 > IT编程>开发语言>Java > 登陆验证码kaptcha结合spring boot的用法详解

登陆验证码kaptcha结合spring boot的用法详解

2019年07月22日  | 移动技术网IT编程  | 我要评论

sunny day 藤木一惠,带感空格键2,压缩尿道避孕法

前言

在我们用户登录的时候,为了安全性考虑,会增加验证码的功能,这里采用的是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> 

去除包中自带的servlet包。在我个人的理解中springboot就是javaconfig和注解搭建起来的轻型的微架构。

下面是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; 
 } 
} 

这里的的katcha的javaconfig相当于springmvc中的bean配置,下面给是一个针对上面javaconfig的springmvc的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.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; 
 } 

如上面的代码,在用户登录的时候使用验证码以及cooike中的captchacode来实现唯一性验证,开始的时候我考虑到放到session中,当时想了下,感觉这不科学啊,比如讲captchacode放到session中,这时候验证码是一个,后来另一个用户再登陆,前一个用户还在登陆中,这时候会出现一系列的问题。这里使用cookie和redis,来应对用户的并发登陆验证。

页面使用也比较简单如下:

<div style="float: left;"> 
  <i><img style="height:22px;" id="codeimg" alt="点击更换" title="点击更换" src="code/captcha-image" /></i> 
</div> 

更换的话加一个click事件,然后清空以前在redis中对应的缓存数据;或者在获取验证码的时候,设置生存周期。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网