验证码是一种区分用户是计算机还是人的公共全自动程序。在登录功能中加入验证码验证,可以有效防止以下安全问题:
1. 防止暴力破解:攻击者通过自动化脚本尝试大量用户名和密码组合,验证码可以显著增加攻击成本。
2. 防止恶意注册:在注册功能中,验证码可以防止恶意用户通过脚本批量注册账号。
3. 防止爬虫攻击:验证码可以阻止自动化爬虫对网站数据的抓取。
4. 提升安全性:验证码是防止自动化攻击的有效手段,能够显著提升系统的安全性。
引入依赖
在 pom.xml
配置文件中引入依赖:
js
<!-- 依赖声明 -->
<dependencies>
<!-- 验证码 -->
<dependency>
<groupId>com.xiaomayi</groupId>
<artifactId>xiaomayi-captcha</artifactId>
</dependency>
</dependencies>
接收请求
js
package com.xiaomayi.admin.controller;
import com.xiaomayi.core.utils.R;
import com.xiaomayi.logger.annotation.LoginLog;
import com.xiaomayi.logger.annotation.RequestLog;
import com.xiaomayi.logger.enums.LoginType;
import com.xiaomayi.logger.enums.RequestType;
import com.xiaomayi.system.dto.LoginDTO;
import com.xiaomayi.admin.service.LoginService;
import com.xiaomayi.system.dto.RegisterDTO;
import com.xiaomayi.tenant.annotation.TenantIgnore;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
/**
* <p>
* 用户表 前端控制器
* </p>
*
* @author 小蚂蚁云团队
* @since 2024-05-21
*/
@RestController
@Tag(name = "登录", description = "登录")
@AllArgsConstructor
public class LoginController {
@Autowired
private LoginService loginService;
/**
* 获取验证码
*
* @return 返回结果
*/
@Operation(summary = "获取验证码", description = "获取验证码")
@RequestLog(title = "获取验证码", type = RequestType.OTHER)
@GetMapping("/captcha")
public R captcha() {
return loginService.captcha();
}
}
类库工具
js
package com.xiaomayi.captcha.utils;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import com.xiaomayi.redis.core.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
* <p>
* 验证码工具类
* </p>
*
* @author 小蚂蚁云团队
* @since 2024-06-04
*/
@Component
public class CaptchaUtil {
@Autowired
private RedisCache redisCache;
/**
* 获取验证码
*
* @return 返回结果
*/
public Map<String, String> getCaptcha() {
// 实例化验证码对象,png类型
SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
// 设置字体
specCaptcha.setCharType(Captcha.FONT_1);
// 设置类型,纯数字、纯字母、字母数字混合
specCaptcha.setCharType(Captcha.TYPE_DEFAULT);
String verCode = specCaptcha.text().toLowerCase();
String key = UUID.randomUUID().toString();
// 设置Redis缓存,有效期5分钟
redisCache.setCacheObject(key, verCode, 5, TimeUnit.MINUTES);
// 将key和base64返回给前端
String base64Captcha = specCaptcha.toBase64();
// 返回结果
Map<String, String> result = new HashMap<>();
result.put("key", key);
result.put("captcha", base64Captcha);
return result;
}
}
结果输出
js
{
"code": 0,
"msg": "操作成功",
"data": {
"captcha": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIIAAAAwCAIAAABSYzXUAAARfUlEQVR4Xu1aCVRb15mWHTtpnGSapm7WdqZJJs20nSY5sRsn8ZakTVInjVNPkzNxtsatI7ANGGyM4y12cLBZbBNjExAgNrEaMPu+GGx2EPsmLLHJIHaB2ISEePM9XfH09BCLT086HB++8x/Oe/+99+nd/7v3X+6DRy1jCYDHVSzj/wPLNCwJLNOwJLBMw5LAMg1LAss0LAlwaege1ojbxzjKfz16ZD3Rp6N9LHwijkcUxxRrNVpujzsLXBpOJXbx+OX/fareJU3RqdRwWv9liHaMlpXJcKFUKLOF2WmX07g97ixwaTibqnj4YBWYgNxlKX7ru2ZR8cDYpI7T7YeG3z6/Ke0UuZ4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