Object loginUser = request.getSession().getAttribute("loginUser");
时间: 2024-06-09 14:09:32 浏览: 7
这行代码是从一个HTTP请求中获取当前登录用户的信息。它首先通过调用 `request.getSession()` 方法来获取当前用户的会话对象,然后调用 `getAttribute()` 方法来获取名为 "loginUser" 的属性。这个属性可以在用户登录时被设置,通常包含用户的身份信息。如果找不到该属性,那么 `loginUser` 变量将被设置为 `null`。
相关问题
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` 注解进行处理。
解释如下代码: User loginUser = (User) request.getSession().getAttribute("localUser");
这段代码是从Session中获取名为"localUser"的属性,并将其强制转换为类型为User的对象赋值给loginUser变量。一般来说,Session是在Web应用程序中用于在服务器和客户端之间共享数据的机制。在该代码中,"localUser"属性可能是在用户登录时设置的,以便在用户与服务器之间的会话中保留用户的身份信息。通过该代码,应用程序可以检索到已经登录的User对象,并在需要时使用该对象。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![](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)
![](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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)