Commit 511edf3f by zhouhaibao

[+]登录信息添加

1 parent 626f6f62
...@@ -27,138 +27,140 @@ import java.util.concurrent.TimeUnit; ...@@ -27,138 +27,140 @@ import java.util.concurrent.TimeUnit;
/** /**
* scrm登录体系 * scrm登录体系
*
*/ */
@Slf4j @Slf4j
@Component @Component
public class SSOUserInterceptor implements HandlerInterceptor { public class SSOUserInterceptor implements HandlerInterceptor {
public static final String USER_KEY = "SSO_USER_SESSION"; public static final String USER_KEY = "SSO_USER_SESSION";
public static final String WECHAT_USER_KEY = "SSO_WECHAT_USER_KEY"; public static final String WECHAT_USER_KEY = "SSO_WECHAT_USER_KEY";
private final String SESSION_KEY = "ROP_SESSION:"; private final String SESSION_KEY = "ROP_SESSION:";
@Autowired @Autowired
private RedisConnectionFactory redisConnectionFactory; private RedisConnectionFactory redisConnectionFactory;
private final String SEPARATOR_COLON = ":"; private final String SEPARATOR_COLON = ":";
private long timeout = 1800; private long timeout = 1800;
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
if (handler instanceof HandlerMethod) {
boolean needLoginFlag = true; // 默认需要登录
HandlerMethod handlerMethod = (HandlerMethod) handler;
//##判断是否需要登录
// 1.先判断类上是否存在@NoLogin,@CheckSign注解[默认无需登录注解]
Class<?> targetBean = handlerMethod.getBeanType();
//验签无需登录校验
if (targetBean.isAnnotationPresent(CheckSign.class) || handlerMethod.hasMethodAnnotation(
CheckSign.class)) {
return true;
}
if (targetBean.isAnnotationPresent(NoLogin.class)) {
// 如果存在,则根据注解的value()确定是否需要登录访问
NoLogin noLogin = targetBean.getAnnotation(NoLogin.class);
needLoginFlag = !noLogin.value();
}
// 2.再判断调用方法上是否存在@NoLogin注解[默认无需登录注解]
if (handlerMethod.hasMethodAnnotation(NoLogin.class)) {
// 如果存在,则根据注解的value()确定是否需要登录访问[方法覆盖类]
NoLogin noLogin = handlerMethod.getMethod().getAnnotation(NoLogin.class);
needLoginFlag = !noLogin.value();
}
// 3.需要登录访问则去验证token,反之,则通过
if (!needLoginFlag) {
return true; return true;
// if (handler instanceof HandlerMethod) { }
// boolean needLoginFlag = true; // 默认需要登录
// //##判断是否登录
// HandlerMethod handlerMethod = (HandlerMethod) handler; String token = request.getHeader(HttpHeaders.AUTHORIZATION);
// //##判断是否需要登录 // 1.判断请求是否携带token
// // 1.先判断类上是否存在@NoLogin,@CheckSign注解[默认无需登录注解] if (StringUtils.isNotEmpty(token)) {
// Class<?> targetBean = handlerMethod.getBeanType(); Session session = getSession(token);
// if (session != null && session instanceof SimpleSession) {
// //验签无需登录校验 SimpleSession simpleSession = SimpleSession.class.cast(session);
// if (targetBean.isAnnotationPresent(CheckSign.class) || handlerMethod.hasMethodAnnotation(CheckSign.class)) { // 非空的情况下解析用户信息放入缓存中
// return true; Object user = simpleSession.getAttribute(USER_KEY);
// } if (user != null) {
// try {
// if (targetBean.isAnnotationPresent(NoLogin.class)) { log.info("用户信息{}", JSONObject.toJSONString(user));
// // 如果存在,则根据注解的value()确定是否需要登录访问 RopThreadLocal.setLoginUser(user);
// NoLogin noLogin = targetBean.getAnnotation(NoLogin.class); return true;
// needLoginFlag = !noLogin.value(); } catch (Exception e) {
// } log.error("rop a conversion user error:{}", e.getMessage(), e);
// }
// // 2.再判断调用方法上是否存在@NoLogin注解[默认无需登录注解] } else {
// if (handlerMethod.hasMethodAnnotation(NoLogin.class)) { Object wechatUserInfo = simpleSession.getAttribute(WECHAT_USER_KEY);
// // 如果存在,则根据注解的value()确定是否需要登录访问[方法覆盖类] if (wechatUserInfo != null) {
// NoLogin noLogin = handlerMethod.getMethod().getAnnotation(NoLogin.class); try {
// needLoginFlag = !noLogin.value(); log.info("小程序用户信息{}", JSONObject.toJSONString(wechatUserInfo));
// } RopThreadLocal.setWechatLoginUserInfo(wechatUserInfo);
// // 3.需要登录访问则去验证token,反之,则通过 return true;
// if (!needLoginFlag) { } catch (Exception e) {
// return true; log.error("rop a conversion user error:{}", e.getMessage(), e);
// } }
// } else {
// //##判断是否登录 // 需要登录
// String token = request.getHeader(HttpHeaders.AUTHORIZATION); needLogin(response);
// // 1.判断请求是否携带token return false;
// if (StringUtils.isNotEmpty(token)) { }
// Session session = getSession(token); }
// if (session != null && session instanceof SimpleSession) { }
// SimpleSession simpleSession = SimpleSession.class.cast(session); }
// // 非空的情况下解析用户信息放入缓存中 // 需要登录
// Object user = simpleSession.getAttribute(USER_KEY); needLogin(response);
// if (user != null) { return false;
// try { } else {
// log.info("用户信息{}", JSONObject.toJSONString(user)); return true;
// cn.com.glsx.evbusiness.config.RopThreadLocal.setLoginUser(user);
// return true;
// } catch (Exception e) {
// log.error("rop a conversion user error:{}", e.getMessage(), e);
// }
// } else {
// Object wechatUserInfo = simpleSession.getAttribute(WECHAT_USER_KEY);
// if (wechatUserInfo != null) {
// try {
// log.info("小程序用户信息{}", JSONObject.toJSONString(wechatUserInfo));
// cn.com.glsx.evbusiness.config.RopThreadLocal.setWechatLoginUserInfo(wechatUserInfo);
// return true;
// } catch (Exception e) {
// log.error("rop a conversion user error:{}", e.getMessage(), e);
// }
// } else {
// // 需要登录
// needLogin(response);
// return false;
// }
// }
// }
// }
// // 需要登录
// needLogin(response);
// return false;
// } else {
// return true;
// }
} }
}
@Override @Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) throws Exception {
} }
private void needLogin(HttpServletResponse response) throws Exception { private void needLogin(HttpServletResponse response) throws Exception {
response.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8"); response.setContentType("application/json; charset=utf-8");
try (PrintWriter writer = response.getWriter()) { try (PrintWriter writer = response.getWriter()) {
writer.write(JSON.toJSONString(R.error(SystemMessage.NOT_LOGIN.getCode(), SystemMessage.NOT_LOGIN.getMsg()))); writer.write(JSON.toJSONString(
writer.flush(); R.error(SystemMessage.NOT_LOGIN.getCode(), SystemMessage.NOT_LOGIN.getMsg())));
} writer.flush();
} }
}
private Session getSession(String sessionId) {
RedisTemplate<String, Session> template = new RedisTemplate<>(); private Session getSession(String sessionId) {
// 配置连接工厂 RedisTemplate<String, Session> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory); // 配置连接工厂
template.setKeySerializer(new StringRedisSerializer()); template.setConnectionFactory(redisConnectionFactory);
template.setHashKeySerializer(new StringRedisSerializer()); template.setKeySerializer(new StringRedisSerializer());
// template.setValueSerializer(new KryoRedisSerializer<>()); template.setHashKeySerializer(new StringRedisSerializer());
// template.setHashValueSerializer(new KryoRedisSerializer<>()); template.setValueSerializer(new KryoRedisSerializer<>());
template.afterPropertiesSet(); template.setHashValueSerializer(new KryoRedisSerializer<>());
Assert.notNull(template, "Redis Template Is Null"); template.afterPropertiesSet();
String sessionKey = getSessionKey(sessionId); Assert.notNull(template, "Redis Template Is Null");
log.info("登录key{}", sessionKey); String sessionKey = getSessionKey(sessionId);
Session object = template.opsForValue().get(sessionKey); log.info("登录key{}", sessionKey);
if (object instanceof Session && object != null) { Session object = template.opsForValue().get(sessionKey);
// 如果session未过期,客户端获取session后重置过期时间 if (object instanceof Session && object != null) {
template.expire(getSessionKey(sessionId), timeout, TimeUnit.SECONDS); // 如果session未过期,客户端获取session后重置过期时间
} template.expire(getSessionKey(sessionId), timeout, TimeUnit.SECONDS);
return object;
} }
return object;
}
private String getSessionKey(String sessionId) { private String getSessionKey(String sessionId) {
return SESSION_KEY.concat("scrm").concat(SEPARATOR_COLON).concat(sessionId); return SESSION_KEY.concat("scrm").concat(SEPARATOR_COLON).concat(sessionId);
} }
} }
...@@ -64,7 +64,6 @@ public class WebConfig implements WebMvcConfigurer { ...@@ -64,7 +64,6 @@ public class WebConfig implements WebMvcConfigurer {
.excludePathPatterns("/") .excludePathPatterns("/")
.excludePathPatterns("/login") .excludePathPatterns("/login")
.excludePathPatterns("/actuator/**") .excludePathPatterns("/actuator/**")
.excludePathPatterns("/api/**")
.excludePathPatterns("/platform-evbusiness/api/gbt/ocpp16/tcp/**") .excludePathPatterns("/platform-evbusiness/api/gbt/ocpp16/tcp/**")
.excludePathPatterns(uploadProperties.getFilepath()) .excludePathPatterns(uploadProperties.getFilepath())
.excludePathPatterns("/swagger-resources/**", "/swagger-ui/**", "/v3/**", "/doc.html"); .excludePathPatterns("/swagger-resources/**", "/swagger-ui/**", "/v3/**", "/doc.html");
......
...@@ -71,7 +71,6 @@ public class OrderService { ...@@ -71,7 +71,6 @@ public class OrderService {
startBo.setAccountBalance(2000.00); startBo.setAccountBalance(2000.00);
startBo.setPhysicalCardNumber("00000000D14B0A54"); startBo.setPhysicalCardNumber("00000000D14B0A54");
startBo.setLogicalCardNumber("00000000D14B0A54"); startBo.setLogicalCardNumber("00000000D14B0A54");
insertOrder(orderCreateBO, orderCode);
Result<Boolean> start = pileControlFeignClient.start(JSONObject.toJSONString(startBo)); Result<Boolean> start = pileControlFeignClient.start(JSONObject.toJSONString(startBo));
if (start.getData()) { if (start.getData()) {
// 4.创建待支付的订单 // 4.创建待支付的订单
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!