优化这段代码: 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) { 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 13:11:56 浏览: 87
首先,可以将比较长的 if 语句拆分成多个小的 if 语句,这样可以使代码更易读。例如,可以将 `if (loginUser != null && loginUser.getGuest() != null && loginUser.getGuest().getGuestNum() != null)` 拆分成三个 if 语句。
其次,可以使用常量或者枚举类型来表示一些常用的字符串,例如 Constants.SERVER_TYPE_APP 和 RedisConstants.GUEST_MAP。
最后,可以使用 try-with-resources 语句来自动关闭资源,例如 redisUtil。这样可以避免因为忘记关闭资源而导致的内存泄漏问题。
以下是优化后的代码:
```
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) {
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) {
Guest guest = loginUser.getGuest();
if (guest != null) {
String guestNum = guest.getGuestNum();
tokenService.verifyToken(loginUser);
HttpSession httpSession = request.getSession();
RedisCache redisUtil = SpringUtils.getBean(RedisCache.class);
try (redisUtil) {
Guest cacheGuest = redisUtil.getCacheMapValue(RedisConstants.GUEST_MAP, guestNum);
SysGuest sysGuest = JSON.parseObject(JSON.toJSONString(cacheGuest), SysGuest.class);
if (sysGuest != null) {
if (httpSession.getAttribute(Constants.SERVER_TYPE_APP) == null) {
httpSession.setAttribute(Constants.SERVER_TYPE_APP, sysGuest);
}
return true;
}
}
}
}
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;
}
```
阅读全文