🐛 Fixing a bug. 序列化问题 修正

This commit is contained in:
冷冷 committed 2019-03-25 21:34:33 +08:00
1 parent 3832226a8c
commit 0da6ef25e2
7 files changed
+29 -24

No files matched your search

@@ -58,8 +58,8 @@ import java.util.Map;
@RequestMapping("/token")
public class PigxTokenEndpoint {
private static final String PIGX_OAUTH_ACCESS = SecurityConstants.PIGX_PREFIX + SecurityConstants.OAUTH_PREFIX + "auth_to_access:";
private final RedisTemplate pigxRedisTemplate;
private final TokenStore tokenStore;
private final RedisTemplate redisTemplate;
private final CacheManager cacheManager;
/**
@@ -131,19 +131,19 @@ public class PigxTokenEndpoint {
List<String> pages = findKeysForPage(key, MapUtil.getInt(params, PaginationConstants.CURRENT)
, MapUtil.getInt(params, PaginationConstants.SIZE));
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
pigxRedisTemplate.setKeySerializer(new StringRedisSerializer());
pigxRedisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
Page result = new Page(MapUtil.getInt(params, PaginationConstants.CURRENT), MapUtil.getInt(params, PaginationConstants.SIZE));
result.setRecords(redisTemplate.opsForValue().multiGet(pages));
result.setTotal(Long.valueOf(redisTemplate.keys(key).size()));
result.setRecords(pigxRedisTemplate.opsForValue().multiGet(pages));
result.setTotal(Long.valueOf(pigxRedisTemplate.keys(key).size()));
return new R<>(result);
}
private List<String> findKeysForPage(String patternKey, int pageNum, int pageSize) {
ScanOptions options = ScanOptions.scanOptions().match(patternKey).build();
RedisSerializer<String> redisSerializer = (RedisSerializer<String>) redisTemplate.getKeySerializer();
Cursor cursor = (Cursor) redisTemplate.executeWithStickyConnection(redisConnection -> new ConvertingCursor<>(redisConnection.scan(options), redisSerializer::deserialize));
RedisSerializer<String> redisSerializer = (RedisSerializer<String>) pigxRedisTemplate.getKeySerializer();
Cursor cursor = (Cursor) pigxRedisTemplate.executeWithStickyConnection(redisConnection -> new ConvertingCursor<>(redisConnection.scan(options), redisSerializer::deserialize));
List<String> result = new ArrayList<>();
int tmpIndex = 0;
int startIndex = (pageNum - 1) * pageSize;
@@ -33,6 +33,7 @@ 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.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
@@ -125,6 +126,8 @@ public class ValidateCodeGatewayFilter extends AbstractGatewayFilterFactory {
}
String key = CommonConstants.DEFAULT_CODE_KEY + randomStr;
redisTemplate.setKeySerializer(new StringRedisSerializer());
if (!redisTemplate.hasKey(key)) {
throw new ValidateCodeException("验证码不合法");
}
@@ -24,6 +24,7 @@ import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
@@ -60,6 +61,7 @@ public class ImageCodeHandler implements HandlerFunction<ServerResponse> {
//保存验证码信息
String randomStr = serverRequest.queryParam("randomStr").get();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.opsForValue().set(CommonConstants.DEFAULT_CODE_KEY + randomStr, text
, SecurityConstants.CODE_TIME, TimeUnit.SECONDS);
@@ -50,14 +50,14 @@ import java.net.URI;
@Configuration
@AllArgsConstructor
public class DynamicRouteInitRunner {
private final RedisTemplate redisTemplate;
private final RedisTemplate pigxRedisTemplate;
private final SysRouteConfService routeConfService;
@Async
@Order
@EventListener({WebServerInitializedEvent.class, DynamicRouteInitEvent.class})
public void initRoute() {
Boolean result = redisTemplate.delete(CommonConstants.ROUTE_KEY);
Boolean result = pigxRedisTemplate.delete(CommonConstants.ROUTE_KEY);
log.info("初始化网关路由 {} ", result);
routeConfService.routes().forEach(route -> {
@@ -73,8 +73,8 @@ public class DynamicRouteInitRunner {
vo.setPredicates(predicateObj.toList(PredicateDefinition.class));
log.info("加载路由ID:{},{}", route.getRouteId(), vo);
redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(RouteDefinitionVo.class));
redisTemplate.opsForHash().put(CommonConstants.ROUTE_KEY, route.getRouteId(), vo);
pigxRedisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(RouteDefinitionVo.class));
pigxRedisTemplate.opsForHash().put(CommonConstants.ROUTE_KEY, route.getRouteId(), vo);
});
log.debug("初始化网关路由结束 ");
}
@@ -45,7 +45,7 @@ import java.util.concurrent.TimeUnit;
@Service
@AllArgsConstructor
public class MobileServiceImpl implements MobileService {
private final RedisTemplate redisTemplate;
private final RedisTemplate pigxRedisTemplate;
private final SysUserMapper userMapper;
@@ -67,7 +67,7 @@ public class MobileServiceImpl implements MobileService {
return new R<>(Boolean.FALSE, "手机号未注册");
}
Object codeObj = redisTemplate.opsForValue().get(CommonConstants.DEFAULT_CODE_KEY + mobile);
Object codeObj = pigxRedisTemplate.opsForValue().get(CommonConstants.DEFAULT_CODE_KEY + mobile);
if (codeObj != null) {
log.info("手机号验证码未过期:{},{}", mobile, codeObj);
@@ -76,7 +76,7 @@ public class MobileServiceImpl implements MobileService {
String code = RandomUtil.randomNumbers(Integer.parseInt(SecurityConstants.CODE_SIZE));
log.debug("手机号生成验证码成功:{},{}", mobile, code);
redisTemplate.opsForValue().set(
pigxRedisTemplate.opsForValue().set(
CommonConstants.DEFAULT_CODE_KEY + LoginTypeEnum.SMS.getType() + "@" + mobile
, code, SecurityConstants.CODE_TIME, TimeUnit.SECONDS);
return new R<>(Boolean.TRUE, code);
@@ -55,7 +55,7 @@ import java.util.stream.Collectors;
@AllArgsConstructor
@Service("sysRouteConfService")
public class SysRouteConfServiceImpl extends ServiceImpl<SysRouteConfMapper, SysRouteConf> implements SysRouteConfService {
private final RedisTemplate redisTemplate;
private final RedisTemplate pigxRedisTemplate;
private final ApplicationEventPublisher applicationEventPublisher;
@@ -82,7 +82,7 @@ public class SysRouteConfServiceImpl extends ServiceImpl<SysRouteConfMapper, Sys
@Transactional(rollbackFor = Exception.class)
public Mono<Void> updateRoutes(JSONArray routes) {
// 清空Redis 缓存
Boolean result = redisTemplate.delete(CommonConstants.ROUTE_KEY);
Boolean result = pigxRedisTemplate.delete(CommonConstants.ROUTE_KEY);
log.info("清空网关路由 {} ", result);
// 遍历修改的routes,保存到Redis
@@ -128,8 +128,8 @@ public class SysRouteConfServiceImpl extends ServiceImpl<SysRouteConfMapper, Sys
vo.setOrder(Integer.parseInt(String.valueOf(order)));
}
redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(RouteDefinitionVo.class));
redisTemplate.opsForHash().put(CommonConstants.ROUTE_KEY, vo.getId(), vo);
pigxRedisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(RouteDefinitionVo.class));
pigxRedisTemplate.opsForHash().put(CommonConstants.ROUTE_KEY, vo.getId(), vo);
routeDefinitionVoList.add(vo);
});
@@ -20,8 +20,8 @@ package com.pig4cloud.pigx.monitor.support;
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
import de.codecentric.boot.admin.server.eventstore.InMemoryEventStore;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
@@ -39,18 +39,18 @@ import java.util.List;
*/
@Slf4j
@Configuration
@AllArgsConstructor
public class RedisEventStore extends InMemoryEventStore {
@Autowired
private RedisTemplate redisTemplate;
private final RedisTemplate pigxRedisTemplate;
@Override
public Mono<Void> append(List<InstanceEvent> events) {
events.forEach(event -> {
String key = event.getInstance().getValue() + "_" + event.getTimestamp().toString();
log.info("保存实例事件的KEY:{},EVENT: {}", key, event.getType());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(InstanceEvent.class));
redisTemplate.opsForHash().put(CommonConstants.EVENT_KEY, key, event);
pigxRedisTemplate.setKeySerializer(new StringRedisSerializer());
pigxRedisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(InstanceEvent.class));
pigxRedisTemplate.opsForHash().put(CommonConstants.EVENT_KEY, key, event);
});
return super.append(events);
}