本文生成图像验证码使用了Hutool官网 — A set of tools that keep Java sweet.
图形验证码是一种用于验证用户身份的安全措施。它通过生成一张包含随机图像和文字的图片,要求用户进行识别并输入正确信息来证明其为真实用户而非计算机程序。旨在防止恶意行为,例如批量注册、登录、灌水等,以提高网站的安全性。图形验证码通常会包含随机生成的数字、字母或符号,并且还会加入一些干扰元素,如曲线、噪点或干扰线,以增加识别难度。用户需要仔细观察图像中的验证码,并手动输入正确的信息以进行验证。只有当用户输入正确的验证码后,才能顺利完成操作。图形验证码的目的是排除自动化程序或机器人,确保只有真实的人类用户才能完成验证过程。这种验证方式有效地提高了网站的安全性,阻止了恶意行为的发生,并保护了用户的个人信息。
本文使用SpringBoot搭建的一个登录的demo,用户名和密码都没有使用数据库存储,仅用了redis存储验证码信息
创建一个SpringBoot项目
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>cn.hutoolgroupId>
<artifactId>hutool-allartifactId>
<version>5.5.2version>
dependency>
dependencies>
application.yaml
server:
port: 8080
spring:
# Redis的相关配置
data:
redis:
host: localhost
port: 6379
再次申明,本文仅为了实现验证码登录功能demo,并未使用数据存储用户信息,用户名和密码都是写在代码中的
LoginService接口,用于处理登录信息
public interface LoginService {
boolean login(String username, String password, String code,String code_key);
}
ValidateCodeService接口,用于生成图形验证码
public interface ValidateCodeService {
Map<String, String> generateValidateCode();
}
登录功能
package com.validatecode.service.impl;
import com.validatecode.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
/**
* @Author YZK
* @Date 2023/11/1
*/
@Service
public class LoginServiceImpl implements LoginService {
@Autowired
StringRedisTemplate stringRedisTemplate;
@Override
public boolean login(String username, String password, String code,String code_key) {
//通过之前设置的验证码的key拿到value,再和用户输入的验证码进行比对
String validateCode = stringRedisTemplate.opsForValue().get("code:validate:"+code_key);
System.out.println(validateCode);
if ("admin".equals(username) && "123456".equals(password) && code.equals(validateCode)) {
return true;
} else {
return false;
}
}
}
生成验证码
package com.validatecode.service.impl;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.validatecode.service.ValidateCodeService;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
* @Author YZK
* @Date 2023/11/1
*/
@Service
public class ValidateCodeServiceImpl implements ValidateCodeService {
@Resource
StringRedisTemplate stringRedisTemplate;
@Override
public Map<String, String> generateValidateCode() {
//通过工具类生成图片验证码
CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(150, 48, 4, 20);
//验证码的值
String codeValue = circleCaptcha.getCode();
System.out.println("验证码为:"+codeValue);
//将图片进行base64编码,并返回
String imageBase64 = circleCaptcha.getImageBase64();
String key = UUID.randomUUID().toString().replaceAll("-", "");
//将验证码的值存入redis并设置5分钟过期
stringRedisTemplate.opsForValue().set("code:validate:" + key, codeValue, 5, TimeUnit.MINUTES);
Map<String, String> codeMap = Map.of("key", key, "imageBase64", "data:image/png;base64," + imageBase64);
return codeMap;
}
}
"data:image/png;base64," + imageBase64
在html中可以直接使用标签直接显示,后文会展示。
使用StringRedisTemplate是为了避免序列化的问题,这样可以免去配置,以最快的速度实现功能
package com.validatecode.controller;
import com.validatecode.service.LoginService;
import com.validatecode.service.ValidateCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/**
* @Author YZK
* @Date 2023/11/1
*/
@RestController
public class LoginController {
@Autowired
LoginService loginService;
@Autowired
ValidateCodeService validateCodeService;
@GetMapping("/login")
public String login(@RequestParam String input_username,
@RequestParam String input_password,
@RequestParam String code,
@RequestParam String code_key) {
if (loginService.login(input_username, input_password, code, code_key)) {
return "登录成功";
} else {
return "登录失败";
}
}
@GetMapping("/generateValidateCode")
public Map<String, String> generateValidateCode() {
return validateCodeService.generateValidateCode();
}
}
注意!!!此处login接口中的code_key参数应该由前端直接携带,而不是由用户输入,此处只是做演示
生成验证码接口
登录接口
启动服务,并访问/generateValidateCode
获得以下结果,
{
"imageBase64": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJYAAAAwCAYAAADzRIMRAAAJgklEQVR4Xu2ba2xcxRXHQ0rUT4hvLVIpX1DVVpRKfKiQ4BvhnYS0NC2V6COlAkoqtbRVoYQSAS3GTWlQIaUqLagkDaGFtHbiYDt+YhpM7E1sx8FsYjuO4ySO7fVj/Vjb+zq9Z5eZnT135s7cu3ftXWd/0pF15/xnFCU/38fcm1VgyG3/Oe1YJZaeniufNqrlYBUdoJiK45Tb0/I+HSrhwFdvuNlV6VgOyZRiOYniBJ3HpCrJpcZUFJUYpvMR1Rp+IxXLi1AUtkZJLGd0IjDcCKFb081aXimJtczoJEDcipCPNd1iE8sPqRhUJnpcIo3uUqaTQDdfhm7NXCmJVYBQUXTllnxLhZTEKiL8EMKPNUywiYX4IReuQUWix0vFxvKtvIodL1sHXubkilasz62ZEjrmFIpYVCZ6XMyIwjiVjOuu2Z2qfCEVC2H7UW7FEvexqEj0ON+oJFKNX0osm1gISmUqlygUg4pEj00ZnYrD7Y+PuCqdPLr+SkYUKl9yacViP5k4qpJBRaLHpjR2ztvE0ZVOHF1/JVNQYnmBikSPTdlZMW0TR1c6cVg/2HaHtlYaJbE+4ZE/jXNhOvoWadu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0MllIoqISf6SEAvBT2HePRKBJ1+fhPt+N5baAMWP/ba8NJ76fBn/U6sJVBgVTjkmw/T0IHSf+EvqVU/VwY2pDdS3qtdCa+tWGBqq55uoMpg8VCwqFVKUYtEBP1FJ4oSXOW5wEkbEKUdlEJFJoAIloiVDtSa9JPpJQYuFuBHFTdYrTsKI6HIquVQSyKBrqERRranK+0FJLJfohGHoclQKEZUIFLqGTBSntWR5vyh4sRATYUwyfqGTRtdnUDEYTjIwZHNlojitJcv7RVGIhaA4MnlU4/lEJ46uLyITBHESQjWHiuK0BkLzflI0YjGYSMshlIhKHtW4EypR2KsZEVUWYaLI5skoiVWgUInosRtQGJU0KIlTn2EqFJJPqZAVIdYVo+XSWgpQJlZ+wASq+ChmKyaOqhBTYUxzXilasUwEMskUGkwiGU49EZ00ur4fFKVYXkTxMmepMZEGMcmp5FGN+03RiZWLILnMzSemZyIRkzxKRGupyFWs/wO0h2vq4huljAAAAABJRU5ErkJggg==",
"key": "fa1e7431674c48118f79277c8eb662c2"
}
redis中也存在刚刚生成的验证码的key-value
查看验证码图片
成功登录