Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use kaptcha to implement CAPTCHA in SpringBoot

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use kaptcha in SpringBoot to achieve Captcha related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe everyone will have some gains after reading this article on how to use kaptcha in SpringBoot to achieve Captcha, let's take a look at it together.

1. kaptcha related introduction

Kaptcha is a SimpleCaptcha based Captcha open source project.

2. integration scheme

① Configuration dependencies in pom.xml

com.github.penggle kaptcha 2.3.2

② Configure Captcha Kaptcha related settings

@Configurationpublic 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", "Arial, Regular Style, Microsoft Elegant Black"); Config config=new Config(properties); defaultKaptcha.setConfig(config); return defaultKaptcha; }}

or

Create myKaptcher.xml file under resources

yes 105,179,90 blue 100 50 27 code 4 Regular font, Microsoft elegant black 23456789ABCEFGHJKMNOPQRSTUVWXYZ com.google.code.kaptcha.impl.WaterRipple black com.google.code.kaptcha.impl.NoNoise 185,56,213 white 3

Then load the configuration in the startup class Application

@EnableTransactionManagement//Start annotation transaction management, equivalent to @SpringBootApplication@EnableScheduling//Start annotation timed task @MapperScan(basePackages = "com.shawn.mapper")@ImportResource(locations={"classpath:mykaptcha.xml"})public class Application extends SpringBootServletInitializer { public void static main(String[] args) throws Exception {void SpringApplication.run(Application.class, args); }}

Both configurations are available in springboot;

③KaptchaController

@CommonsLog@Controllerpublic 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); //Save Captcha to 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(); } }} About "How to use kaptcha to implement Captcha in SpringBoot" The content of this article is introduced here, thank you for reading! I believe everyone has a certain understanding of "how to use kaptcha to implement Captcha in SpringBoot." If you want to learn more, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report