Commit c5492616 by weisong

fix redis issue

parent c47604ed
......@@ -41,6 +41,11 @@
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
<version>2.9.0</version>
......
......@@ -23,6 +23,7 @@ import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.security.crypto.password.PasswordEncoder;
import javax.annotation.Resource;
import java.time.Duration;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
......@@ -35,8 +36,8 @@ import java.util.concurrent.TimeUnit;
@Slf4j
public class LoginServiceImpl implements LoginService {
@Autowired
private RedisTemplate<String, ZhuanJiaUser> redisTemplate;
@Resource(name="redisCommonTemplate")
private RedisTemplate<String, Object> redisTemplate;
@Autowired
private SmsService smsService;
......
......@@ -16,6 +16,7 @@ import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
......@@ -28,8 +29,8 @@ import java.util.Map;
@Component
public class LoginInterceptor implements HandlerInterceptor {
@Autowired
private RedisTemplate<String, ZhuanJiaUser> redisTemplate;
@Resource(name="redisCommonTemplate")
private RedisTemplate<String, Object> redisTemplate;
@Override
......@@ -45,7 +46,7 @@ public class LoginInterceptor implements HandlerInterceptor {
String uuidKey = userMap.get(TokenConstants.UUID_KEY);
ValidUtils.isNotNull(uuidKey, "登录异常,请重新登录");
ZhuanJiaUser zhuanjiaUser = redisTemplate.opsForValue().get(TokenConstants.LOGIN_USER_KEY_ + userId);
ZhuanJiaUser zhuanjiaUser = (ZhuanJiaUser)redisTemplate.opsForValue().get(TokenConstants.LOGIN_USER_KEY_ + userId);
if (zhuanjiaUser==null ){
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.getWriter().write("{\"httpCode\":401,\"message\":\"请先登录\"}");
......
package com.cnooc.expert.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean(name="redisCommonTemplate")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
// 使用 StringRedisSerializer 来序列化和反序列化 redis 的 key 值
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
// 使用 GenericJackson2JsonRedisSerializer 来序列化和反序列化 redis 的 value 值
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
template.afterPropertiesSet();
return template;
}
@Bean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment