Commit 511edf3f by zhouhaibao

[+]登录信息添加

1 parent 626f6f62
......@@ -27,138 +27,140 @@ import java.util.concurrent.TimeUnit;
/**
* scrm登录体系
*
*/
@Slf4j
@Component
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
private RedisConnectionFactory redisConnectionFactory;
@Autowired
private RedisConnectionFactory redisConnectionFactory;
private final String SEPARATOR_COLON = ":";
private final String SEPARATOR_COLON = ":";
private long timeout = 1800;
private long timeout = 1800;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
@Override
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;
// 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;
// }
//
// //##判断是否登录
// String token = request.getHeader(HttpHeaders.AUTHORIZATION);
// // 1.判断请求是否携带token
// 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);
// if (user != null) {
// try {
// log.info("用户信息{}", JSONObject.toJSONString(user));
// 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;
// }
}
//##判断是否登录
String token = request.getHeader(HttpHeaders.AUTHORIZATION);
// 1.判断请求是否携带token
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);
if (user != null) {
try {
log.info("用户信息{}", JSONObject.toJSONString(user));
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));
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
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) throws Exception {
}
}
private void needLogin(HttpServletResponse response) throws Exception {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
try (PrintWriter writer = response.getWriter()) {
writer.write(JSON.toJSONString(R.error(SystemMessage.NOT_LOGIN.getCode(), SystemMessage.NOT_LOGIN.getMsg())));
writer.flush();
}
private void needLogin(HttpServletResponse response) throws Exception {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
try (PrintWriter writer = response.getWriter()) {
writer.write(JSON.toJSONString(
R.error(SystemMessage.NOT_LOGIN.getCode(), SystemMessage.NOT_LOGIN.getMsg())));
writer.flush();
}
private Session getSession(String sessionId) {
RedisTemplate<String, Session> template = new RedisTemplate<>();
// 配置连接工厂
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
// template.setValueSerializer(new KryoRedisSerializer<>());
// template.setHashValueSerializer(new KryoRedisSerializer<>());
template.afterPropertiesSet();
Assert.notNull(template, "Redis Template Is Null");
String sessionKey = getSessionKey(sessionId);
log.info("登录key{}", sessionKey);
Session object = template.opsForValue().get(sessionKey);
if (object instanceof Session && object != null) {
// 如果session未过期,客户端获取session后重置过期时间
template.expire(getSessionKey(sessionId), timeout, TimeUnit.SECONDS);
}
return object;
}
private Session getSession(String sessionId) {
RedisTemplate<String, Session> template = new RedisTemplate<>();
// 配置连接工厂
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new KryoRedisSerializer<>());
template.setHashValueSerializer(new KryoRedisSerializer<>());
template.afterPropertiesSet();
Assert.notNull(template, "Redis Template Is Null");
String sessionKey = getSessionKey(sessionId);
log.info("登录key{}", sessionKey);
Session object = template.opsForValue().get(sessionKey);
if (object instanceof Session && object != null) {
// 如果session未过期,客户端获取session后重置过期时间
template.expire(getSessionKey(sessionId), timeout, TimeUnit.SECONDS);
}
return object;
}
private String getSessionKey(String sessionId) {
return SESSION_KEY.concat("scrm").concat(SEPARATOR_COLON).concat(sessionId);
}
private String getSessionKey(String sessionId) {
return SESSION_KEY.concat("scrm").concat(SEPARATOR_COLON).concat(sessionId);
}
}
......@@ -64,7 +64,6 @@ public class WebConfig implements WebMvcConfigurer {
.excludePathPatterns("/")
.excludePathPatterns("/login")
.excludePathPatterns("/actuator/**")
.excludePathPatterns("/api/**")
.excludePathPatterns("/platform-evbusiness/api/gbt/ocpp16/tcp/**")
.excludePathPatterns(uploadProperties.getFilepath())
.excludePathPatterns("/swagger-resources/**", "/swagger-ui/**", "/v3/**", "/doc.html");
......
......@@ -71,7 +71,6 @@ public class OrderService {
startBo.setAccountBalance(2000.00);
startBo.setPhysicalCardNumber("00000000D14B0A54");
startBo.setLogicalCardNumber("00000000D14B0A54");
insertOrder(orderCreateBO, orderCode);
Result<Boolean> start = pileControlFeignClient.start(JSONObject.toJSONString(startBo));
if (start.getData()) {
// 4.创建待支付的订单
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!