服务器之家:专注于服务器技术及软件下载分享
分类导航

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

延伸 · 阅读

精彩推荐