public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // String token = TokenHelper.getFromReq(request); String token = request.getHeader(Authentication.TOKEN_NAME); if (StringUtils.isEmpty(token)) { // mute for OPTIONS if (!request.getMethod().equalsIgnoreCase(HttpMethod.OPTIONS.name())) { // need log return true; } return false; } UserContext userContext = Authentication.verify(token); if (userContext == null) { log.error(""); return false; } UserContextHolder.set(userContext); return true; }这段代码没又token就没办法走完吗
时间: 2024-02-14 14:28:42 浏览: 19
根据你提供的代码,如果请求中没有提供 token,那么这段代码会返回 `false`,即请求将被拦截并不会继续执行后续的处理逻辑。
具体来说,在没有提供 token 的情况下,代码会先检查请求的方法是否为 OPTIONS,如果是 OPTIONS 请求,则会返回 `false`,这是因为 OPTIONS 请求通常是用于预检请求,不需要进行身份验证。
如果请求不是 OPTIONS 请求且没有提供 token,则会记录一条错误日志,并返回 `false`,表示身份验证失败,请求将被拦截。
只有在请求中提供了有效的 token 且经过验证后,代码才会继续执行后续的逻辑,并将验证通过的用户上下文信息存储到 UserContextHolder 中。
需要注意的是,这段代码中可能还存在其他的处理逻辑或者异常处理,上述的解释仅基于你所提供的代码片段。如果需要更详细的解释或者有其他问题,请提供更多相关的代码信息。
相关问题
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // String token = TokenHelper.getFromReq(request); String token = request.getHeader(Authentication.TOKEN_NAME); if (StringUtils.isEmpty(token)) { // mute for OPTIONS if (!request.getMethod().equalsIgnoreCase(HttpMethod.OPTIONS.name())) { // need log return true; } return false; } UserContext userContext = Authentication.verify(token); if (userContext == null) { log.error(""); return false; } UserContextHolder.set(userContext); return true; } 这段代码干什么的
这段代码是一个拦截器(Interceptor)中的方法,用于在请求处理之前进行预处理。具体功能如下:
1. 获取请求中的 token 值。在这段代码中,使用 `request.getHeader(Authentication.TOKEN_NAME)` 方法从请求头中获取 token。
2. 检查 token 是否为空,如果为空,则判断请求方法是否为 OPTIONS。如果是 OPTIONS 请求,则直接返回 false,表示允许跨域请求的预检请求通过;如果不是 OPTIONS 请求,则需要记录日志,并返回 true。
3. 如果 token 不为空,则调用 `Authentication.verify(token)` 方法进行验证。如果验证成功,返回一个包含用户上下文信息的 `UserContext` 对象;如果验证失败,则记录错误日志,并返回 false。
4. 如果验证成功,将用户上下文信息存储到 `UserContextHolder` 中,以便后续代码可以获取到用户上下文信息。
5. 最后,返回 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) { 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; }
首先,可以将比较长的 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;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)