亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot集成kaptcha驗(yàn)證碼

 更新時(shí)間:2018年07月04日 15:04:48   作者:eagle-zhang  
這篇文章主要為大家詳細(xì)介紹了SpringBoot集成kaptcha驗(yàn)證碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了SpringBoot集成kaptcha驗(yàn)證碼的具體代碼,供大家參考,具體內(nèi)容如下

1.kaptcha相關(guān)介紹

Kaptcha是一個(gè)基于SimpleCaptcha的驗(yàn)證碼開源項(xiàng)目。

2.集成方案

①pom.xml中配置依賴

<!-- 驗(yàn)證碼-->
<dependency>
 <groupId>com.github.penggle</groupId>
 <artifactId>kaptcha</artifactId>
 <version>2.3.2</version>
</dependency>

②配置驗(yàn)證碼Kaptcha相關(guān)設(shè)置

@Configuration
public class kaptchaConfig {
  @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;
  }
}

或者

在resources下創(chuàng)建myKaptcher.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">23456789ABCEFGHJKMNOPQRSTUVWXYZ</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.NoNoise</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>

然后在啟動(dòng)類Application中加載配置

@EnableTransactionManagement// 啟動(dòng)注解事務(wù)管理,等同于xml配置方式的 <tx:annotation-driven />
@SpringBootApplication
@EnableScheduling//啟動(dòng)注解定時(shí)任務(wù)
@MapperScan(basePackages = "com.shawn.mapper")
@ImportResource(locations={"classpath:mykaptcha.xml"})
public class Application extends SpringBootServletInitializer {

  public static void main(String[] args) throws Exception {
    SpringApplication.run(Application.class, args);
  }

}

兩種配置方式在springboot中均可;

③KaptchaController

@CommonsLog
@Controller
public class KaptchaController extends BaseController {
  @Autowired
  private Producer captchaProducer;
  @GetMapping("/getKaptchaImage")
  public void getKaptchaImage() throws Exception {

    response.setDateHeader("Expires", 0);

    // Set standard HTTP/1.1 no-cache headers.
    response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
    // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
    response.addHeader("Cache-Control", "post-check=0, pre-check=0");
    // Set standard HTTP/1.0 no-cache header.
    response.setHeader("Pragma", "no-cache");
    // return a jpeg
    response.setContentType("image/jpeg");
    // create the text for the image
    String capText = captchaProducer.createText();
    // store the text in the session
    //request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
    //將驗(yàn)證碼存到session
    session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
    log.info(capText);
    // create the image with the text
    BufferedImage bi = captchaProducer.createImage(capText);
    ServletOutputStream out = response.getOutputStream();
    // write the data out
    ImageIO.write(bi, "jpg", out);
    try {
      out.flush();
    } finally {
      out.close();
    }
  }
}

3.測(cè)試效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring?Boot?整合?Fisco?Bcos部署、調(diào)用區(qū)塊鏈合約的案例

    Spring?Boot?整合?Fisco?Bcos部署、調(diào)用區(qū)塊鏈合約的案例

    本篇文章介紹?Spring?Boot?整合?Fisco?Bcos?的相關(guān)技術(shù),最最重要的技術(shù)點(diǎn),部署、調(diào)用區(qū)塊鏈合約的工程案例,本文通過流程分析給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2022-01-01
  • Java反射機(jī)制介紹

    Java反射機(jī)制介紹

    Java的反射機(jī)制是在運(yùn)行狀態(tài)中,對(duì)于任何一個(gè)類,都可以知道這個(gè)類的所有屬性和方法,對(duì)于任何一個(gè)對(duì)象,都可以調(diào)用它所有的方法和屬性,修改部分類型信息,這種動(dòng)態(tài)獲取信息以及動(dòng)態(tài)調(diào)用對(duì)象方法的功能稱為Java的反射機(jī)制
    2022-08-08
  • Java兩大工具庫(kù)Commons和Guava使用示例詳解

    Java兩大工具庫(kù)Commons和Guava使用示例詳解

    這篇文章主要為大家介紹了Java兩大工具庫(kù)Commons和Guava使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • Javaweb項(xiàng)目session超時(shí)解決方案

    Javaweb項(xiàng)目session超時(shí)解決方案

    這篇文章主要介紹了Javaweb項(xiàng)目session超時(shí)解決方案,關(guān)于解決方案分類比較明確,內(nèi)容詳細(xì),需要的朋友可以參考下。
    2017-09-09
  • java模擬post請(qǐng)求登錄貓撲示例分享

    java模擬post請(qǐng)求登錄貓撲示例分享

    這篇文章主要介紹了java模擬post請(qǐng)求登錄貓撲的小示例,需要的朋友可以參考下
    2014-02-02
  • IDEA中Maven報(bào)錯(cuò)Cannot resolve xxx的解決方法匯總(親測(cè)有效)

    IDEA中Maven報(bào)錯(cuò)Cannot resolve xxx的解決方法匯總(親測(cè)有效)

    在IDEA中的pom文件中添加了依賴,并且正確加載了相應(yīng)依賴,pom文件沒有報(bào)紅,看起來像是把所有依賴庫(kù)全部加載進(jìn)來了,但是代碼中使用依賴的類庫(kù)使報(bào)紅,本文給大家介紹了IDEA中Maven報(bào)錯(cuò)Cannot resolve xxx的解決方法匯總,需要的朋友可以參考下
    2024-06-06
  • Java使用遞歸回溯完美解決八皇后的問題

    Java使用遞歸回溯完美解決八皇后的問題

    這篇文章主要介紹了Java基于循環(huán)遞歸回溯實(shí)現(xiàn)八皇后問題算法,結(jié)合具體實(shí)例形式分析了java的遍歷、遞歸、回溯等算法實(shí)現(xiàn)八皇后問題的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2021-11-11
  • 5種java排序算法匯總工具類

    5種java排序算法匯總工具類

    這篇文章主要總結(jié)了java的快速排序,希爾排序,插入排序,堆排序,歸并排序五種排序算法,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Java雙色球系統(tǒng)開發(fā)詳解

    Java雙色球系統(tǒng)開發(fā)詳解

    這篇文章主要為大家詳細(xì)介紹了Java雙色球系統(tǒng)的開發(fā),超級(jí)簡(jiǎn)單的邏輯,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • springboot解決使用localhost或127.0.01模擬CORS失效

    springboot解決使用localhost或127.0.01模擬CORS失效

    CORS允許不同源的網(wǎng)頁請(qǐng)求訪問另一個(gè)源服務(wù)器上的某些資源,本文主要介紹了springboot解決使用localhost或127.0.01模擬CORS失效,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-07-07

最新評(píng)論