网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

怎么在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://bjjierui.cn/article/dpjjip.html

其他资讯