From 495669ac846e50757182a3e45f8b2526bac6fa95 Mon Sep 17 00:00:00 2001 From: Gary Date: Mon, 15 Jan 2024 15:46:55 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=E4=B8=AA=E4=BA=BA=E4=B8=AD?= =?UTF-8?q?=E5=BF=83-=E8=8E=B7=E5=8F=96=E7=94=A8=E6=88=B7=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/dto/UserLoginDto.java | 12 +++++ luoo_common/src/main/java/util/JwtUtil.java | 18 ++++++- .../src/main/java}/util/StringTools.java | 4 +- .../com/luoo/user/aspect/OperationAspect.java | 2 +- .../luoo/user/controller/MyController.java | 50 +++++++++++++++++++ .../luoo/user/controller/UserController.java | 2 +- .../java/com/luoo/user/util/VerifyUtils.java | 2 + 7 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 luoo_common/src/main/java/dto/UserLoginDto.java rename {luoo_user/src/main/java/com/luoo/user => luoo_common/src/main/java}/util/StringTools.java (90%) create mode 100644 luoo_user/src/main/java/com/luoo/user/controller/MyController.java diff --git a/luoo_common/src/main/java/dto/UserLoginDto.java b/luoo_common/src/main/java/dto/UserLoginDto.java new file mode 100644 index 0000000..d44089d --- /dev/null +++ b/luoo_common/src/main/java/dto/UserLoginDto.java @@ -0,0 +1,12 @@ +package dto; + +import lombok.Getter; +import lombok.Setter; + +@Setter +@Getter +public class UserLoginDto { + private String userId; + private String nickName; + private String roles; +} diff --git a/luoo_common/src/main/java/util/JwtUtil.java b/luoo_common/src/main/java/util/JwtUtil.java index cf46ea5..fa7e3c8 100644 --- a/luoo_common/src/main/java/util/JwtUtil.java +++ b/luoo_common/src/main/java/util/JwtUtil.java @@ -5,7 +5,8 @@ import io.jsonwebtoken.JwtBuilder; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.stereotype.Component; + +import dto.UserLoginDto; import java.util.Date; @@ -67,4 +68,19 @@ public class JwtUtil { .getBody(); } + public UserLoginDto getUserLoginDto(String token) { + try { + if (StringTools.isEmpty(token)) { + return null; + } + UserLoginDto userLoginDto=new UserLoginDto(); + Claims claims = this.parseJWT(token); + userLoginDto.setUserId(claims.getId()); + userLoginDto.setNickName(claims.getSubject()); + userLoginDto.setRoles((String) claims.get("roles")); + return userLoginDto; + } catch (Exception e) { + return null; + } + } } diff --git a/luoo_user/src/main/java/com/luoo/user/util/StringTools.java b/luoo_common/src/main/java/util/StringTools.java similarity index 90% rename from luoo_user/src/main/java/com/luoo/user/util/StringTools.java rename to luoo_common/src/main/java/util/StringTools.java index ba9a5d7..6e0490a 100644 --- a/luoo_user/src/main/java/com/luoo/user/util/StringTools.java +++ b/luoo_common/src/main/java/util/StringTools.java @@ -1,8 +1,6 @@ -package com.luoo.user.util; +package util; public class StringTools { - - public static boolean isEmpty(String str) { if (null == str || "".equals(str) || "null".equals(str) || "\u0000".equals(str)) { return true; 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 e1bdace..349ad34 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 @@ -16,11 +16,11 @@ import org.springframework.stereotype.Component; import com.luoo.user.annotation.GlobalInterceptor; import com.luoo.user.annotation.VerifyParam; -import com.luoo.user.util.StringTools; import com.luoo.user.util.VerifyUtils; import api.StatusCode; import exception.BizException; +import util.StringTools; @Aspect @Component("operationAspect") diff --git a/luoo_user/src/main/java/com/luoo/user/controller/MyController.java b/luoo_user/src/main/java/com/luoo/user/controller/MyController.java new file mode 100644 index 0000000..038849c --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/controller/MyController.java @@ -0,0 +1,50 @@ +package com.luoo.user.controller; + +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.luoo.user.annotation.GlobalInterceptor; +import com.luoo.user.annotation.VerifyParam; +import com.luoo.user.constants.Constants; +import com.luoo.user.dto.response.UserRespDTO; +import com.luoo.user.pojo.User; +import com.luoo.user.service.UserService; + +import api.Result; +import api.StatusCode; +import dto.UserLoginDto; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import util.JwtUtil; + +@RestController +@CrossOrigin +@RequestMapping("/my") +@Api(tags = "MyController") +public class MyController { + @Autowired + private JwtUtil jwtUtil; + @Autowired + private UserService userService; + + @ApiOperation(value = "1.获取个人信息") + @PostMapping("/getUserInfo/{token}") + @GlobalInterceptor + public Result getUserInfo(@RequestHeader(value = "token", required = false) String token) { + UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token); + if (null == userLoginDto) { + return Result.validateFailed(); + } + User user = userService.findById(userLoginDto.getUserId()); + UserRespDTO userRespDTO = new UserRespDTO(); + BeanUtils.copyProperties(user, userRespDTO); + userRespDTO.setToken(token); + return Result.success(userRespDTO); + } +} 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 079fae8..347234c 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 @@ -97,7 +97,6 @@ public class UserController { // 得到缓存中的验证码 String redisKey=Constants.REDIS_KEY_MOBILE_CHECK_CODE+mobile; try { - UserRespDTO userRespDTO=new UserRespDTO(); String checkcodeRedis = (String) redisTemplate.opsForValue().get(redisKey); if (null == checkcodeRedis || checkcodeRedis.isEmpty()) { return Result.failed(StatusCode.USER_VERIFICATION_CODE_EXPIRED); @@ -106,6 +105,7 @@ public class UserController { return Result.failed(StatusCode.USER_VERIFICATION_CODE_MISMATCH); } User user = userService.loginOrRegister(mobile); + UserRespDTO userRespDTO=new UserRespDTO(); BeanUtils.copyProperties(user, userRespDTO); String token = jwtUtil.createJWT(user.getId(),user.getNickname(),Constants.TOKEN_ROLE_APP_USER); userRespDTO.setToken(token); diff --git a/luoo_user/src/main/java/com/luoo/user/util/VerifyUtils.java b/luoo_user/src/main/java/com/luoo/user/util/VerifyUtils.java index 21d3129..3491674 100644 --- a/luoo_user/src/main/java/com/luoo/user/util/VerifyUtils.java +++ b/luoo_user/src/main/java/com/luoo/user/util/VerifyUtils.java @@ -6,6 +6,8 @@ import java.util.regex.Pattern; import com.luoo.user.enums.VerifyRegexEnum; +import util.StringTools; + /** * @ClassName VerifyUtils * @Description TODO