首頁  >  文章  >  Java  >  登陸時驗證碼結合springboot的用法實例介紹

登陸時驗證碼結合springboot的用法實例介紹

零下一度
零下一度原創
2017-06-17 11:48:061654瀏覽

在一個web應用程式中驗證碼是一個常見的元素。不管是防止機器人還是爬蟲都有一定的作用,以下這篇文章主要給大家介紹了登陸驗證碼kaptcha結合spring boot用法的相關資料,需要的朋友可以參考借鑒,下面來一起看看吧。

前言

在我們使用者登入的時候,為了安全性考慮,會增加驗證碼的功能,這裡採用的是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,來應對使用者的並發登陸驗證。

頁面使用也比較簡單如下:


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

更換的話加上click事件,然後清空先前在redis中對應的快取資料;或是在取得驗證碼的時候,設定生存週期。

以上是登陸時驗證碼結合springboot的用法實例介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn