How to integrate Kaptcha verification codes in SpringBoot
SpringBoot Integrated Kaptcha Verification Code
Introduction
In development, the verification code function is a common and important feature. Kaptcha is a plug-in for generating verification codes provided by the famous Google company, which supports highly configurable;
This chapter will show how to implement verification code functionality through a simple example
Implementation steps
1. In the configuration file
Add the following configuration:
Since access to Google's network is restricted in China, it is recommended to use the following dependency download
<dependency> <groupId></groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>
2. Add the following code to the system public configuration class
Of course, Kaptcha configuration can also be added to the configuration file
@Configuration public class AppConfigure implements WebMvcConfigurer { /** * Verification code configuration */ @Bean public DefaultKaptcha kaptcha() { DefaultKaptcha kaptcha = new DefaultKaptcha(); Properties properties = new Properties(); ("", "yes"); ("", "100"); ("", "33"); ("", "code"); ("", "105,179,90"); ("", "30"); ("", "4"); ("", "blue"); ("", "Song style,Kaiyi,Microsoft Yahei"); (new Config(properties)); return kaptcha; } }
3. Add a method to provide verification code generation in
@Controller @RequestMapping("/kaptcha") @Slf4j public class KaptchaController { @Resource private DefaultKaptcha kaptcha; /** * Application verification code */ @GetMapping("/kaptcha") public void getKaptcha(HttpServletRequest request, HttpServletResponse response) { ("Expires", 0); ("Cache-Control", "no-store, no-cache, must-revalidate"); ("Cache-Control", "post-check=0, pre-check=0"); ("Pragma", "no-cache"); ("image/jpeg"); HttpSession session = (); ServletOutputStream outputStream = null; try { //Generate verification code String kaptchaText = (); // Save the verification code for 5 minutes (session, (), kaptchaText, Properties.EXPIRETIME_KAPTCHA.value()); ("captcha code: " + kaptchaText); //Export to the client BufferedImage bufferedImage = (kaptchaText); outputStream = (); (bufferedImage, "jpg", outputStream); (); } catch (IOException e) { throw new BusinessException(ErrorCode.CLOSE_IO_EXCEPTION); } finally { (outputStream); } } ...... }
4. Use the img tag to directly reference the front-end page
<img src="/kaptcha/kaptcha" title="Click to refresh">
Additional: Kaptcha more configurations
Attributes (constant) | describe | default value |
---|---|---|
Image border, legal value: yes, no | yes | |
Border color, legal value: r,g,b (and optional alpha) or white,black,blue. | black | |
Border thickness, legal value: >0 | 1 | |
Picture wide | 200 | |
High picture | 50 | |
Image implementation class | ||
Text implementation class | ||
Text collection, verification code value is obtained from this collection | abcde2345678gfynmnpwx | |
Verification code length | 5 | |
Font | Arial, Courier | |
Font size | 40px | |
Font color, legal values: r,g,b or white,black,blue. | black | |
Text interval | 2 | |
Interference implementation class | ||
Interference color, legal value: r,g,b or white,black,blue. | black | |
Picture style: water pattern, fish eyes, shadow | ||
Background implementation class | ||
Background color gradient, start color | light grey | |
Background color gradient, end color | white | |
Text Renderer | ||
session key | KAPTCHA_SESSION_KEY | |
session date | KAPTCHA_SESSION_DATE |
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.
Related Articles
5 examples of load balancing algorithms using Java
Load balancing refers to a symmetrical set of servers. Each server has an equivalent status and can provide services to the outside world without the assistance of other servers. This article mainly introduces relevant information about using Java to implement 5 load balancing algorithms. Friends who need it can refer to it.2021-09-09Example of Java implementing global unique ID based on database
This article mainly introduces an example of Java implementing a global unique ID based on database. The example code is introduced in this article in detail, which has certain reference learning value for everyone's learning or work. Friends who need it, please learn with the editor below.2023-04-04Detailed explanation of Lambda expression understanding and usage examples in Java8
Lambda expression is a simple writing method introduced by Java8. It is used to implement functional interfaces and can simplify the code of anonymous functions. This article introduces the understanding and usage examples of Lambda expressions in Java8. Interested friends can take a look.2024-11-11Summary of synchronized usage in Java
Using synchronized does not require manual locking and releasing operations. We only need to declare the synchronized keyword. The JVM level will help us automatically perform locking and releasing operations. Today we will focus on several uses of synchronized.2022-04-04Introduction to the features of string deduplication in Java
This article mainly introduces the feature of string deduplication in Java, which is a new feature introduced in Java 8. As for whether it is really easy to use, it depends on your opinion... If you need it, please refer to it.2015-07-07Analysis of spring-retry retry mechanism in Spring
This article mainly introduces the analysis of the spring-retry retry mechanism in Spring. Spring-retry can be used to elegantly implement the reprocessing function without intruding into the original business logic code. In spring-retry, all configurations are based on simple annotations. Friends who need it can refer to it.2024-01-01Things in Java for self-increment and self-decrease operators (++/--)
This article mainly introduces you to the matters about the self-increase and self-decrease operators (++/--) in Java. The article introduces the example code in detail, which has certain reference learning value for everyone's learning or work. Friends who need it, let's learn together.2019-02-02Detailed explanation of Java basics packaging
This article mainly introduces the basic Java packaging, which has certain reference value. Interested friends can refer to it. I hope it can help you.2022-01-01
Summary of several ways to print logs in java
This article mainly introduces several ways to print logs in Java, which is of great reference value. I hope it will be helpful to everyone. If there are any errors or no complete considerations, I hope you will be very encouraged.2023-11-11An article will introduce you to Java arithmetic operators (addition, subtraction, multiplication, and division, character connection)
This article mainly introduces Java basic data types and operators, and analyzes Java basic data types, data type conversion, arithmetic operators, logical operators and other related principles and operation techniques based on examples. Friends who need it can refer to it2021-08-08