From 247bcce796191a7969294f3e7a60a5a6c11f54d3 Mon Sep 17 00:00:00 2001 From: Gary Date: Tue, 16 Jan 2024 00:35:58 +0800 Subject: [PATCH] 1.fix autologin issue --- .../com/luoo/user/aspect/OperationAspect.java | 3 +- .../luoo/user/controller/UserController.java | 31 +++++++++---------- .../com/luoo/user/service/UserService.java | 11 +++---- 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/luoo_user/src/main/java/com/luoo/user/aspect/OperationAspect.java b/luoo_user/src/main/java/com/luoo/user/aspect/OperationAspect.java index 2d12dbe..eab24ff 100644 --- a/luoo_user/src/main/java/com/luoo/user/aspect/OperationAspect.java +++ b/luoo_user/src/main/java/com/luoo/user/aspect/OperationAspect.java @@ -89,7 +89,8 @@ public class OperationAspect { String ip = getIpAddr(request); ip = ip.replace(":", ""); String redisKey = fullMethodName + ip; - Integer cuCount = (Integer) redisUtils.get(redisKey); + Object count=redisUtils.get(redisKey); + Integer cuCount= null==count?0:Integer.valueOf((String)count); if (cuCount != null && cuCount > threshold - 1) { throw new BizException(StatusCode.REQUEST_TOO_FREQUENCY); } diff --git a/luoo_user/src/main/java/com/luoo/user/controller/UserController.java b/luoo_user/src/main/java/com/luoo/user/controller/UserController.java index 71d4b5e..5eacdb4 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/UserController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/UserController.java @@ -90,7 +90,7 @@ public class UserController { return Result.success(map); } - @ApiOperation(value = "2.登录/注册后返回token") + @ApiOperation(value = "2.登录/注册",notes="成功后返回用户信息和token") @PostMapping("/appLogin/{mobile}/{mobileCheckCode}") @GlobalInterceptor public Result appLogin( @PathVariable @VerifyParam(required=true,regex=VerifyRegexEnum.MOBILE)String mobile, @@ -135,7 +135,7 @@ public class UserController { */ @ApiOperation(value = "1.发送短信验证码", notes = "有效期15分钟,一个手机号一天最多发50次请求") @PostMapping("/sendsms/{mobile}") - @GlobalInterceptor(checkLogin = true, frequencyType = RequestFrequencyTypeEnum.DAY, requestFrequencyThreshold = 50) + @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.DAY, requestFrequencyThreshold = 50) public Result sendSms(@PathVariable @VerifyParam(required=true,regex=VerifyRegexEnum.MOBILE)String mobile) { userService.sendSms(mobile); return Result.success(); @@ -144,17 +144,17 @@ public class UserController { /** * token 续期 */ - @ApiOperation(value = "5.token 续期,有效期7天") - @PostMapping("/autoLogin/{token}") + @ApiOperation(value = "5.token续期", notes = "有效期7天") + @PostMapping("/autoLogin") @GlobalInterceptor - public Result autoLogin(@PathVariable @VerifyParam(required = true) String token) { + public Result autoLogin(@RequestHeader(value = "token", required = false) String token) { return Result.success(userService.autoLogin(token)); } /** * 获取图行验证码 */ - @ApiOperation(value = "4.三次短信验证码失败后,获取图形验证码,有效期10分钟") + @ApiOperation(value="4.获取图形验证码", notes = "4.三次短信验证失败后,获取图形验证码,有效期10分钟") @GetMapping("/imageCheckCode/{mobile}") @GlobalInterceptor public void imageCheckCode(HttpServletResponse response, @@ -179,18 +179,15 @@ public class UserController { ) { // 得到缓存中的验证码 String redisImageCheckCodeKey=Constants.REDIS_KEY_IMAGE_CHECK_CODE+mobile; - try { - String redisImageCheckCode = (String) redisTemplate.opsForValue().get(redisImageCheckCodeKey); - if (StringTools.isEmpty(redisImageCheckCode)) { - return Result.failed(StatusCode.USER_IMAGE_VERIFICATION_CODE_EXPIRED); - } - if (!redisImageCheckCode.equals(imageCheckCode)) { - return Result.failed(StatusCode.USER_IMAGE_VERIFICATION_CODE_MISMATCH); - } - return appLogin(mobile,mobileCheckCode); - }finally { - redisTemplate.delete(redisImageCheckCodeKey); + String redisImageCheckCode = (String) redisTemplate.opsForValue().get(redisImageCheckCodeKey); + if (StringTools.isEmpty(redisImageCheckCode)) { + return Result.failed(StatusCode.USER_IMAGE_VERIFICATION_CODE_EXPIRED); + } + if (!redisImageCheckCode.equalsIgnoreCase(imageCheckCode)) { + return Result.failed(StatusCode.USER_IMAGE_VERIFICATION_CODE_MISMATCH); } + redisTemplate.delete(redisImageCheckCodeKey); + return appLogin(mobile,mobileCheckCode); } /* diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserService.java b/luoo_user/src/main/java/com/luoo/user/service/UserService.java index bd8ae9d..9bafc97 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserService.java @@ -33,6 +33,7 @@ import com.luoo.user.util.NickNameUtil; import api.Result; import api.StatusCode; +import dto.UserLoginDto; import io.jsonwebtoken.Claims; import util.JwtUtil; @@ -288,15 +289,13 @@ public class UserService { public String autoLogin(String token) { try { - Claims claims = jwtUtil.parseJWT(token); - String userId = claims.getId(); - if(Constants.TOKEN_ROLE_APP_USER.equals((String) claims.get(Constants.TOKEN_ROLE))){ - User user = userDao.findById(userId).orElse(null); + UserLoginDto userLoginDto=jwtUtil.getUserLoginDto(token); + if(Constants.TOKEN_ROLE_APP_USER.equals(userLoginDto.getRoles())){ + User user = userDao.findById(userLoginDto.getUserId()).get(); user.setLastdate(new Date()); userDao.save(user); } - - return jwtUtil.createJWT(userId, claims.getSubject(), Constants.TOKEN_ROLE_APP_USER); + return jwtUtil.createJWT(userLoginDto.getUserId(), userLoginDto.getNickName(), userLoginDto.getRoles()); } catch (Exception e) { return null; }