if (annotation != null) { if (this.isRepeatSubmit(request, annotation)) { AjaxResult ajaxResult = AjaxResult.error(annotation.message()); ServletUtils.renderString(response, JSON.toJSONString(ajaxResult)); return false; } } 这两个if语句怎么合并
时间: 2024-02-14 09:13:16 浏览: 76
可以使用逻辑运算符 `&&` 将两个 if 语句合并为一个条件语句,如下所示:
```
if (annotation != null && this.isRepeatSubmit(request, annotation)) {
AjaxResult ajaxResult = AjaxResult.error(annotation.message());
ServletUtils.renderString(response, JSON.toJSONString(ajaxResult));
return false;
}
```
这样可以简化代码,提高可读性。当且仅当 `annotation` 不为 null 且 `isRepeatSubmit` 返回 true 时,才会执行后续的重复提交处理逻辑。
相关问题
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; } 重构这段代码
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));
}
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; } } 优化这段代码
首先,可以将该方法拆分成两个方法,一个处理重复提交的逻辑,一个处理登录用户的逻辑,以提高代码可读性和可维护性。
处理重复提交的逻辑可以改为使用注解方式,遍历所有的参数,判断是否存在 @Token 注解,如果存在则验证 token 是否一致。
处理登录用户的逻辑可以简化代码,将获取当前登录用户和验证 token 的逻辑移动到 TokenService 中,简化该方法代码。
重构后的代码如下所示:
```
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
Token annotation = method.getAnnotation(Token.class);
if (annotation != null) {
if (!tokenService.verifyToken(request)) {
AjaxResult ajaxResult = AjaxResult.error(annotation.message());
ServletUtils.renderString(response, JSON.toJSONString(ajaxResult));
return false;
}
}
}
if (tokenService.checkLogin(request)) {
HttpSession session = request.getSession();
if (session.getAttribute(Constants.SERVER_TYPE_APP) == null) {
session.setAttribute(Constants.SERVER_TYPE_APP, tokenService.getLoginUser(request).getGuest());
}
return true;
} else {
ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(HttpStatus.UNAUTHORIZED.value(), "未登录或登录超时。")));
return false;
}
}
```
其中,`TokenService` 的实现中包括了 `verifyToken` 和 `checkLogin` 方法,分别用于验证 token 和检查当前用户是否已经登录。同时,重复提交的逻辑使用 `@Token` 注解进行处理。
阅读全文