♻️ Refactoring code. 重构Redis<> 序列化方式

This commit is contained in:
lbw committed 2023-12-03 23:44:09 +08:00
1 parent 1bfc0e728a
commit 29019e0672
4 files changed
+8 -15

No files matched your search

@@ -6,7 +6,6 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @author lengleng
@@ -22,7 +21,6 @@ public class DynamicRouteHealthIndicator extends AbstractHealthIndicator {
@Override
protected void doHealthCheck(Health.Builder builder) {
redisTemplate.setKeySerializer(new StringRedisSerializer());
if (redisTemplate.hasKey(CacheConstants.ROUTE_KEY)) {
builder.up();
}
@@ -38,7 +38,6 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
@@ -57,9 +56,9 @@ import reactor.core.publisher.Mono;
@SuppressWarnings("all")
public class ValidateCodeGatewayFilter extends AbstractGatewayFilterFactory {
private final ObjectMapper objectMapper;
private final RedisTemplate<String, String> redisTemplate;
private final RedisTemplate redisTemplate;
private final ObjectMapper objectMapper;
@Override
public GatewayFilter apply(Object config) {
@@ -127,15 +126,14 @@ public class ValidateCodeGatewayFilter extends AbstractGatewayFilterFactory {
String key = String.format("%s:%s:%s", StrUtil.isBlank(tenantId) ? CommonConstants.TENANT_ID_1 : tenantId,
CacheConstants.CLIENT_FLAG, clientId);
redisTemplate.setKeySerializer(new StringRedisSerializer());
Object val = redisTemplate.opsForValue().get(key);
String val = redisTemplate.opsForValue().get(key);
// 当配置不存在时,不用校验
if (val == null) {
return false;
}
JSONObject information = JSONUtil.parseObj(val.toString());
JSONObject information = JSONUtil.parseObj(val);
if (StrUtil.equals(CaptchaFlagTypeEnum.OFF.getType(), information.getStr(CommonConstants.CAPTCHA_FLAG))) {
return false;
}
@@ -175,19 +173,16 @@ public class ValidateCodeGatewayFilter extends AbstractGatewayFilterFactory {
}
String key = CacheConstants.DEFAULT_CODE_KEY + randomStr;
redisTemplate.setKeySerializer(new StringRedisSerializer());
if (!redisTemplate.hasKey(key)) {
throw new ValidateCodeException("验证码不合法");
}
Object codeObj = redisTemplate.opsForValue().get(key);
String saveCode = redisTemplate.opsForValue().get(key);
if (codeObj == null) {
if (saveCode == null) {
throw new ValidateCodeException("验证码不合法");
}
String saveCode = codeObj.toString();
if (StrUtil.isBlank(saveCode)) {
redisTemplate.delete(key);
throw new ValidateCodeException("验证码不合法");
@@ -37,7 +37,7 @@ public class ClientDetailsInitRunner implements InitializingBean {
private final SysTenantService tenantService;
private final RedisTemplate redisTemplate;
private final RedisTemplate<String,String> redisTemplate;
/**
* WebServerInitializedEvent 使用 TransactionalEventListener 时启动时无法获取到事件
@@ -49,7 +49,7 @@ import java.util.concurrent.TimeUnit;
@AllArgsConstructor
public class MobileServiceImpl implements MobileService {
private final RedisTemplate redisTemplate;
private final RedisTemplate<String, String> redisTemplate;
private final SysUserMapper userMapper;