欢迎光临散文网 会员登陆 & 注册

黑马程序员Redis入门到实战教程,深度透析redis底层原理+redis分布式

2023-08-05 15:38 作者:微风簇细浪  | 我要投稿

p33中的同一个用户多次登录会在redis中生成多个token,虽然一般用户不会这样做,但是难保不会有什么恶意攻击沙的。我在UserService中写了一个方法来判断是否重复登录并返回错误信息。

public class UserController {
  @PostMapping("/login")
  public Result login(@RequestBody LoginFormDTO loginForm, HttpSession session, HttpServletRequest request){
      // 实现登录功能
      if (userService.isLogined(request)) {
          return Result.fail("拒绝重复登录");
      }
      return userService.login(loginForm, session);
}

UserServiceImpl:

@Override
public boolean isLogined(HttpServletRequest request) {
    String token = request.getHeader("authorization");
    if (StrUtil.isBlank(token)) {
        return false;
    }
    String userKey = LOGIN_USER_KEY + token;
    Map<Object, Object> cacheUserHash = stringRedisTemplate.opsForHash().entries(userKey);
    if (cacheUserHash.isEmpty()) {
        return false;
    }
    return true;
}


黑马程序员Redis入门到实战教程,深度透析redis底层原理+redis分布式的评论 (共 条)

分享到微博请遵守国家法律