美文网首页Java 杂谈扣丁学堂Java培训
扣丁学堂Java开发SpringBoot集成kaptcha验证码

扣丁学堂Java开发SpringBoot集成kaptcha验证码

作者: 994d14631d16 | 来源:发表于2018-08-29 10:05 被阅读3次

  今天扣丁学堂Java培训老师给大家介绍一下关于SpringBoot集成kaptcha验证码的具体代码,首先kaptcha是一个非常实用的验证码生成工具。有了它,你可以生成各种样式的验证码,因为它是可配置的。kaptcha工作的原理是调用com.google.code.kaptcha.servlet.KaptchaServlet,生成一个图片。同时将生成的验证码字符串放到HttpSession中,下面我们一起来看一下吧。

​  1.集成方案

  ①pom.xml中配置依赖

  

  com.github.penggle

  kaptcha

  2.3.2

  ②配置验证码Kaptcha相关设置

  @Configuration

  publicclasskaptchaConfig{

  @Bean(name="captchaProducer")

  publicDefaultKaptchagetKaptchaBean(){

  DefaultKaptchadefaultKaptcha=newDefaultKaptcha();

  Propertiesproperties=newProperties();

  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","宋体,楷体,微软雅黑");

  Configconfig=newConfig(properties);

  defaultKaptcha.setConfig(config);

  returndefaultKaptcha;

  }

  }

  或者

  在resources下创建myKaptcher.xml文件

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">

  

  

  

  

  

  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"})

  publicclassApplicationextendsSpringBootServletInitializer{

  publicstaticvoidmain(String[]args)throwsException{

  SpringApplication.run(Application.class,args);

  }

  }

  两种配置方式在springboot中均可;

  ③KaptchaController

  @CommonsLog

  @Controller

  publicclassKaptchaControllerextendsBaseController{

  @Autowired

  privateProducercaptchaProducer;

  @GetMapping("/getKaptchaImage")

  publicvoidgetKaptchaImage()throwsException{

  response.setDateHeader("Expires",0);

  //SetstandardHTTP/1.1no-cacheheaders.

  response.setHeader("Cache-Control","no-store,no-cache,must-revalidate");

  //SetIEextendedHTTP/1.1no-cacheheaders(useaddHeader).

  response.addHeader("Cache-Control","post-check=0,pre-check=0");

  //SetstandardHTTP/1.0no-cacheheader.

  response.setHeader("Pragma","no-cache");

  //returnajpeg

  response.setContentType("image/jpeg");

  //createthetextfortheimage

  StringcapText=captchaProducer.createText();

  //storethetextinthesession

  //request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY,capText);

  //将验证码存到session

  session.setAttribute(Constants.KAPTCHA_SESSION_KEY,capText);

  log.info(capText);

  //createtheimagewiththetext

  BufferedImagebi=captchaProducer.createImage(capText);

  ServletOutputStreamout=response.getOutputStream();

  //writethedataout

  ImageIO.write(bi,"jpg",out);

  try{

  out.flush();

  }finally{

  out.close();

  }

  }

  }

  2、测试效果

  以上就是关于Java开发实现验证码生成库kaptcha使用的全部内容,希望对大家的学习有所帮助,也希望大家多多支持扣丁学堂。

相关文章

网友评论

    本文标题:扣丁学堂Java开发SpringBoot集成kaptcha验证码

    本文链接:https://www.haomeiwen.com/subject/ymlhwftx.html