Home > Java > javaTutorial > How to use java to get website login verification code

How to use java to get website login verification code

PHPz
Release: 2023-05-10 18:28:06
forward
1596 people have browsed it

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>
Copy after login

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());
    }
 
}
Copy after login

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="/kaptcha" onclick="this.src=&#39;/kaptcha?t=new Date()&#39;" alt="How to use java to get website login verification code" >
</body>
</html>
Copy after login

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:

How to use java to get website login verification code

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 "验证成功";
    }
}
Copy after login

Front-end code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>验证码验证</title>
</head>
<body>
 
<img  src="/kaptcha" onclick="this.src=&#39;/kaptcha?d=new Date()&#39;" 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(){
  //验证按钮点击事件
   $(&#39;#verify&#39;).click(function(){
    var code=$(&#39;#code&#39;).val();
    $.ajax({
      type:&#39;GET&#39;,//方法类型
      url:&#39;/verify?code=&#39;+code,
      success:function(result){
        $(&#39;#verifyResult&#39;).html(result);
      },
      error:function(){
        alert(&#39;请求失败&#39;);
      },
    });
   });
  });
</script>
</html>
Copy after login

Effect

How to use java to get website login verification code

How to use java to get website login verification code

How to use java to get website login verification code

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template