SpringBoot集成kaptcha验证码

 更新时间:2018年07月04日 15:04:48   作者:eagle-zhang  
这篇文章主要为大家详细介绍了SpringBoot集成kaptcha验证码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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.测试效果

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

相关文章

  • Mybatis Generator逆向工程的使用详细教程

    Mybatis Generator逆向工程的使用详细教程

    这篇文章主要介绍了Mybatis Generator逆向工程的使用详细教程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-06-06
  • SpringBoot配置application.yml时遇到的错误及解决

    SpringBoot配置application.yml时遇到的错误及解决

    这篇文章主要介绍了SpringBoot配置application.yml时遇到的错误及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-07-07
  • SpringBoot中LogBack日志配置与多环境实战

    SpringBoot中LogBack日志配置与多环境实战

    在现代软件开发中,日志记录是不可或缺的一部分,Spring Boot 提供了多种日志框架的支持,其中 Logback 是一个非常流行的选择,因为它简单、高效且功能强大,本文将介绍如何在 Spring Boot 项目中配置 Logback,并实现不同环境下的日志配置,需要的朋友可以参考下
    2025-01-01
  • 在SpringBoot中实现一个订单号生成系统的示例代码

    在SpringBoot中实现一个订单号生成系统的示例代码

    在Spring Boot中设计一个订单号生成系统,主要考虑到生成的订单号需要满足的几个要求:唯一性、可扩展性、以及可能的业务相关性,本文给大家介绍了几种常见的解决方案及相应的示例代码,需要的朋友可以参考下
    2024-02-02
  • mybatis关联关系映射的实现

    mybatis关联关系映射的实现

    MyBatis的关联关系映射在复杂数据模型中至关重要,使开发人员能够以最灵活的方式满足不同项目的需求,本文就来介绍一下mybatis关联关系映射的实现,感兴趣的可以了解一下
    2023-09-09
  • Java使用递归回溯完美解决八皇后的问题

    Java使用递归回溯完美解决八皇后的问题

    这篇文章主要介绍了Java基于循环递归回溯实现八皇后问题算法,结合具体实例形式分析了java的遍历、递归、回溯等算法实现八皇后问题的具体步骤与相关操作技巧,需要的朋友可以参考下
    2021-11-11
  • Java几个实例带你进阶升华上篇

    Java几个实例带你进阶升华上篇

    与其明天开始,不如现在行动,本文为你带来几个Java书写的实际案例,对巩固编程的基础能力很有帮助,快来一起往下看看吧
    2022-03-03
  • Java实现简单树结构

    Java实现简单树结构

    这篇文章主要为大家详细介绍了Java实现简单树结构的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • Java的MyBatis框架中MyBatis Generator代码生成器的用法

    Java的MyBatis框架中MyBatis Generator代码生成器的用法

    这篇文章主要介绍了Java的MyBatis框架中Mybatis Generator代码生成器的用法,Mybatis Generator主要被用来生成繁琐的配置文件来提高效率,需要的朋友可以参考下
    2016-04-04
  • Eclipse Web项目打成war包的方法图解

    Eclipse Web项目打成war包的方法图解

    当Tomcat启动后该压缩文件自动解压缩,war包方便了web工程的发布,那么Eclipse中如何将Web项目打成war包呢?下面小编通过图文并茂的方式给大家讲解下Eclipse Web项目打成war包的方法,一起看看吧
    2016-08-08

最新评论