新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

如何在SpringBoot中使用kaptcha实现验证码-创新互联

这篇文章主要介绍了如何在SpringBoot中使用kaptcha实现验证码的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇如何在SpringBoot中使用kaptcha实现验证码文章都会有所收获,下面我们一起来看看吧。

成都创新互联公司长期为近1000家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为广南企业提供专业的成都网站设计、成都做网站,广南网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。

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实现验证码”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注创新互联行业资讯频道。


名称栏目:如何在SpringBoot中使用kaptcha实现验证码-创新互联
网页地址:http://www.wjwzjz.com/article/dcoeco.html
在线咨询
服务热线
服务热线:028-86922220
TOP