一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - SpringBoot集成kaptcha驗證碼

SpringBoot集成kaptcha驗證碼

2021-05-13 11:44eagle-zhang Java教程

這篇文章主要為大家詳細介紹了SpringBoot集成kaptcha驗證碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了springboot集成kaptcha驗證碼的具體代碼,供大家參考,具體內容如下

1.kaptcha相關介紹

kaptcha是一個基于simplecaptcha的驗證碼開源項目。

2.集成方案

①pom.xml中配置依賴

?
1
2
3
4
5
6
<!-- 驗證碼-->
<dependency>
 <groupid>com.github.penggle</groupid>
 <artifactid>kaptcha</artifactid>
 <version>2.3.2</version>
</dependency>

②配置驗證碼kaptcha相關設置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@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下創建mykaptcher.xml文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?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>

然后在啟動類application中加載配置

?
1
2
3
4
5
6
7
8
9
10
11
12
@enabletransactionmanagement// 啟動注解事務管理,等同于xml配置方式的 <tx:annotation-driven />
@springbootapplication
@enablescheduling//啟動注解定時任務
@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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
@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);
    //將驗證碼存到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.測試效果

SpringBoot集成kaptcha驗證碼

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/zhangxing52077/article/details/75270259

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩毛片基地一区二区三区 | 国产精品香蕉夜间视频免费播放 | 东方影视欧美天天影院 | 亚洲国产成人久久综合一区 | ysav67| 久久热r在线视频精品 | 香蕉久久一区二区三区啪啪 | 久久综久久美利坚合众国 | 欧美又大又粗又长又硬 | 丝袜足控免费网站xx动漫漫画 | 国产成人 免费观看 | 欧乱色国产精品兔费视频 | 色噜噜视频影院 | 日韩性事 | 很黄的孕妇a级黄毛片 | 国产黄色大片网站 | 性吟网 | 性满足久久久久久久久 | 冰漪丰满大乳人体图片欣赏 | 精品一区二区三区高清免费不卡 | 精品一区二区三区高清免费不卡 | 国产福利在线观看第二区 | 国产专区亚洲欧美另类在线 | melody中文字幕 | 成品人视频免费观看 | 香蕉精品 | 金牛网155755水心论坛黄大父母 | 12一14性水蜜桃 | 欧美日韩精品一区二区三区视频在线 | avove本人照片 | 亚洲精品国产乱码AV在线观看 | 亚洲AV久久久噜噜噜久久 | 九九99香蕉在线视频免费 | 好姑娘完整版在线观看中文 | 欧美综合一区二区三区 | 亚洲欧美日韩中文字幕久久 | boobsmilking流奶水野战 | 日本私人影院 | 国产国语在线播放视频 | 亚洲精品中文字幕久久久久久 | 美女撒尿毛片免费看 |