Commit 44d42ef9 by weisong

add ZhuanjiaUser to ThreadLocal

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