黑馬程序員Redis入門到實(shí)戰(zhàn)教程,深度透析redis底層原理+redis分布式
2023-08-05 15:38 作者:微風(fēng)簇細(xì)浪 | 我要投稿

p33中的同一個(gè)用戶多次登錄會在redis中生成多個(gè)token,雖然一般用戶不會這樣做,但是難保不會有什么惡意攻擊沙的。我在UserService中寫了一個(gè)方法來判斷是否重復(fù)登錄并返回錯(cuò)誤信息。
public class UserController { @PostMapping("/login") public Result login(@RequestBody LoginFormDTO loginForm, HttpSession session, HttpServletRequest request){ // 實(shí)現(xiàn)登錄功能 if (userService.isLogined(request)) { return Result.fail("拒絕重復(fù)登錄"); } 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; }
標(biāo)簽: