mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-12-22 18:29:51 +08:00
图形验证码
This commit is contained in:
@@ -1,13 +1,17 @@
|
||||
package xyz.playedu.api.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import xyz.playedu.api.domain.AdminUser;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import xyz.playedu.api.types.PaginationResult;
|
||||
|
||||
/**
|
||||
* @author tengteng
|
||||
* @description 针对表【admin_users】的数据库操作Service
|
||||
* @createDate 2023-01-30 16:22:00
|
||||
*/
|
||||
* @author tengteng
|
||||
* @description 针对表【admin_users】的数据库操作Service
|
||||
* @createDate 2023-02-11 10:58:52
|
||||
*/
|
||||
public interface AdminUserService extends IService<AdminUser> {
|
||||
PaginationResult<AdminUser> paginate(int page, int size, Wrapper<AdminUser> queryWrapper);
|
||||
|
||||
AdminUser findByEmail(String email);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package xyz.playedu.api.service;
|
||||
|
||||
import xyz.playedu.api.types.ImageCaptchaResult;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface ImageCaptchaService {
|
||||
|
||||
ImageCaptchaResult generate() throws IOException;
|
||||
|
||||
boolean verify(String key, String code);
|
||||
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
package xyz.playedu.api.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import xyz.playedu.api.domain.AdminUser;
|
||||
import xyz.playedu.api.service.AdminUserService;
|
||||
import xyz.playedu.api.mapper.AdminUserMapper;
|
||||
@@ -13,10 +14,11 @@ import xyz.playedu.api.types.PaginationResult;
|
||||
/**
|
||||
* @author tengteng
|
||||
* @description 针对表【admin_users】的数据库操作Service实现
|
||||
* @createDate 2023-01-30 16:22:00
|
||||
* @createDate 2023-02-11 10:58:52
|
||||
*/
|
||||
@Service
|
||||
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser> implements AdminUserService {
|
||||
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser>
|
||||
implements AdminUserService {
|
||||
|
||||
public PaginationResult<AdminUser> paginate(int page, int size, Wrapper<AdminUser> queryWrapper) {
|
||||
IPage<AdminUser> userPage = new Page<>(page, size);
|
||||
@@ -29,6 +31,12 @@ public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser
|
||||
return pageResult;
|
||||
}
|
||||
|
||||
public AdminUser findByEmail(String email) {
|
||||
QueryWrapper<AdminUser> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("email", email);
|
||||
return this.getBaseMapper().selectOne(queryWrapper);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package xyz.playedu.api.service.impl;
|
||||
|
||||
import com.google.code.kaptcha.impl.DefaultKaptcha;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.FastByteArrayOutputStream;
|
||||
import xyz.playedu.api.service.ImageCaptchaService;
|
||||
import xyz.playedu.api.types.ImageCaptchaResult;
|
||||
import xyz.playedu.api.util.Base64Util;
|
||||
import xyz.playedu.api.util.EnvUtil;
|
||||
import xyz.playedu.api.util.RedisUtil;
|
||||
import xyz.playedu.api.util.ToolUtil;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class ImageCaptchaServiceImpl implements ImageCaptchaService {
|
||||
|
||||
@Resource
|
||||
private DefaultKaptcha defaultKaptcha;
|
||||
|
||||
@Override
|
||||
public ImageCaptchaResult generate() throws IOException {
|
||||
ImageCaptchaResult imageCaptcha = new ImageCaptchaResult();
|
||||
BufferedImage image;
|
||||
|
||||
// 图形验证码的key[api是无状态的需要key来锁定验证码的值]
|
||||
String randomKey = ToolUtil.randomString(16);
|
||||
imageCaptcha.setKey(randomKey);
|
||||
|
||||
// 生成验证码
|
||||
imageCaptcha.setCode(defaultKaptcha.createText());
|
||||
image = defaultKaptcha.createImage(imageCaptcha.getCode());
|
||||
|
||||
// 写入到redis中
|
||||
RedisUtil.set(getCacheKey(randomKey), imageCaptcha.getCode(), getExpireSeconds());
|
||||
|
||||
FastByteArrayOutputStream os = new FastByteArrayOutputStream();
|
||||
ImageIO.write(image, "png", os);
|
||||
|
||||
String base64 = "data:image/png;base64," + Base64Util.encode(os.toByteArray());
|
||||
imageCaptcha.setImage(base64);
|
||||
|
||||
return imageCaptcha;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean verify(String key, String code) {
|
||||
String cacheKey = getCacheKey(key);
|
||||
Object queryResult = RedisUtil.get(cacheKey);
|
||||
if (queryResult == null) {//未查找到[已过期 | 不存在]
|
||||
return false;
|
||||
}
|
||||
String cacheValue = (String) queryResult;
|
||||
boolean verifyResult = cacheValue.equals(code);
|
||||
|
||||
if (verifyResult) {//验证成功删除缓存->防止多次使用
|
||||
RedisUtil.del(cacheKey);
|
||||
}
|
||||
|
||||
return verifyResult;
|
||||
}
|
||||
|
||||
private String getCacheKey(String val) {
|
||||
return EnvUtil.get("playedu.captcha.cache-name") + val;
|
||||
}
|
||||
|
||||
private Long getExpireSeconds() {
|
||||
return Long.parseLong(EnvUtil.get("playedu.captcha.expire"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user