资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

怎么在SpringBoot中使用kaptcha实现验证码

今天就跟大家聊聊有关怎么在SpringBoot中使用kaptcha实现验证码,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

我们一直强调成都网站建设、网站建设对于企业的重要性,如果您也觉得重要,那么就需要我们慎重对待,选择一个安全靠谱的网站建设公司,企业网站我们建议是要么不做,要么就做好,让网站能真正成为企业发展过程中的有力推手。专业的建站公司不一定是大公司,成都创新互联作为专业的网络公司选择我们就是放心。

1.kaptcha相关介绍

Kaptcha是一个基于SimpleCaptcha的验证码开源项目。

2.集成方案

①pom.xml中配置依赖



 com.github.penggle
 kaptcha
 2.3.2

②配置验证码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文件



  
    
      
        
          
            yes
            105,179,90
            blue
            100
            50
            27
            code
            4
            宋体,楷体,微软雅黑
            23456789ABCEFGHJKMNOPQRSTUVWXYZ
            com.google.code.kaptcha.impl.WaterRipple
            black
            com.google.code.kaptcha.impl.NoNoise
            
            185,56,213
            white
            3
          
        
      
    
  

然后在启动类Application中加载配置

@EnableTransactionManagement// 启动注解事务管理,等同于xml配置方式的 
@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();
    }
  }
}

看完上述内容,你们对怎么在SpringBoot中使用kaptcha实现验证码有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


本文名称:怎么在SpringBoot中使用kaptcha实现验证码
本文网址:http://cdkjz.cn/article/ieegji.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220