欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot集成kaptcha验证码

程序员文章站 2023-12-20 16:16:22
本文实例为大家分享了springboot集成kaptcha验证码的具体代码,供大家参考,具体内容如下 1.kaptcha相关介绍 kaptcha是一个基于simplec...

本文实例为大家分享了springboot集成kaptcha验证码的具体代码,供大家参考,具体内容如下

1.kaptcha相关介绍

kaptcha是一个基于simplecaptcha的验证码开源项目。

2.集成方案

①pom.xml中配置依赖

<!-- 验证码-->
<dependency>
 <groupid>com.github.penggle</groupid>
 <artifactid>kaptcha</artifactid>
 <version>2.3.2</version>
</dependency>

②配置验证码kaptcha相关设置

@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文件

<?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中加载配置

@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

@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验证码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: