重构这个方法将其认知复杂度从17降低到允许的15: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-03-13 10:43:06 浏览: 119
spring-method-interceptor:Spring Handler Interceptor反思Web方法
首先,可以将整个方法拆分成几个小的方法,这样可以使代码更易于维护和测试。其次,可以将注解的判断提取出来,避免在方法中嵌套过多的if语句。最后,可以将获取网名的代码块与其他代码块分开,这样可以使代码更加清晰易懂。
以下是重构后的代码示例:
```
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (isRepeatSubmit(request, handler)) {
renderRepeatSubmitError(response);
return false;
}
boolean flag = false;
LoginAppUser loginUser = getLoginUser(request);
if (loginUser != null) {
flag = setServerTypeApp(request, loginUser);
}
if (!flag) {
flag = checkIfNoLogin(request);
}
return flag;
}
private boolean isRepeatSubmit(HttpServletRequest request, Object handler) {
if (!(handler instanceof HandlerMethod)) {
return false;
}
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class);
return annotation != null && this.isRepeatSubmit(request, annotation);
}
private boolean setServerTypeApp(HttpServletRequest request, LoginAppUser loginUser) {
boolean flag = false;
if (loginUser.getGuest() != null && loginUser.getGuest().getGuestNum() != null) {
TokenService tokenService = SpringUtils.getBean(TokenService.class);
tokenService.verifyToken(loginUser);
String guestNum = loginUser.getGuest().getGuestNum();
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 httpSession = request.getSession();
if (httpSession.getAttribute(Constants.SERVER_TYPE_APP) == null) {
httpSession.setAttribute(Constants.SERVER_TYPE_APP, sysGuest);
}
flag = true;
} else {
HttpSession httpSession = request.getSession();
httpSession.removeAttribute(Constants.SERVER_TYPE_APP);
}
}
return flag;
}
private boolean checkIfNoLogin(HttpServletRequest request) {
String servletPath = request.getServletPath();
return 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");
}
private void renderRepeatSubmitError(HttpServletResponse response) throws IOException {
AjaxResult ajaxResult = AjaxResult.error(annotation.message());
ServletUtils.renderString(response, JSON.toJSONString(ajaxResult));
}
private void noLogin(HttpServletResponse response) throws IOException {
AjaxResult ajaxResult = AjaxResult.error("未登录或登录超时。请重新登录。");
ServletUtils.renderString(response, JSON.toJSONString(ajaxResult));
}
private LoginAppUser getLoginUser(HttpServletRequest request) {
TokenService tokenService = SpringUtils.getBean(TokenService.class);
return tokenService.getLoginUser(request);
}
```
这样将认知复杂度降低到了15以下,同时使代码更加易于维护和测试。
阅读全文