全局:简化 captcha 组件,融合到 system 模块

This commit is contained in:
YunaiV 2024-02-28 20:38:35 +08:00
parent 161b5e5bfc
commit c3eae200db
49 changed files with 56 additions and 134 deletions

View File

@ -151,16 +151,6 @@
<artifactId>yudao-spring-boot-starter-biz-ip</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>cn.iocoder.cloud</groupId>
<artifactId>yudao-spring-boot-starter-captcha</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>cn.iocoder.cloud</groupId>
<artifactId>yudao-spring-boot-starter-desensitize</artifactId>
<version>${revision}</version>
</dependency>
<!-- Spring 核心 -->
<dependency>

View File

@ -36,7 +36,6 @@
<module>yudao-spring-boot-starter-biz-ip</module>
<module>yudao-spring-boot-starter-flowable</module>
<module>yudao-spring-boot-starter-captcha</module>
<module>yudao-spring-boot-starter-websocket</module>
</modules>

View File

@ -1,39 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>cn.iocoder.cloud</groupId>
<artifactId>yudao-framework</artifactId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>yudao-spring-boot-starter-captcha</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>验证码拓展
1. 基于 aj-captcha 实现滑块验证码文档https://ajcaptcha.beliefteam.cn/captcha-doc/
</description>
<dependencies>
<!-- Spring 核心 -->
<dependency>
<groupId>com.xingyuv</groupId>
<artifactId>spring-boot-starter-captcha-plus</artifactId>
</dependency>
<!-- Spring 核心 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- DB 相关 -->
<dependency>
<groupId>cn.iocoder.cloud</groupId>
<artifactId>yudao-spring-boot-starter-redis</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -1,29 +0,0 @@
package cn.iocoder.yudao.framework.captcha.config;
import cn.iocoder.yudao.framework.captcha.core.service.RedisCaptchaServiceImpl;
import com.xingyuv.captcha.properties.AjCaptchaProperties;
import com.xingyuv.captcha.service.CaptchaCacheService;
import com.xingyuv.captcha.service.impl.CaptchaServiceFactory;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.StringRedisTemplate;
import jakarta.annotation.Resource;
@AutoConfiguration
public class YudaoCaptchaConfiguration {
@Resource
private StringRedisTemplate stringRedisTemplate;
@Bean
public CaptchaCacheService captchaCacheService(AjCaptchaProperties config) {
// 缓存类型 redis/local/....
CaptchaCacheService ret = CaptchaServiceFactory.getCache(config.getCacheType().name());
if (ret instanceof RedisCaptchaServiceImpl) {
((RedisCaptchaServiceImpl) ret).setStringRedisTemplate(stringRedisTemplate);
}
return ret;
}
}

View File

@ -1,28 +0,0 @@
package cn.iocoder.yudao.framework.captcha.core.enums;
/**
* 验证码 Redis Key 枚举类
*
* @author 芋道源码
*/
public interface CaptchaRedisKeyConstants {
/**
* 验证码的请求限流
*
* KEY 格式AJ.CAPTCHA.REQ.LIMIT-%s-%s
* VALUE 数据类型String // 例如说验证失败 5 get 接口锁定
* 过期时间60
*/
String AJ_CAPTCHA_REQ_LIMIT = "AJ.CAPTCHA.REQ.LIMIT-%s-%s";
/**
* 验证码的坐标
*
* KEY 格式RUNNING:CAPTCHA:%s // AbstractCaptchaService.REDIS_CAPTCHA_KEY
* VALUE 数据类型String // PointVO.class {"secretKey":"PP1w2Frr2KEejD2m","x":162,"y":5}
* 过期时间120
*/
String AJ_CAPTCHA_RUNNING = "RUNNING:CAPTCHA:%s";
}

View File

@ -1,7 +0,0 @@
/**
* 验证码拓展
* 1. 基于 aj-captcha 实现滑块验证码文档https://ajcaptcha.beliefteam.cn/captcha-doc/
*
* @author 星语
*/
package cn.iocoder.yudao.framework.captcha;

View File

@ -1 +0,0 @@
cn.iocoder.yudao.framework.captcha.core.service.RedisCaptchaServiceImpl

View File

