public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (handler instanceof HandlerMethod) { HandlerMethod handlerMethod = (HandlerMethod) handler; Method method = handlerMethod.getMethod(); RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class); if (annotation != null && this.isRepeatSubmit(request, annotation)) { AjaxResult ajaxResult = AjaxResult.error(annotation.message()); ServletUtils.renderString(response, JSON.toJSONString(ajaxResult)); return false; } // if (annotation != null) { // if (this.isRepeatSubmit(request, annotation)) { // AjaxResult ajaxResult = AjaxResult.error(annotation.message()); // ServletUtils.renderString(response, JSON.toJSONString(ajaxResult)); // return false; // } // } } boolean flag = false; TokenService tokenService = SpringUtils.getBean(TokenService.class); LoginAppUser loginUser = tokenService.getLoginUser(request); if (loginUser != null && loginUser.getGuest() != null && loginUser.getGuest().getGuestNum() != null) { tokenService.verifyToken(loginUser); String guestNum = loginUser.getGuest().getGuestNum(); HttpSession httpSession = request.getSession(); RedisCache redisUtil = SpringUtils.getBean(RedisCache.class); Guest guest = redisUtil.getCacheMapValue(RedisConstants.GUEST_MAP, guestNum); SysGuest sysGuest = JSON.parseObject(JSON.toJSONString(guest), SysGuest.class); if (sysGuest == null) { flag = false; httpSession.removeAttribute(Constants.SERVER_TYPE_APP); } else { if (httpSession.getAttribute(Constants.SERVER_TYPE_APP) == null) { httpSession.setAttribute(Constants.SERVER_TYPE_APP , sysGuest); } return true; } } //获取网名 也就是/login String servletPath = request.getServletPath(); if(servletPath.contains("/app/card/isCard") || servletPath.contains("/app/bank/signingBankCard") || servletPath.contains("/app/bank/sendMessage") || servletPath.contains("/app/bank/sendSysMessage") || servletPath.contains("/app/bank/login") || servletPath.contains("/app/card/guestIdAndPhone") || servletPath.contains("/app/family/isDevice") || servletPath.contains("/websocket/") || servletPath.contains("/upload/") || servletPath.contains("/app/deviceSocket/toSocket")) { flag = true; } if (!flag) { noLogin(response); } return flag; } 重构这段代码
时间: 2024-02-14 11:19:35 浏览: 86
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (!(handler instanceof HandlerMethod)) {
return true;
}
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class);
if (annotation != null && isRepeatSubmit(request, annotation)) {
AjaxResult ajaxResult = AjaxResult.error(annotation.message());
ServletUtils.renderString(response, JSON.toJSONString(ajaxResult));
return false;
}
TokenService tokenService = SpringUtils.getBean(TokenService.class);
LoginAppUser loginUser = tokenService.getLoginUser(request);
if (loginUser != null && loginUser.getGuest() != null && loginUser.getGuest().getGuestNum() != null) {
tokenService.verifyToken(loginUser);
String guestNum = loginUser.getGuest().getGuestNum();
HttpSession httpSession = request.getSession();
RedisCache redisUtil = SpringUtils.getBean(RedisCache.class);
Guest guest = redisUtil.getCacheMapValue(RedisConstants.GUEST_MAP, guestNum);
SysGuest sysGuest = JSON.parseObject(JSON.toJSONString(guest), SysGuest.class);
if (sysGuest == null) {
httpSession.removeAttribute(Constants.SERVER_TYPE_APP);
noLogin(response);
return false;
} else {
if (httpSession.getAttribute(Constants.SERVER_TYPE_APP) == null) {
httpSession.setAttribute(Constants.SERVER_TYPE_APP, sysGuest);
}
return true;
}
}
List<String> allowedPaths = Arrays.asList("/app/card/isCard", "/app/bank/signingBankCard", "/app/bank/sendMessage", "/app/bank/sendSysMessage", "/app/bank/login", "/app/card/guestIdAndPhone", "/app/family/isDevice", "/websocket/", "/upload/", "/app/deviceSocket/toSocket");
String servletPath = request.getServletPath();
if (allowedPaths.stream().anyMatch(servletPath::contains)) {
return true;
} else {
noLogin(response);
return false;
}
}
private boolean isRepeatSubmit(HttpServletRequest request, RepeatSubmit annotation) {
if (annotation == null) {
return false;
}
String key = getCacheKey(request, annotation);
Object cacheObj = redisUtil.getCacheObject(key);
if (cacheObj != null) {
return true;
}
redisUtil.setCacheObject(key, key, annotation.time());
return false;
}
private String getCacheKey(HttpServletRequest request, RepeatSubmit annotation) {
String token = tokenService.getToken(request);
if (StringUtils.isNotEmpty(token)) {
return "repeatSubmit:" + token + ":" + request.getServletPath();
}
return "repeatSubmit:" + request.getSession().getId() + ":" + request.getServletPath();
}
private void noLogin(HttpServletResponse response) throws IOException {
AjaxResult ajaxResult = AjaxResult.error(HttpStatus.UNAUTHORIZED.value(), "用户未登录");
ServletUtils.renderString(response, JSON.toJSONString(ajaxResult));
}
阅读全文