Authored by qinchao

session 校验,从uic获取session缓存后存入redis

@@ -33,7 +33,7 @@ import java.util.Arrays; @@ -33,7 +33,7 @@ import java.util.Arrays;
33 import java.util.LinkedList; 33 import java.util.LinkedList;
34 import java.util.List; 34 import java.util.List;
35 import java.util.Map; 35 import java.util.Map;
36 - 36 +import java.util.concurrent.TimeUnit;
37 37
38 38
39 public class SecurityInterceptor implements HandlerInterceptor, ApplicationEventPublisherAware { 39 public class SecurityInterceptor implements HandlerInterceptor, ApplicationEventPublisherAware {
@@ -141,8 +141,9 @@ public class SecurityInterceptor implements HandlerInterceptor, ApplicationEvent @@ -141,8 +141,9 @@ public class SecurityInterceptor implements HandlerInterceptor, ApplicationEvent
141 141
142 //7 从REDIS中获取服务端session的值. 如果REDIS中获取不到,可能存在双中心延迟的情况, 回源数据库查询 142 //7 从REDIS中获取服务端session的值. 如果REDIS中获取不到,可能存在双中心延迟的情况, 回源数据库查询
143 String sessionInfo; 143 String sessionInfo;
  144 + RedisKeyBuilder cacheKey;
144 try { 145 try {
145 - RedisKeyBuilder cacheKey = getSessionCacheKey(jSessionID, clientType, sessionType); 146 + cacheKey = getSessionCacheKey(jSessionID, clientType, sessionType);
146 sessionInfo = valueOperations.get(cacheKey); 147 sessionInfo = valueOperations.get(cacheKey);
147 if(null == sessionInfo){ //如果REDIS主从延迟, 从主REDIS中获取SESSION 148 if(null == sessionInfo){ //如果REDIS主从延迟, 从主REDIS中获取SESSION
148 cacheKey = RedisKeyBuilder.newInstance().appendFixed(SESSION_CACHE_KEY_PRE).appendVar(jSessionID); 149 cacheKey = RedisKeyBuilder.newInstance().appendFixed(SESSION_CACHE_KEY_PRE).appendVar(jSessionID);
@@ -156,7 +157,7 @@ public class SecurityInterceptor implements HandlerInterceptor, ApplicationEvent @@ -156,7 +157,7 @@ public class SecurityInterceptor implements HandlerInterceptor, ApplicationEvent
156 157
157 //8 session双云同步延迟时,获取用户session 158 //8 session双云同步延迟时,获取用户session
158 if(null == sessionInfo){ 159 if(null == sessionInfo){
159 - sessionInfo = this.getUserSesion(uid, jSessionID, clientType, sessionType); 160 + sessionInfo = this.getUserSesion(cacheKey,uid, jSessionID, clientType, sessionType);
160 } 161 }
161 162
162 //9 校验SESSION, 校验不通过重新登录 163 //9 校验SESSION, 校验不通过重新登录
@@ -344,7 +345,7 @@ public class SecurityInterceptor implements HandlerInterceptor, ApplicationEvent @@ -344,7 +345,7 @@ public class SecurityInterceptor implements HandlerInterceptor, ApplicationEvent
344 * @param sessionKey 345 * @param sessionKey
345 * @return 346 * @return
346 */ 347 */
347 - private String getUserSesion(String uid, String sessionKey, String clientType, String sessionType){ 348 + private String getUserSesion(RedisKeyBuilder cacheKey,String uid, String sessionKey, String clientType, String sessionType){
348 try{ 349 try{
349 boolean degrade_getSession_enable = configReader.getBoolean("gateway.degrade.users.getUserSesion.enable",false); 350 boolean degrade_getSession_enable = configReader.getBoolean("gateway.degrade.users.getUserSesion.enable",false);
350 if(degrade_getSession_enable){ 351 if(degrade_getSession_enable){
@@ -356,10 +357,13 @@ public class SecurityInterceptor implements HandlerInterceptor, ApplicationEvent @@ -356,10 +357,13 @@ public class SecurityInterceptor implements HandlerInterceptor, ApplicationEvent
356 reqBO.setClientType(clientType); 357 reqBO.setClientType(clientType);
357 reqBO.setSessionType(sessionType); 358 reqBO.setSessionType(sessionType);
358 UserSessionReqBO result = serviceCaller.call("uic.selectUserSession", reqBO, UserSessionReqBO.class); 359 UserSessionReqBO result = serviceCaller.call("uic.selectUserSession", reqBO, UserSessionReqBO.class);
359 - logger.debug("SecurityInterceptor: call uic.selectUserSession, uid is {}, sessionKey is {}"); 360 + logger.debug("SecurityInterceptor: call uic.selectUserSession, uid is {}, sessionKey is {},result is {}",uid,sessionKey,result);
360 if(result == null || result.getUid() == null){ 361 if(result == null || result.getUid() == null){
361 return null; 362 return null;
362 } 363 }
  364 + //特殊处理(有可能与有货不共用redis):如果获取到缓存,把缓存再一次保存到ufo的redis中
  365 + valueOperations.set(cacheKey,result.getSessionKey(),result.getSessionKeyTimeOut(), TimeUnit.SECONDS);
  366 + logger.debug("SecurityInterceptor: set session to redis uid {},session key {},result {}",uid,result.getSessionKey(),result);
363 return String.valueOf(result.getUid()); 367 return String.valueOf(result.getUid());
364 }catch(Exception e){ 368 }catch(Exception e){
365 logger.warn("SecurityInterceptor: getUserSession failed ! uid is {}, sessionKey is {}, error is {}", uid, sessionKey, e); 369 logger.warn("SecurityInterceptor: getUserSession failed ! uid is {}, sessionKey is {}, error is {}", uid, sessionKey, e);