Skip to content

Commit 0c92eb9

Browse files
committed
style(admin): uni-prefix of captcha key
1 parent 2fca86f commit 0c92eb9

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

blog-admin/src/main/java/com/hackyle/blog/admin/infrastructure/redis/CacheKey.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ public class CacheKey {
1414
public static class Auth {
1515
private static final String PREFIX = UNI_PREFIX + "auth:";
1616

17+
//每次生成验证码的缓存前缀
18+
public static final String CAPTCHA = PREFIX + "cha:";
1719
//登录者用户id
18-
public static final String LOGIN_UID = PREFIX + "uid";
20+
public static final String LOGIN_UID = PREFIX + "uid:";
1921

2022
}
2123

blog-admin/src/main/java/com/hackyle/blog/admin/module/auth/service/impl/AuthServiceImpl.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import cn.hutool.core.util.StrUtil;
44
import com.google.code.kaptcha.impl.DefaultKaptcha;
55
import com.hackyle.blog.admin.infrastructure.holder.AuthedContextHolder;
6+
import com.hackyle.blog.admin.infrastructure.redis.CacheKey;
67
import com.hackyle.blog.admin.module.auth.model.dto.LoginDto;
78
import com.hackyle.blog.admin.module.auth.model.dto.UserDetailsDto;
89
import com.hackyle.blog.admin.module.auth.model.vo.CaptchaVo;
@@ -50,8 +51,9 @@ public class AuthServiceImpl implements AuthService {
5051

5152
@Override
5253
public LoginVo login(LoginDto loginDto) {
53-
String cacheCode = (String) redisTemplate.opsForValue().get(loginDto.getUuid());
54-
redisTemplate.delete(loginDto.getUuid()); // 清除验证码
54+
String key = CacheKey.Auth.CAPTCHA+loginDto.getUuid();
55+
String cacheCode = (String) redisTemplate.opsForValue().get(key);
56+
redisTemplate.delete(key); // 清除验证码
5557
if (StringUtils.isBlank(cacheCode)) {
5658
throw new IllegalArgumentException("验证码不存在或已过期");
5759
}
@@ -109,7 +111,7 @@ public CaptchaVo captcha() {
109111
String uuid = UUID.randomUUID().toString();
110112
captchaVo.setUuid(uuid);
111113
captchaVo.setCode("data:image/png;base64," + base64Code);
112-
redisTemplate.opsForValue().set(uuid, kaptchaText, 60L, TimeUnit.SECONDS);
114+
redisTemplate.opsForValue().set(CacheKey.Auth.CAPTCHA+uuid, kaptchaText, 60L, TimeUnit.SECONDS);
113115

114116
return captchaVo;
115117
}

0 commit comments

Comments
 (0)