Commit 44d42ef9 by weisong

add ZhuanjiaUser to ThreadLocal

parent 0544e970
......@@ -10,6 +10,7 @@ import com.cnooc.expert.common.utils.ValidUtils;
import com.cnooc.expert.external.expert.auth.service.LoginServicesClient;
import com.cnooc.expert.external.expert.model.response.ExpertInfoAppResp;
import com.cnooc.expert.external.expert.model.response.ExpertInfoResp;
import com.cnooc.expert.system.entity.pojo.ZhuanJiaUser;
import com.cnooc.expert.system.entity.vo.LoginVO;
import com.cnooc.expert.system.entity.vo.VerifyCodeVO;
import com.cnooc.expert.auth.service.LoginService;
......@@ -17,12 +18,14 @@ import com.cnooc.expert.auth.service.SmsService;
import com.cnooc.expert.auth.service.SysCaptchaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.security.crypto.password.PasswordEncoder;
import java.time.Duration;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
* @Author: songYuHang
......@@ -33,7 +36,7 @@ import java.util.UUID;
public class LoginServiceImpl implements LoginService {
@Autowired
private StringRedisTemplate redisTemplate;
private RedisTemplate<String, ZhuanJiaUser> redisTemplate;
@Autowired
private SmsService smsService;
......@@ -174,14 +177,16 @@ public class LoginServiceImpl implements LoginService {
if (!flag) {
// 登录日志
//sysLogLoginService.savePortal(login.getAccountName(), Constant.FAIL, LoginOperationEnum.ACCOUNT_FAIL.getValue(), 1);
throw new IllegalArgumentException("密码错误");
throw new BusinessException("密码错误");
}
//3.生成相应的uuid作为redis的key
// // todo userid
String uuidKey = UUID.randomUUID().toString();
String token = JwtUtils.createToken(1,uuidKey);
tokenSetRedis(token, uuidKey);
ZhuanJiaUser zhuanJiaUser = convert2ZhuanjiaUser( expertInfoResp );
redisTemplate.opsForValue().set(TokenConstants.LOGIN_USER_KEY_ + expertInfoResp.getBaseGuid(), zhuanJiaUser, 48, TimeUnit.HOURS);
String token = JwtUtils.createToken(expertInfoResp.getBaseGuid(),uuidKey);
//6.返回token
return token;
}
......@@ -208,20 +213,39 @@ public class LoginServiceImpl implements LoginService {
throw new IllegalArgumentException("手机验证码错误");
}
//3.生成相应的uuid作为redis的key
// todo userid
String uuidKey = UUID.randomUUID().toString();
String token = JwtUtils.createToken(1,uuidKey);
tokenSetRedis(token, uuidKey);
ZhuanJiaUser zhuanJiaUser = convert2ZhuanjiaUser( expertInfoResp );
redisTemplate.opsForValue().set(TokenConstants.LOGIN_USER_KEY_ + expertInfoResp.getBaseGuid(), zhuanJiaUser, 48, TimeUnit.HOURS);
String token = JwtUtils.createToken(expertInfoResp.getBaseGuid(),uuidKey);
return token;
}
/**
* 缓存存入token
* @param token token
* @param uuidKey uuidKey
*/
private void tokenSetRedis(String token, String uuidKey) {
String redisTokenKey = TokenConstants.TOKEN_KEY_ + uuidKey;
redisTemplate.opsForValue().set(redisTokenKey, token, Duration.ofHours(24));
}
// /**
// * 缓存存入token
// * @param token token
// * @param uuidKey uuidKey
// */
// private void tokenSetRedis(String token, String uuidKey) {
// String redisTokenKey = TokenConstants.TOKEN_KEY_ + uuidKey;
// redisTemplate.opsForValue().set(redisTokenKey, token, Duration.ofHours(24));
// }
private ZhuanJiaUser convert2ZhuanjiaUser(ExpertInfoResp expertInfoResp){
if( expertInfoResp == null ){
return null;
}
ZhuanJiaUser zhuanJiaUser = new ZhuanJiaUser();
// TODO weisong
return zhuanJiaUser;
}
}
......@@ -33,4 +33,8 @@ public class TokenConstants
*/
public static final String TOKEN_KEY_ = "TOKEN:TOKEN_KEY_";
/**
* 用于登录用户信息的key
*/
public static final String LOGIN_USER_KEY_ = "LOGIN:USER_KEY_";
}
......@@ -7,8 +7,10 @@ import com.cnooc.expert.common.exception.GlobalErrorCodeConstants;
import com.cnooc.expert.common.utils.JwtUtils;
import com.cnooc.expert.common.utils.UserUtils;
import com.cnooc.expert.common.utils.ValidUtils;
import com.cnooc.expert.system.entity.pojo.ZhuanJiaUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
......@@ -27,7 +29,7 @@ import java.util.Map;
public class LoginInterceptor implements HandlerInterceptor {
@Autowired
private StringRedisTemplate redisTemplate;
private RedisTemplate<String, ZhuanJiaUser> redisTemplate;
@Override
......@@ -43,16 +45,13 @@ public class LoginInterceptor implements HandlerInterceptor {
String uuidKey = userMap.get(TokenConstants.UUID_KEY);
ValidUtils.isNotNull(uuidKey, "登录异常,请重新登录");
// String cachedToken = redisTemplate.opsForValue().get(TokenConstants.TOKEN_KEY_ + userId);
//
// if (cachedToken == null || !cachedToken.equals(token)) {
// // token不存在或不匹配,说明已退出登录或token失效
// response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
// response.getWriter().write("{\"code\":401,\"msg\":\"请先登录\"}");
// return false;
// }
// todo 通过token解析出用户id,代用UserUtils.setUserId(userId)方法进行存储
ZhuanJiaUser zhuanjiaUser = redisTemplate.opsForValue().get(TokenConstants.LOGIN_USER_KEY_ + userId);
if (zhuanjiaUser==null ){
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.getWriter().write("{\"httpCode\":401,\"message\":\"请先登录\"}");
return false;
}
UserUtils.setUserId(zhuanjiaUser);
// 所有条件都满足,放行请求
return true;
......
......@@ -28,7 +28,7 @@ public class JwtUtils {
* 生成token
* @return token字符串
*/
public static String createToken(Integer userId , String uuidKey) {
public static String createToken(String userId , String uuidKey) {
// 生成token
return Jwts.builder()
.setExpiration(new Date(System.currentTimeMillis() + EXPIRATION_TIME))
......@@ -63,7 +63,7 @@ public class JwtUtils {
public static void main(String[] args) throws Exception{
String portalToken = JwtUtils.createToken(111, "testtest");
String portalToken = JwtUtils.createToken("111", "testtest");
Thread.sleep(5000);
Map<String, String> claims = JwtUtils.getTokenInfo(portalToken);
System.out.print(JSON.toJSONString(claims, true));
......
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