欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

SpringBoot集成Kaptcha實(shí)現(xiàn)驗(yàn)證碼功能實(shí)例詳解

在一個(gè)web應(yīng)用中驗(yàn)證碼是一個(gè)常見的元素。不管是防止機(jī)器人還是爬蟲都有一定的作用,我們是自己編寫生產(chǎn)驗(yàn)證碼的工具類,也可以使用一些比較方便的驗(yàn)證碼工具。在網(wǎng)上收集一些資料之后,今天給大家介紹一下kaptcha的和springboot一起使用的簡單例子。

創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比衛(wèi)輝網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式衛(wèi)輝網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋衛(wèi)輝地區(qū)。費(fèi)用合理售后完善,十年實(shí)體公司更值得信賴。

準(zhǔn)備工作:

1.你要有一個(gè)springboot的hello world的工程,并能正常運(yùn)行。

2.導(dǎo)入kaptcha的maven:

<!-- https://mvnrepository.com/artifact/com.github.penggle/kaptcha --> 
<dependency> 
 <groupId>com.github.penggle</groupId> 
 <artifactId>kaptcha</artifactId> 
 <version>2.3.2</version> 
</dependency> 

開始實(shí)驗(yàn):

我們有兩種方式在springboot中使用kaptcha

第一種使用.xml的配置方式配置生成kaptcha的bean對(duì)象,在啟動(dòng)類上@ImportResource這個(gè)xml文件;在controller中注入其對(duì)象并使用

第二種是把kaptcha作為工程的一個(gè)類,加上@component注解在返回kaptcha的方法中加上@Bean注解,再在controller中注入其對(duì)象。

第一種方法:

在resources中創(chuàng)建一個(gè)xxx.xml文件 如:

mykaptcha.xml文件:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 
 <bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha"> 
  <property name="config"> 
   <bean class="com.google.code.kaptcha.util.Config"> 
    <constructor-arg type="java.util.Properties"> 
     <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">100</prop> 
       <prop key="kaptcha.image.height">50</prop> 
       <prop key="kaptcha.textproducer.font.size">27</prop> 
       <prop key="kaptcha.session.key">code</prop> 
       <prop key="kaptcha.textproducer.char.length">4</prop> 
       <prop key="kaptcha.textproducer.font.names">宋體,楷體,微軟雅黑</prop> 
       <prop key="kaptcha.textproducer.char.string">0123456789ABCEFGHIJKLMNOPQRSTUVWXYZ</prop> 
       <prop key="kaptcha.obscurificator.impl">com.google.code.kaptcha.impl.WaterRipple</prop> 
       <prop key="kaptcha.noise.color">black</prop> 
       <prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.DefaultNoise</prop> 
       <prop key="kaptcha.background.clear.from">185,56,213</prop> 
       <prop key="kaptcha.background.clear.to">white</prop> 
       <prop key="kaptcha.textproducer.char.space">3</prop> 
     </props> 
    </constructor-arg> 
   </bean> 
  </property> 
 </bean> 
</beans> 

在springboot啟動(dòng)類上引入這個(gè)文件

@SpringBootApplication 
@ImportResource(locations={"classpath:mykaptcha.xml"}) 
public class Application { 
 public static void main(String[] args) { 
  SpringApplication.run(Application.class, args); 
 } 
} 

在controller中使用:

@Autowired 
DefaultKaptcha defaultKaptcha; 
...... 
@RequestMapping("/defaultKaptcha") 
 public void defaultKaptcha(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) throws Exception{ 
   byte[] captchaChallengeAsJpeg = null; 
    ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream(); 
    try { 
    //生產(chǎn)驗(yàn)證碼字符串并保存到session中 
    String createText = defaultKaptcha.createText(); 
    httpServletRequest.getSession().setAttribute("vrifyCode", createText); 
    //使用生產(chǎn)的驗(yàn)證碼字符串返回一個(gè)BufferedImage對(duì)象并轉(zhuǎn)為byte寫入到byte數(shù)組中 
    BufferedImage challenge = defaultKaptcha.createImage(createText); 
    ImageIO.write(challenge, "jpg", jpegOutputStream); 
    } catch (IllegalArgumentException e) { 
     httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND); 
     return; 
    } 
    //定義response輸出類型為image/jpeg類型,使用response輸出流輸出圖片的byte數(shù)組 
    captchaChallengeAsJpeg = jpegOutputStream.toByteArray(); 
    httpServletResponse.setHeader("Cache-Control", "no-store"); 
    httpServletResponse.setHeader("Pragma", "no-cache"); 
    httpServletResponse.setDateHeader("Expires", 0); 
    httpServletResponse.setContentType("image/jpeg"); 
    ServletOutputStream responseOutputStream = 
      httpServletResponse.getOutputStream(); 
    responseOutputStream.write(captchaChallengeAsJpeg); 
    responseOutputStream.flush(); 
    responseOutputStream.close(); 
 } 

驗(yàn)證的方法:

@RequestMapping("/imgvrifyControllerDefaultKaptcha") 
public ModelAndView imgvrifyControllerDefaultKaptcha(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){ 
 ModelAndView andView = new ModelAndView(); 
  String captchaId = (String) httpServletRequest.getSession().getAttribute("vrifyCode"); 
  String parameter = httpServletRequest.getParameter("vrifyCode"); 
  System.out.println("Session vrifyCode "+captchaId+" form vrifyCode "+parameter); 
  
 if (!captchaId.equals(parameter)) { 
  andView.addObject("info", "錯(cuò)誤的驗(yàn)證碼"); 
  andView.setViewName("index"); 
 } else { 
  andView.addObject("info", "登錄成功"); 
  andView.setViewName("succeed"); 
   
 } 
 return andView; 
} 

模板html:

<!DOCTYPE html> 
<html> 
<head lang="en"> 
 <meta charset="UTF-8" /> 
 <title>hello</title> 
</head> 
<body> 
 <h2 th:text="${info}" /> 
 <div> 
  <!-- <img alt="這是圖片" src="/img/001.png"/> --> 
  <img alt="驗(yàn)證碼" onclick = "this.src='/defaultKaptcha?d='+new Date()*1" src="/defaultKaptcha" /> 
 </div> 
 <form action="imgvrifyControllerDefaultKaptcha"> 
  <input type="text" name="vrifyCode" /> 
  <input type="submit" value="提交"></input> 
 </form> 
</body> 
</html> 

啟動(dòng)并訪問:

SpringBoot 集成Kaptcha實(shí)現(xiàn)驗(yàn)證碼功能實(shí)例詳解

提交:

SpringBoot 集成Kaptcha實(shí)現(xiàn)驗(yàn)證碼功能實(shí)例詳解SpringBoot 集成Kaptcha實(shí)現(xiàn)驗(yàn)證碼功能實(shí)例詳解

第二中方發(fā):

這種方法把.xml文件換成使用代碼來配置:

KaptchaConfig.Java:

import java.util.Properties; 
import org.springframework.context.annotation.Bean; 
import org.springframework.stereotype.Component; 
import com.google.code.kaptcha.impl.DefaultKaptcha; 
import com.google.code.kaptcha.util.Config; 
@Component 
public class KaptchaConfig { 
 @Bean 
 public DefaultKaptcha getDefaultKaptcha(){ 
  com.google.code.kaptcha.impl.DefaultKaptcha defaultKaptcha = new com.google.code.kaptcha.impl.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", "110"); 
  properties.setProperty("kaptcha.image.height", "40"); 
  properties.setProperty("kaptcha.textproducer.font.size", "30"); 
  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; 
 } 
} 

注意要去掉啟動(dòng)類中引入的.xml文件,不然會(huì)有兩個(gè)相同的對(duì)象,而你沒有指明要注入哪一個(gè)的話啟動(dòng)會(huì)失敗。

啟動(dòng)并測試:

SpringBoot 集成Kaptcha實(shí)現(xiàn)驗(yàn)證碼功能實(shí)例詳解SpringBoot 集成Kaptcha實(shí)現(xiàn)驗(yàn)證碼功能實(shí)例詳解

到這里就算成功了。(也有使用jcaptcha的,只是他們最好不要再一個(gè)工程中使用,使用到了相同的類,有時(shí)候會(huì)導(dǎo)致異常。)

補(bǔ)充:對(duì)于kaptcha的配置屬性大家可以找找,根據(jù)屬性就可以配置了。

總結(jié)

以上所述是小編給大家介紹的SpringBoot 集成Kaptcha實(shí)現(xiàn)驗(yàn)證碼功能實(shí)例詳解,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!

標(biāo)題名稱:SpringBoot集成Kaptcha實(shí)現(xiàn)驗(yàn)證碼功能實(shí)例詳解
新聞來源:http://chinadenli.net/article26/jogojg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、搜索引擎優(yōu)化、靜態(tài)網(wǎng)站、建站公司、網(wǎng)站導(dǎo)航、網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計(jì)
欧美一级特黄大片做受大屁股| 日韩综合国产欧美一区| 国产精品免费自拍视频| 精品一区二区三区不卡少妇av| 国产一二三区不卡视频| 在线欧美精品二区三区| 国产一区二区三区午夜精品 | 美日韩一区二区精品系列| 精品午夜福利无人区乱码| 国产精品免费不卡视频| 在线九月婷婷丁香伊人| 欧美又黑又粗大又硬又爽| 欧美成人免费一级特黄| 日韩精品一区二区三区射精| 九九蜜桃视频香蕉视频| 欧美有码黄片免费在线视频| 亚洲欧美国产精品一区二区| 自拍偷拍福利视频在线观看| 少妇特黄av一区二区三区| 国产亚洲中文日韩欧美综合网| 中文字幕亚洲精品在线播放| 丁香七月啪啪激情综合| 日韩毛片视频免费观看| 国产一级性生活录像片| 日韩精品成区中文字幕| 亚洲视频偷拍福利来袭| 日本欧美一区二区三区就 | 国产精品久久三级精品| 国产欧美韩日一区二区三区| 日本视频在线观看不卡| 国产香蕉国产精品偷在线观看 | 午夜精品久久久99热连载| 五月天综合网五月天综合网| 精品少妇人妻一区二区三区| 久久精品国产熟女精品| 日韩欧美国产亚洲一区| 国产成人精品国产亚洲欧洲| 欧美午夜视频免费观看| 欧美午夜性刺激在线观看| 精品午夜福利无人区乱码| 欧美字幕一区二区三区|