Commit 8d1af00c authored by 李健华's avatar 李健华

Merge branch 'OFRK-312' into 'release'

Ofrk 312

See merge request !43
parents f7cf28f2 35df8670
...@@ -81,10 +81,10 @@ public class PasswordFreeAccessValidateAdvisor { ...@@ -81,10 +81,10 @@ public class PasswordFreeAccessValidateAdvisor {
} }
// 当前请求的Token // 当前请求的Token
String token = request.getHeader(Constants.X_AUTH_TOKEN); String token = request.getHeader(Constants.X_AUTH_TOKEN);
if (StringUtils.length(token) != Constants.TOKEN_LENGTH) { // if (StringUtils.length(token) != Constants.TOKEN_LENGTH) {
log.info("非法请求 - 无效token, token={}, phoneNo={}, userId={}, clientIp={}", token, phoneNo, userId, clientIp); // log.info("非法请求 - 无效token, token={}, phoneNo={}, userId={}, clientIp={}", token, phoneNo, userId, clientIp);
return false; // return false;
} // }
// 当前session // 当前session
SessionStruct session = XyqbSessionContextHolder.getXSessionFromRedis(token); SessionStruct session = XyqbSessionContextHolder.getXSessionFromRedis(token);
......
...@@ -15,6 +15,8 @@ import cn.quantgroup.xyqb.exception.UserNotExistException; ...@@ -15,6 +15,8 @@ import cn.quantgroup.xyqb.exception.UserNotExistException;
import cn.quantgroup.xyqb.exception.VerificationCodeErrorException; import cn.quantgroup.xyqb.exception.VerificationCodeErrorException;
import cn.quantgroup.xyqb.model.*; import cn.quantgroup.xyqb.model.*;
import cn.quantgroup.xyqb.model.session.SessionStruct; import cn.quantgroup.xyqb.model.session.SessionStruct;
import cn.quantgroup.xyqb.model.session.SessionValue;
import cn.quantgroup.xyqb.service.http.IHttpService;
import cn.quantgroup.xyqb.service.merchant.IMerchantService; import cn.quantgroup.xyqb.service.merchant.IMerchantService;
import cn.quantgroup.xyqb.service.register.IUserRegisterService; import cn.quantgroup.xyqb.service.register.IUserRegisterService;
import cn.quantgroup.xyqb.service.session.ISessionService; import cn.quantgroup.xyqb.service.session.ISessionService;
...@@ -27,12 +29,15 @@ import cn.quantgroup.xyqb.util.PasswordUtil; ...@@ -27,12 +29,15 @@ import cn.quantgroup.xyqb.util.PasswordUtil;
import cn.quantgroup.xyqb.util.TenantUtil; import cn.quantgroup.xyqb.util.TenantUtil;
import cn.quantgroup.xyqb.util.ValidationUtil; import cn.quantgroup.xyqb.util.ValidationUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
...@@ -82,6 +87,15 @@ public class UserController implements IBaseController { ...@@ -82,6 +87,15 @@ public class UserController implements IBaseController {
@Autowired @Autowired
private ITenantService tenantService; private ITenantService tenantService;
@Value("${xyqb.user.service.host}")
private String userHost;
@Value("${token.prefix}")
private String prefix;
@Autowired
private IHttpService httpService;
/** /**
* 登录(账号 + 密码) * 登录(账号 + 密码)
...@@ -432,6 +446,11 @@ public class UserController implements IBaseController { ...@@ -432,6 +446,11 @@ public class UserController implements IBaseController {
String tokenKey2 = Constants.Session.USER_SESSION_CACHE + token; String tokenKey2 = Constants.Session.USER_SESSION_CACHE + token;
// 判断token是否存在 // 判断token是否存在
boolean exist = stringRedisTemplate.hasKey(tokenKey) || stringRedisTemplate.hasKey(tokenKey2); boolean exist = stringRedisTemplate.hasKey(tokenKey) || stringRedisTemplate.hasKey(tokenKey2);
if (!token.contains(prefix) && !exist) {
Integer tenantId = TenantUtil.TENANT_DEFAULT;
findTokenExchange(token, tenantId);
exist = stringRedisTemplate.hasKey(tokenKey) || stringRedisTemplate.hasKey(tokenKey2);
}
return JsonResult.buildSuccessResult("token valid", exist); return JsonResult.buildSuccessResult("token valid", exist);
} }
...@@ -453,7 +472,16 @@ public class UserController implements IBaseController { ...@@ -453,7 +472,16 @@ public class UserController implements IBaseController {
} }
SessionStruct sessionStruct = XyqbSessionContextHolder.getXSessionFromRedis(token); SessionStruct sessionStruct = XyqbSessionContextHolder.getXSessionFromRedis(token);
if (sessionStruct == null || sessionStruct.getValues() == null) { if (sessionStruct == null || sessionStruct.getValues() == null) {
return JsonResult.buildSuccessResult(null, result); // 使用token去电商查询信息, 如果token 不包含本系统token前缀
if (!token.contains(prefix)) {
findTokenExchange(token, tenantId);
sessionStruct = XyqbSessionContextHolder.getXSessionFromRedis(token);
if (sessionStruct == null || sessionStruct.getValues() == null) {
return JsonResult.buildSuccessResult(null, result);
}
} else {
return JsonResult.buildSuccessResult(null, result);
}
} }
User user = sessionStruct.getValues().getUser(); User user = sessionStruct.getValues().getUser();
Integer sessionTenantId = sessionStruct.getValues().getLoginProperties().getTenantId(); Integer sessionTenantId = sessionStruct.getValues().getLoginProperties().getTenantId();
...@@ -476,9 +504,78 @@ public class UserController implements IBaseController { ...@@ -476,9 +504,78 @@ public class UserController implements IBaseController {
result.put("phoneNo", phoneNo); result.put("phoneNo", phoneNo);
result.put("userId", user.getId()); result.put("userId", user.getId());
result.put("exist", true); result.put("exist", true);
result.put("uuid", user.getUuid());
return JsonResult.buildSuccessResult(null, result); return JsonResult.buildSuccessResult(null, result);
} }
/**
* token 交换
*
*/
@RequestMapping("/tokenExchange")
public JsonResult tokenExchange(@RequestParam String token, @RequestParam(required = false) Integer tenantId) {
TokenExchange tokenExchange = new TokenExchange();
if (StringUtils.isEmpty(token)) {
return JsonResult.buildSuccessResult(null, tokenExchange);
}
if (token.contains(Constants.TOKEN_MASTER)) {
return JsonResult.buildSuccessResult(null, tokenExchange);
}
SessionStruct sessionStruct = XyqbSessionContextHolder.getXSessionFromRedis(token);
if (sessionStruct == null || sessionStruct.getValues() == null) {
return JsonResult.buildSuccessResult(null, tokenExchange);
}
User user = sessionStruct.getValues().getUser();
tokenExchange.setLoginProperties(JSONObject.toJSONString(sessionStruct.getValues().getLoginProperties()));
tokenExchange.setUserId(user.getId());
tokenExchange.setPhoneNo(user.getPhoneNo());
tokenExchange.setUuid(user.getUuid());
tokenExchange.setExpire(sessionStruct.getExpire());
return JsonResult.buildSuccessResult(null, tokenExchange);
}
/**
* 交换token
* @param token
* @return
*/
private void findTokenExchange(String token, Integer tenantId) {
if (tenantId == null) {
tenantId = TenantUtil.TENANT_DEFAULT;
}
// 请求其他系统信息
HashMap<String, String> parameters = new HashMap<>();
HashMap<String, String> headers = new HashMap<>();
headers.put("qg-tenant-id", tenantId.toString());
parameters.put("token", token);
try {
String resultStr = httpService.post(userHost + "/api/finance-gateway/finance-user/finance/tokenExchange", headers, parameters);
JsonResult<TokenExchange> tokenExchangeResult = JSONObject.parseObject(resultStr, new TypeReference<JsonResult<TokenExchange>>(){});
// 如果有返回值
TokenExchange tokenExchange = tokenExchangeResult.getData();
if (tokenExchange != null) {
SessionValue sessionValue = new SessionValue();
User user = userService.findById(tokenExchange.getUserId());
if (user != null) {
LoginProperties loginProperties = JSONObject.parseObject(tokenExchange.getLoginProperties(), LoginProperties.class);
// 根据返回值生成token, 返回值包含user:session:token的值
sessionValue.setUser(user);
sessionValue.setLoginProperties(loginProperties);
sessionService.persistSessionExchange(token, sessionValue, tokenExchange.getExpire());
}
}
} catch (Exception e) {
e.printStackTrace();
log.error("请求金融卡包网关出错--{}", e.getMessage());
}
}
/** /**
* 用户中心首页,显示用户头像、昵称、姓名 * 用户中心首页,显示用户头像、昵称、姓名
* *
......
package cn.quantgroup.xyqb.model;
import lombok.*;
import java.io.Serializable;
/**
* @author mengfan.feng
* @time 2015-10-27 16:15
*/
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class TokenExchange implements Serializable {
private static final long serialVersionUID = -1L;
private Long userId;
private String uuid;
private String phoneNo;
private String loginProperties;
private Long expire = 15 * 24 * 60 * 60L;
}
...@@ -16,6 +16,7 @@ public class SessionStruct implements Serializable { ...@@ -16,6 +16,7 @@ public class SessionStruct implements Serializable {
private static final long serialVersionUID = -1L; private static final long serialVersionUID = -1L;
private String sid; private String sid;
private SessionValue values; private SessionValue values;
private long expire;
public void setAttribute(String key, String value) { public void setAttribute(String key, String value) {
if (value == null) { if (value == null) {
......
...@@ -42,5 +42,7 @@ public interface ISessionService { ...@@ -42,5 +42,7 @@ public interface ISessionService {
*/ */
void deleteSession(String token); void deleteSession(String token);
void persistSessionExchange(String token, SessionValue sessionValue, long expire);
void kdspDeleteSession(Long userId, LoginProperties loginProperties); void kdspDeleteSession(Long userId, LoginProperties loginProperties);
} }
...@@ -14,6 +14,7 @@ import cn.quantgroup.xyqb.util.TenantUtil; ...@@ -14,6 +14,7 @@ import cn.quantgroup.xyqb.util.TenantUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching; import org.springframework.cache.annotation.Caching;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
...@@ -37,6 +38,9 @@ public class SessionServiceImpl implements ISessionService { ...@@ -37,6 +38,9 @@ public class SessionServiceImpl implements ISessionService {
@Resource @Resource
private RedisTemplate<String, String> stringRedisTemplate; private RedisTemplate<String, String> stringRedisTemplate;
@Value("${token.prefix}")
private String prefix;
/** /**
* 更新session * 更新session
* 用户信息存在,更新session中的最后访问时间,重新写入缓存. * 用户信息存在,更新session中的最后访问时间,重新写入缓存.
...@@ -78,7 +82,7 @@ public class SessionServiceImpl implements ISessionService { ...@@ -78,7 +82,7 @@ public class SessionServiceImpl implements ISessionService {
SessionStruct sessionStruct; SessionStruct sessionStruct;
//获取sessionid //获取sessionid
String sessionId = findSessionIdByUserIdLoginProperties(user.getId(), properties); String sessionId = findSessionIdByUserIdLoginProperties(user.getId(), properties);
if (StringUtils.length(sessionId) == Constants.TOKEN_LENGTH) { if (StringUtils.length(sessionId) > 0) {
sessionStruct = findSessionBySessionId(sessionId); sessionStruct = findSessionBySessionId(sessionId);
if (sessionStruct == null) { if (sessionStruct == null) {
sessionStruct = newSession(user, properties); sessionStruct = newSession(user, properties);
...@@ -123,7 +127,7 @@ public class SessionServiceImpl implements ISessionService { ...@@ -123,7 +127,7 @@ public class SessionServiceImpl implements ISessionService {
Timestamp now = new Timestamp(System.currentTimeMillis()); Timestamp now = new Timestamp(System.currentTimeMillis());
SessionStruct sessionStruct = new SessionStruct(); SessionStruct sessionStruct = new SessionStruct();
SessionValue sessionValue = new SessionValue(); SessionValue sessionValue = new SessionValue();
sessionStruct.setSid(UUID.randomUUID().toString()); sessionStruct.setSid(prefix + UUID.randomUUID().toString());
sessionValue.setCreatedAt(now); sessionValue.setCreatedAt(now);
sessionValue.setLastAccessTime(now); sessionValue.setLastAccessTime(now);
sessionValue.setUser(user); sessionValue.setUser(user);
...@@ -283,6 +287,18 @@ public class SessionServiceImpl implements ISessionService { ...@@ -283,6 +287,18 @@ public class SessionServiceImpl implements ISessionService {
} }
@Override @Override
public void persistSessionExchange(String token, SessionValue sessionValue, long expire) {
Timestamp current = new Timestamp(System.currentTimeMillis());
sessionValue.setLastAccessTime(current);
String json = JSON.toJSONString(sessionValue);
stringRedisTemplate.opsForValue().set(Constants.Session.USER_SESSION_CACHE + token, json,
Constants.Session.SESSION_VALID_TIME, TimeUnit.SECONDS);
String key = generateLoginPropertiesKey(sessionValue.getUser().getId(), sessionValue.getLoginProperties());
stringRedisTemplate.opsForValue().set(key, token, expire, TimeUnit.SECONDS);
setUserIdTokenKeys(sessionValue.getUser().getId(), key);
}
public void kdspDeleteSession(Long userId, LoginProperties loginProperties) { public void kdspDeleteSession(Long userId, LoginProperties loginProperties) {
List<Long> createFromList = Arrays.asList(214L, 217L); List<Long> createFromList = Arrays.asList(214L, 217L);
for (Long createFrom : createFromList) { for (Long createFrom : createFromList) {
...@@ -294,6 +310,7 @@ public class SessionServiceImpl implements ISessionService { ...@@ -294,6 +310,7 @@ public class SessionServiceImpl implements ISessionService {
} }
/** /**
* 获取用户的会话缓存Set的Redis-Key * 获取用户的会话缓存Set的Redis-Key
* *
......
...@@ -52,7 +52,7 @@ public class XyqbSessionContextHolder { ...@@ -52,7 +52,7 @@ public class XyqbSessionContextHolder {
} }
public static SessionStruct getXSessionFromRedis(String token) { public static SessionStruct getXSessionFromRedis(String token) {
if (StringUtils.length(token) != Constants.TOKEN_LENGTH || Objects.isNull(redisTemplate)) { if ( Objects.isNull(redisTemplate)) {
return null; return null;
} }
String result = redisTemplate.opsForValue().get(Constants.Session.USER_SESSION_CACHE + token); String result = redisTemplate.opsForValue().get(Constants.Session.USER_SESSION_CACHE + token);
...@@ -69,6 +69,7 @@ public class XyqbSessionContextHolder { ...@@ -69,6 +69,7 @@ public class XyqbSessionContextHolder {
SessionStruct sessionStruct = new SessionStruct(); SessionStruct sessionStruct = new SessionStruct();
sessionStruct.setSid(token); sessionStruct.setSid(token);
sessionStruct.setValues(values); sessionStruct.setValues(values);
sessionStruct.setExpire(redisTemplate.getExpire(Constants.Session.USER_SESSION_CACHE + token));
return sessionStruct; return sessionStruct;
} catch (Exception e) { } catch (Exception e) {
log.warn("[XyqbSessionContextHolder][getXSessionFromRedis] 序列化SessionValue出错:sid:{},sessionValue:{}", token, result, e); log.warn("[XyqbSessionContextHolder][getXSessionFromRedis] 序列化SessionValue出错:sid:{},sessionValue:{}", token, result, e);
......
...@@ -36,6 +36,8 @@ public class IpUtil { ...@@ -36,6 +36,8 @@ public class IpUtil {
static { static {
String[] ips = {"10.", "172.", "192.168.3.", "192.168.4."}; String[] ips = {"10.", "172.", "192.168.3.", "192.168.4."};
WHITE_ADDRESS.addAll(Arrays.asList(ips)); WHITE_ADDRESS.addAll(Arrays.asList(ips));
WHITE_ADDRESS.add("123.56.31.54"); // 天津自牧生产环境
WHITE_ADDRESS.add("47.93.61.134"); // 天津自牧生产环境
//系统环境 //系统环境
if (!TechEnvironment.isPro()) { if (!TechEnvironment.isPro()) {
WHITE_ADDRESS.add("192.168."); WHITE_ADDRESS.add("192.168.");
...@@ -43,6 +45,8 @@ public class IpUtil { ...@@ -43,6 +45,8 @@ public class IpUtil {
WHITE_ADDRESS.add("10."); WHITE_ADDRESS.add("10.");
//劲松的测试环境 //劲松的测试环境
WHITE_ADDRESS.add("54.223.247.69"); WHITE_ADDRESS.add("54.223.247.69");
WHITE_ADDRESS.add("123.56.159.197"); // 天津自牧预发布环境
WHITE_ADDRESS.add("182.92.71.41"); // 天津自牧测试环境
WHITE_ADDRESS.add(LOCAL_ADDRESS); WHITE_ADDRESS.add(LOCAL_ADDRESS);
} }
} }
......
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