1.添加个人中心-获取用户信息接口

main
Gary 1 year ago
parent eb1ea9b5fe
commit 495669ac84

@ -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;
}

@ -5,7 +5,8 @@ import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts; import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm; import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import dto.UserLoginDto;
import java.util.Date; import java.util.Date;
@ -67,4 +68,19 @@ public class JwtUtil {
.getBody(); .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;
}
}
} }

@ -1,8 +1,6 @@
package com.luoo.user.util; package util;
public class StringTools { public class StringTools {
public static boolean isEmpty(String str) { public static boolean isEmpty(String str) {
if (null == str || "".equals(str) || "null".equals(str) || "\u0000".equals(str)) { if (null == str || "".equals(str) || "null".equals(str) || "\u0000".equals(str)) {
return true; return true;

@ -16,11 +16,11 @@ import org.springframework.stereotype.Component;
import com.luoo.user.annotation.GlobalInterceptor; import com.luoo.user.annotation.GlobalInterceptor;
import com.luoo.user.annotation.VerifyParam; import com.luoo.user.annotation.VerifyParam;
import com.luoo.user.util.StringTools;
import com.luoo.user.util.VerifyUtils; import com.luoo.user.util.VerifyUtils;
import api.StatusCode; import api.StatusCode;
import exception.BizException; import exception.BizException;
import util.StringTools;
@Aspect @Aspect
@Component("operationAspect") @Component("operationAspect")

@ -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);
}
}

@ -97,7 +97,6 @@ public class UserController {
// 得到缓存中的验证码 // 得到缓存中的验证码
String redisKey=Constants.REDIS_KEY_MOBILE_CHECK_CODE+mobile; String redisKey=Constants.REDIS_KEY_MOBILE_CHECK_CODE+mobile;
try { try {
UserRespDTO userRespDTO=new UserRespDTO();
String checkcodeRedis = (String) redisTemplate.opsForValue().get(redisKey); String checkcodeRedis = (String) redisTemplate.opsForValue().get(redisKey);
if (null == checkcodeRedis || checkcodeRedis.isEmpty()) { if (null == checkcodeRedis || checkcodeRedis.isEmpty()) {
return Result.failed(StatusCode.USER_VERIFICATION_CODE_EXPIRED); return Result.failed(StatusCode.USER_VERIFICATION_CODE_EXPIRED);
@ -106,6 +105,7 @@ public class UserController {
return Result.failed(StatusCode.USER_VERIFICATION_CODE_MISMATCH); return Result.failed(StatusCode.USER_VERIFICATION_CODE_MISMATCH);
} }
User user = userService.loginOrRegister(mobile); User user = userService.loginOrRegister(mobile);
UserRespDTO userRespDTO=new UserRespDTO();
BeanUtils.copyProperties(user, userRespDTO); BeanUtils.copyProperties(user, userRespDTO);
String token = jwtUtil.createJWT(user.getId(),user.getNickname(),Constants.TOKEN_ROLE_APP_USER); String token = jwtUtil.createJWT(user.getId(),user.getNickname(),Constants.TOKEN_ROLE_APP_USER);
userRespDTO.setToken(token); userRespDTO.setToken(token);

@ -6,6 +6,8 @@ import java.util.regex.Pattern;
import com.luoo.user.enums.VerifyRegexEnum; import com.luoo.user.enums.VerifyRegexEnum;
import util.StringTools;
/** /**
* @ClassName VerifyUtils * @ClassName VerifyUtils
* @Description TODO * @Description TODO

Loading…
Cancel
Save