How to use java to get website login verification code
Verification code generation
This effect is achieved using the easy-captcha toolkit. First, you need to add relevant dependencies to pom.xml. The code is as follows:
<dependency> <groupId>com.github.whvcse</groupId> <artifactId>easy-captcha</artifactId> <version>1.6.2</version> </dependency>
Verification code format
easy-captcha verification code tool supports GIF, Chinese, arithmetic and other types, which are implemented through the following instance objects:
SpecCaptcha (PNG type static image verification code)
GifCaptcha (Gif type picture verification code)
ChineseCaptcha (GIF type Chinese picture verification code)
-
ArithmeticCaptcha (arithmetic type picture verification code)
Character types are divided into the following types:
TYPE_DEFAULT: mixed numbers and letters
TYPEONLYNUMBER: Pure numbers
TYPEONLYCHAR: Pure letters
TYPEONLYUPPER: Pure uppercase letters
TYPEONLYLOWER: pure lowercase letters
TYPENUMAND_UPPER: mixed numbers and uppercase letters
Backend logic Implementation
package com.yanx.controller; import com.wf.captcha.SpecCaptcha; import com.wf.captcha.base.Captcha; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.thymeleaf.util.StringUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @Controller public class KapchaController { @GetMapping("/kaptcha") public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException { httpServletResponse.setHeader("Cache-Control","no-store"); httpServletResponse.setHeader("Pragma","no-cache"); httpServletResponse.setDateHeader("Expires",0); httpServletResponse.setContentType("image/gif"); //三个参数分别为宽、高、位数 SpecCaptcha captcha=new SpecCaptcha(75,30,4); //设置类型为数字和字母混合 captcha.setCharType(Captcha.TYPE_DEFAULT); //设置字体 captcha.setCharType(Captcha.FONT_9); //验证码存入session httpServletRequest.getSession().setAttribute("verifyCode",captcha.text().toLowerCase()); //输出图片流 captcha.out(httpServletResponse.getOutputStream()); } }
Here the controller adds the defaultKaptcha() method. The path intercepted and processed by this method is/kaptcha
Implementation of front-end logic
Create a new kaptcha.html page in the static directory, the code is as follows:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>验证码</title> </head> <body> <img src="/static/imghw/default1.png" data-src="/kaptcha" class="lazy" onclick="this.src='/kaptcha?t=new Date()'" alt="How to use java to get website login verification code" > </body> </html>
Access the backend verification code path/kaptcha, the verification code is in the form of a picture. The onclick method can dynamically switch to display the verification code when the label is clicked.
Start the Spring Boot project, open the browser and enter the address:
http://localhost:8080/kaptcha.html
The effect is as follows:
Verification code verification
Back-end code
package com.yanx.controller; import com.wf.captcha.SpecCaptcha; import com.wf.captcha.base.Captcha; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.thymeleaf.util.StringUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; @Controller public class KapchaController { @GetMapping("/kaptcha") public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException { httpServletResponse.setHeader("Cache-Control","no-store"); httpServletResponse.setHeader("Pragma","no-cache"); httpServletResponse.setDateHeader("Expires",0); httpServletResponse.setContentType("image/gif"); //三个参数分别为宽、高、位数 SpecCaptcha captcha=new SpecCaptcha(75,30,4); //设置类型为数字和字母混合 captcha.setCharType(Captcha.TYPE_DEFAULT); //设置字体 captcha.setCharType(Captcha.FONT_9); //验证码存入session httpServletRequest.getSession().setAttribute("verifyCode",captcha.text().toLowerCase()); //输出图片流 captcha.out(httpServletResponse.getOutputStream()); } @GetMapping("/verify") @ResponseBody public String verify(@RequestParam("code") String code, HttpSession session){ if(StringUtils.isEmpty(code)){ return "验证码不能为空"; } String kapchaCode = session.getAttribute("verifyCode")+""; if(StringUtils.isEmpty(kapchaCode)||!code.toLowerCase().equals(kapchaCode)){ return "验证码输入错误"; } return "验证成功"; } }
Front-end code
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>验证码验证</title> </head> <body> <img src="/static/imghw/default1.png" data-src="/kaptcha" class="lazy" onclick="this.src='/kaptcha?d=new Date()'" alt="How to use java to get website login verification code" > <input type="text" maxlength="5" id="code" placeholder="请输入验证码"/> <button id="verify">验证</button> <p id="verifyResult"></p> </body> <script src="https://s3.pstatp.com/cdn/expire-1-M/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript" > $(function(){ //验证按钮点击事件 $('#verify').click(function(){ var code=$('#code').val(); $.ajax({ type:'GET',//方法类型 url:'/verify?code='+code, success:function(result){ $('#verifyResult').html(result); }, error:function(){ alert('请求失败'); }, }); }); }); </script> </html>
Effect
The above is the detailed content of How to use java to get website login verification code. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
