1.fix autologin issue

main
Gary 1 year ago
parent 390ddcde05
commit 247bcce796

@ -89,7 +89,8 @@ public class OperationAspect {
String ip = getIpAddr(request); String ip = getIpAddr(request);
ip = ip.replace(":", ""); ip = ip.replace(":", "");
String redisKey = fullMethodName + ip; 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) { if (cuCount != null && cuCount > threshold - 1) {
throw new BizException(StatusCode.REQUEST_TOO_FREQUENCY); throw new BizException(StatusCode.REQUEST_TOO_FREQUENCY);
} }

@ -90,7 +90,7 @@ public class UserController {
return Result.success(map); return Result.success(map);
} }
@ApiOperation(value = "2.登录/注册后返回token") @ApiOperation(value = "2.登录/注册",notes="成功后返回用户信息和token")
@PostMapping("/appLogin/{mobile}/{mobileCheckCode}") @PostMapping("/appLogin/{mobile}/{mobileCheckCode}")
@GlobalInterceptor @GlobalInterceptor
public Result appLogin( @PathVariable @VerifyParam(required=true,regex=VerifyRegexEnum.MOBILE)String mobile, 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次请求") @ApiOperation(value = "1.发送短信验证码", notes = "有效期15分钟一个手机号一天最多发50次请求")
@PostMapping("/sendsms/{mobile}") @PostMapping("/sendsms/{mobile}")
@GlobalInterceptor(checkLogin = true, frequencyType = RequestFrequencyTypeEnum.DAY, requestFrequencyThreshold = 50) @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.DAY, requestFrequencyThreshold = 50)
public Result<Void> sendSms(@PathVariable @VerifyParam(required=true,regex=VerifyRegexEnum.MOBILE)String mobile) { public Result<Void> sendSms(@PathVariable @VerifyParam(required=true,regex=VerifyRegexEnum.MOBILE)String mobile) {
userService.sendSms(mobile); userService.sendSms(mobile);
return Result.success(); return Result.success();
@ -144,17 +144,17 @@ public class UserController {
/** /**
* token * token
*/ */
@ApiOperation(value = "5.token 续期,有效期7天") @ApiOperation(value = "5.token续期", notes = "有效期7天")
@PostMapping("/autoLogin/{token}") @PostMapping("/autoLogin")
@GlobalInterceptor @GlobalInterceptor
public Result<String> autoLogin(@PathVariable @VerifyParam(required = true) String token) { public Result<String> autoLogin(@RequestHeader(value = "token", required = false) String token) {
return Result.success(userService.autoLogin(token)); return Result.success(userService.autoLogin(token));
} }
/** /**
* *
*/ */
@ApiOperation(value = "4.三次短信验证失败后获取图形验证码有效期10分钟") @ApiOperation(value="4.获取图形验证码", notes = "4.三次短信验证失败后获取图形验证码有效期10分钟")
@GetMapping("/imageCheckCode/{mobile}") @GetMapping("/imageCheckCode/{mobile}")
@GlobalInterceptor @GlobalInterceptor
public void imageCheckCode(HttpServletResponse response, public void imageCheckCode(HttpServletResponse response,
@ -179,18 +179,15 @@ public class UserController {
) { ) {
// 得到缓存中的验证码 // 得到缓存中的验证码
String redisImageCheckCodeKey=Constants.REDIS_KEY_IMAGE_CHECK_CODE+mobile; String redisImageCheckCodeKey=Constants.REDIS_KEY_IMAGE_CHECK_CODE+mobile;
try { String redisImageCheckCode = (String) redisTemplate.opsForValue().get(redisImageCheckCodeKey);
String redisImageCheckCode = (String) redisTemplate.opsForValue().get(redisImageCheckCodeKey); if (StringTools.isEmpty(redisImageCheckCode)) {
if (StringTools.isEmpty(redisImageCheckCode)) { return Result.failed(StatusCode.USER_IMAGE_VERIFICATION_CODE_EXPIRED);
return Result.failed(StatusCode.USER_IMAGE_VERIFICATION_CODE_EXPIRED); }
} if (!redisImageCheckCode.equalsIgnoreCase(imageCheckCode)) {
if (!redisImageCheckCode.equals(imageCheckCode)) { return Result.failed(StatusCode.USER_IMAGE_VERIFICATION_CODE_MISMATCH);
return Result.failed(StatusCode.USER_IMAGE_VERIFICATION_CODE_MISMATCH);
}
return appLogin(mobile,mobileCheckCode);
}finally {
redisTemplate.delete(redisImageCheckCodeKey);
} }
redisTemplate.delete(redisImageCheckCodeKey);
return appLogin(mobile,mobileCheckCode);
} }
/* /*

@ -33,6 +33,7 @@ import com.luoo.user.util.NickNameUtil;
import api.Result; import api.Result;
import api.StatusCode; import api.StatusCode;
import dto.UserLoginDto;
import io.jsonwebtoken.Claims; import io.jsonwebtoken.Claims;
import util.JwtUtil; import util.JwtUtil;
@ -288,15 +289,13 @@ public class UserService {
public String autoLogin(String token) { public String autoLogin(String token) {
try { try {
Claims claims = jwtUtil.parseJWT(token); UserLoginDto userLoginDto=jwtUtil.getUserLoginDto(token);
String userId = claims.getId(); if(Constants.TOKEN_ROLE_APP_USER.equals(userLoginDto.getRoles())){
if(Constants.TOKEN_ROLE_APP_USER.equals((String) claims.get(Constants.TOKEN_ROLE))){ User user = userDao.findById(userLoginDto.getUserId()).get();
User user = userDao.findById(userId).orElse(null);
user.setLastdate(new Date()); user.setLastdate(new Date());
userDao.save(user); userDao.save(user);
} }
return jwtUtil.createJWT(userLoginDto.getUserId(), userLoginDto.getNickName(), userLoginDto.getRoles());
return jwtUtil.createJWT(userId, claims.getSubject(), Constants.TOKEN_ROLE_APP_USER);
} catch (Exception e) { } catch (Exception e) {
return null; return null;
} }

Loading…
Cancel
Save