From 4f968fe3d385c8b79cc30158703a4686f6f8ba28 Mon Sep 17 00:00:00 2001 From: Gary Date: Mon, 15 Jan 2024 15:00:59 +0800 Subject: [PATCH] =?UTF-8?q?1.add=20autoLogin=20=E7=BB=AD=E6=9C=9F=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/luoo/user/constants/Constants.java | 6 +++++ .../luoo/user/controller/UserController.java | 23 ++++++++----------- .../com/luoo/user/service/UserService.java | 19 +++++++++++++++ 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/luoo_user/src/main/java/com/luoo/user/constants/Constants.java b/luoo_user/src/main/java/com/luoo/user/constants/Constants.java index e081085..16e4cd9 100644 --- a/luoo_user/src/main/java/com/luoo/user/constants/Constants.java +++ b/luoo_user/src/main/java/com/luoo/user/constants/Constants.java @@ -5,4 +5,10 @@ public class Constants { public static final String REDIS_KEY_IMAGE_CHECK_CODE="redis_key_image_check_code_"; public static final String REDIS_KEY_MOBILE_CHECK_CODE="redis_key_mobile_check_code_"; + + public static final String TOKEN_ROLE="roles"; + + public static final String TOKEN_ROLE_APP_USER="user"; + public static final String TOKEN_ROLE_ADMIN_USER="admin"; + public static final String TOKEN_ROLE_TOURIST="tourist"; } 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 8ba1b22..079fae8 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 @@ -27,6 +27,7 @@ import com.luoo.user.util.NickNameUtil; import api.PageResult; import api.Result; import api.StatusCode; +import io.jsonwebtoken.Claims; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import util.IdWorker; @@ -106,7 +107,7 @@ public class UserController { } User user = userService.loginOrRegister(mobile); BeanUtils.copyProperties(user, userRespDTO); - String token = jwtUtil.createJWT(user.getId(),user.getMobile(),"user"); + String token = jwtUtil.createJWT(user.getId(),user.getNickname(),Constants.TOKEN_ROLE_APP_USER); userRespDTO.setToken(token); return Result.success(userRespDTO); }finally { @@ -120,8 +121,9 @@ public class UserController { public Result touristLogin() { UserRespDTO userRespDTO=new UserRespDTO(); userRespDTO.setId(String.valueOf(idWorker.nextId())); + String nickName="游客-"+NickNameUtil.getRandomNickName(); userRespDTO.setNickname("游客-"+NickNameUtil.getRandomNickName()); - String token = jwtUtil.createJWT(userRespDTO.getId(),"tourist","tourist"); + String token = jwtUtil.createJWT(userRespDTO.getId(),nickName,Constants.TOKEN_ROLE_TOURIST); userRespDTO.setToken(token); return Result.success(userRespDTO); } @@ -140,17 +142,12 @@ public class UserController { /** * token 续期 */ - /* - * @ApiOperation(value = "5.token 续期,默认token 有效期7天") - * - * @PostMapping("/autoLogin") - * - * @GlobalInterceptor public Result - * autoLogin(@PathVariable @VerifyParam(required=true,max=32) String deviceId, - * - * @PathVariable @VerifyParam(required=true,regex=VerifyRegexEnum.MOBILE)String - * mobile) { userService.sendSms(deviceId,mobile); return Result.success(); } - */ + @ApiOperation(value = "5.token 续期,有效期7天") + @PostMapping("/autoLogin/{token}") + @GlobalInterceptor + public Result autoLogin(@PathVariable @VerifyParam(required = true) String token) { + return Result.success(userService.autoLogin(token)); + } /** * 获取图行验证码 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 82ddd04..bd8ae9d 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 @@ -31,6 +31,9 @@ import com.luoo.user.enumerate.Gender; import com.luoo.user.pojo.User; import com.luoo.user.util.NickNameUtil; +import api.Result; +import api.StatusCode; +import io.jsonwebtoken.Claims; import util.JwtUtil; /** @@ -282,4 +285,20 @@ public class UserService { User user = userDao.findByMobile(mobile); return user; } + + 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); + user.setLastdate(new Date()); + userDao.save(user); + } + + return jwtUtil.createJWT(userId, claims.getSubject(), Constants.TOKEN_ROLE_APP_USER); + } catch (Exception e) { + return null; + } + } }