update:验证码,登录,注册

This commit is contained in:
hrz 2025-03-07 18:37:26 +08:00
parent e2c318a32b
commit 07579d5a45
18 changed files with 889 additions and 752 deletions

0
main/manager-api/README.md Executable file → Normal file
View file

View file

@ -67,6 +67,7 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<!-- 验证码工具包 -->
<dependency> <dependency>
<groupId>com.github.whvcse</groupId> <groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId> <artifactId>easy-captcha</artifactId>

View file

@ -40,4 +40,6 @@ public interface ErrorCode {
int PASSWORD_LENGTH_ERROR = 10030; int PASSWORD_LENGTH_ERROR = 10030;
int PASSWORD_WEAK_ERROR = 10031; int PASSWORD_WEAK_ERROR = 10031;
int DEL_MYSELF_ERROR = 10032; int DEL_MYSELF_ERROR = 10032;
// 验证码错误
int VERIFICATION_CODE = 10033;
} }

View file

@ -0,0 +1,24 @@
package xiaozhi.modules.sys.service;
import jakarta.servlet.http.HttpServletResponse;
/**
* @author zjy
* @since 2025-3-7
*/
public interface CaptchaService {
/**
* 生成验证码
* @param response 响应对象
* @param uuid 验证码唯一id
*/
void create(HttpServletResponse response, String uuid);
/**
* 验证码验证
* @param uuid 验证码唯一id
* @param code 验证码内容
* @return
*/
void validate(String uuid, String code);
}

View file

@ -0,0 +1,11 @@
package xiaozhi.modules.sys.service;
public interface TokenService {
/**
* 生成token
*
* @param userId
* @return
*/
String createToken(long userId);
}

View file

@ -0,0 +1,65 @@
package xiaozhi.modules.sys.service.impl;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import xiaozhi.common.exception.ErrorCode;
import xiaozhi.common.exception.RenException;
import xiaozhi.common.redis.RedisUtils;
import xiaozhi.modules.sys.service.CaptchaService;
import java.io.IOException;
/**
* @author zjy
* @since 2025-3-7
*/
@AllArgsConstructor
@Service("captchaService")
public class CaptchaServiceImpl implements CaptchaService {
private final RedisUtils redisUtils;
@Value("${captcha.expire}")
private Integer captchaExpireTime;
@Override
public void create(HttpServletResponse response, String uuid) {
response.setContentType("image/png");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// 创建验证码对象 宽度、高度、字符长度
SpecCaptcha captcha = new SpecCaptcha(130, 48, 5);
// 默认字符类型
captcha.setCharType(Captcha.TYPE_DEFAULT);
// 缓存验证码
redisUtils.set(uuid,captcha.text(),captchaExpireTime);
// 输出验证码图片
try {
captcha.out(response.getOutputStream());
} catch (IOException e) {
throw new RenException(ErrorCode.VERIFICATION_CODE,"验证码生成错误");
}
}
@Override
public void validate(String uuid, String code) {
// 从缓存中获取验证码
Object storedCode = redisUtils.get(uuid);
//对验证码进行验证过程
if(storedCode == null){
throw new RenException(ErrorCode.VERIFICATION_CODE,"验证码过期,请从新获取");
}else if(code.equalsIgnoreCase(storedCode.toString())){
throw new RenException(ErrorCode.VERIFICATION_CODE,"验证码错误");
}
// 从缓存删除验证码
redisUtils.delete(uuid);
}
}

View file

@ -0,0 +1,31 @@
package xiaozhi.modules.sys.service.impl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import xiaozhi.common.redis.RedisUtils;
import xiaozhi.modules.security.oauth2.TokenGenerator;
import xiaozhi.modules.sys.service.TokenService;
import java.util.Date;
@AllArgsConstructor
@Service
public class TokenServiceImpl implements TokenService {
private final RedisUtils redisUtils;
/**
* 3小时无操作过期
*/
private final static int EXPIRE = 60 * 60 * 3;
@Override
public String createToken(long userId) {
//生成一个token
String token = TokenGenerator.generateValue();
//当前时间
Date now = new Date();
//过期时间
Date expireTime = new Date(now.getTime() + EXPIRE * 1000);
return token;
}
}

View file

@ -15,7 +15,7 @@ spring:
druid: druid:
#MySQL #MySQL
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/xiaozhi_esp32_server?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true url: jdbc:mysql://192.168.110.130:3306/xiaozhi_esp32_server?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
username: root username: root
password: 123456 password: 123456
initial-size: 10 initial-size: 10
@ -47,3 +47,6 @@ logging:
level: level:
org.flowable.engine.impl.persistence.entity.*: debug org.flowable.engine.impl.persistence.entity.*: debug
org.flowable.task.service.impl.persistence.entity.*: debug org.flowable.task.service.impl.persistence.entity.*: debug
# 登录验证码过期时间(秒)
captcha:
expire: 300