@ -129,11 +129,6 @@
<artifactId>yudao-spring-boot-starter-excel</artifactId>
</dependency>
<dependency>
<groupId>cn.iocoder.cloud</groupId>
<artifactId>yudao-spring-boot-starter-captcha</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
@ -173,6 +168,10 @@
<artifactId>tencentcloud-sdk-java-sms</artifactId> <!-- 短信(腾讯云) -->
</dependency>
<dependency>
<groupId>com.xingyuv</groupId>
<artifactId>spring-boot-starter-captcha-plus</artifactId> <!-- 验证码,一般用于登录使用 -->
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,29 @@
package cn.iocoder.yudao.module.system.framework.captcha.config;
import cn.iocoder.yudao.module.system.framework.captcha.core.RedisCaptchaServiceImpl;
import com.xingyuv.captcha.properties.AjCaptchaProperties;
import com.xingyuv.captcha.service.CaptchaCacheService;
import com.xingyuv.captcha.service.impl.CaptchaServiceFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
/**
* 验证码的配置类
*
* @author 芋道源码
*/
@Configuration(proxyBeanMethods = false)
public class YudaoCaptchaConfiguration {
@Bean
public CaptchaCacheService captchaCacheService(AjCaptchaProperties config,
StringRedisTemplate stringRedisTemplate) {
CaptchaCacheService captchaCacheService = CaptchaServiceFactory.getCache(config.getCacheType().name());
if (captchaCacheService instanceof RedisCaptchaServiceImpl) {
((RedisCaptchaServiceImpl) captchaCacheService).setStringRedisTemplate(stringRedisTemplate);
}
return captchaCacheService;
}
}

View File

@ -1,11 +1,9 @@
package cn.iocoder.yudao.framework.captcha.core.service;
package cn.iocoder.yudao.module.system.framework.captcha.core;
import com.xingyuv.captcha.service.CaptchaCacheService;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.redis.core.StringRedisTemplate;
import jakarta.annotation.Resource;
import java.util.concurrent.TimeUnit;
/**
@ -13,11 +11,9 @@ import java.util.concurrent.TimeUnit;
*
* @author 星语
*/
@NoArgsConstructor // 保证 aj-captcha SPI 创建
@AllArgsConstructor
@Setter
public class RedisCaptchaServiceImpl implements CaptchaCacheService {
@Resource // 保证 aj-captcha SPI 创建时的注入
private StringRedisTemplate stringRedisTemplate;
@Override
@ -25,10 +21,6 @@ public class RedisCaptchaServiceImpl implements CaptchaCacheService {
return "redis";
}
public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
this.stringRedisTemplate = stringRedisTemplate;
}
@Override
public void set(String key, String value, long expiresInSeconds) {
stringRedisTemplate.opsForValue().set(key, value, expiresInSeconds, TimeUnit.SECONDS);

View File

@ -0,0 +1,8 @@
/**
* 验证码拓展
*
* 基于 aj-captcha 实现滑块验证码文档https://ajcaptcha.beliefteam.cn/captcha-doc/
*
* @author 星语
*/
package cn.iocoder.yudao.module.system.framework.captcha;

View File

@ -0,0 +1 @@
cn.iocoder.yudao.module.system.framework.captcha.core.RedisCaptchaServiceImpl

View File

@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.system.service.auth;
import cn.hutool.core.util.ReflectUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.system.api.sms.SmsCodeApi;
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
@ -32,6 +33,7 @@ import jakarta.validation.Validation;
import jakarta.validation.Validator;
import static cn.hutool.core.util.RandomUtil.randomEle;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
@ -208,8 +210,15 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest {
public void testSmsLogin_success() {
// 准备参数
String mobile = randomString();
String scene = randomString();
AuthSmsLoginReqVO reqVO = new AuthSmsLoginReqVO(mobile, scene);
String code = randomString();
AuthSmsLoginReqVO reqVO = new AuthSmsLoginReqVO(mobile, code);
// mock 方法校验验证码
when(smsCodeApi.useSmsCode(argThat(reqDTO -> {
assertEquals(mobile, reqDTO.getMobile());
assertEquals(code, reqDTO.getCode());
assertEquals(SmsSceneEnum.ADMIN_MEMBER_LOGIN.getScene(), reqDTO.getScene());
return true;
}))).thenReturn(success(true));
// mock 方法用户信息
AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setId(1L));
when(userService.getUserByMobile(eq(mobile))).thenReturn(user);