Merge pull request '双token' (#6) from czk into main

Reviewed-on: #6
main
czk 2024-04-29 10:10:12 +08:00
commit 19b4852916
7 changed files with 53 additions and 14 deletions

View File

@ -1,16 +1,21 @@
package com.mall.auth.controller; package com.mall.auth.controller;
import com.mall.auth.service.AuthService; import com.mall.auth.service.AuthService;
import com.mall.common.constant.JwtConstants;
import com.mall.common.constant.TokenConstants;
import com.mall.common.domain.UserInfo; import com.mall.common.domain.UserInfo;
import com.mall.common.domain.request.LoginRequest; import com.mall.common.domain.request.LoginRequest;
import com.mall.common.domain.vo.LoginVo; import com.mall.common.domain.vo.LoginVo;
import com.mall.common.domain.vo.UserInfoVo; import com.mall.common.domain.vo.UserInfoVo;
import com.mall.common.result.Result; import com.mall.common.result.Result;
import com.mall.common.utils.IdUtils;
import com.mall.common.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.HashMap;
/** /**
* @Author: lzh * @Author: lzh
@ -77,4 +82,9 @@ public class AuthController {
Result<UserInfo> result = authService.userInfo(); Result<UserInfo> result = authService.userInfo();
return result; return result;
} }
@GetMapping("refreshToken")
public Result refreshToken(@RequestParam String refreshToken){
return authService.refreshToken(refreshToken);
}
} }

View File

@ -16,4 +16,7 @@ public interface AuthService {
Result phoneRegister(UserInfoVo userInfo); Result phoneRegister(UserInfoVo userInfo);
Result<UserInfo> userInfo(); Result<UserInfo> userInfo();
Result refreshToken(String refreshToken);
} }

View File

@ -55,19 +55,20 @@ public class AuthServiceImpl implements AuthService {
Assert.notNull( Assert.notNull(
data,"用户不存在" data,"用户不存在"
); );
// String password = SecureUtil.md5(
// loginVo.getPassword()+ "|" + data.getSalt()
// );
if (!loginVo.getPassword().equals(data.getPassword())){ if (!loginVo.getPassword().equals(data.getPassword())){
throw new BizException("密码错误") ; throw new BizException("密码错误") ;
} }
HashMap<String, Object> map = new HashMap<>(); HashMap<String, Object> map = new HashMap<>();
map.put(JwtConstants.USER_KEY,data.getId()); map.put(JwtConstants.USER_KEY,data.getId());
String token = JwtUtils.createToken(map); String token = JwtUtils.createToken(map);
redisCache.setCacheObject(TokenConstants.LOGIN_TOKEN_KEY+token,data,TokenConstants.EXPIRATION, TimeUnit.HOURS); String refreshToken = JwtUtils.createToken(map);
redisCache.setCacheObject(TokenConstants.TOKEN+token,data,TokenConstants.EXPIRATION, TimeUnit.HOURS);
redisCache.setCacheObject(TokenConstants.REFRESH_TOKEN+refreshToken,data,TokenConstants.REFRESH_TIME,TimeUnit.HOURS);
JwtResponse jwtResponse = new JwtResponse(); JwtResponse jwtResponse = new JwtResponse();
jwtResponse.setToken(token); jwtResponse.setToken(token);
jwtResponse.setEndTim(TokenConstants.EXPIRATION); jwtResponse.setEndTime(TokenConstants.EXPIRATION);
jwtResponse.setRefreshToken(refreshToken);
jwtResponse.setRefreshEndTime(TokenConstants.REFRESH_TIME);
return Result.success(jwtResponse); return Result.success(jwtResponse);
} }
@ -110,10 +111,10 @@ public class AuthServiceImpl implements AuthService {
String userKey = IdUtils.genId(); String userKey = IdUtils.genId();
map.put(JwtConstants.USER_KEY,userKey); map.put(JwtConstants.USER_KEY,userKey);
String token = JwtUtils.createToken(map); String token = JwtUtils.createToken(map);
redisCache.setCacheObject(TokenConstants.LOGIN_TOKEN_KEY+token,data,TokenConstants.EXPIRATION, TimeUnit.HOURS); redisCache.setCacheObject(TokenConstants.TOKEN+token,data,TokenConstants.EXPIRATION, TimeUnit.MINUTES);
JwtResponse jwtResponse = new JwtResponse(); JwtResponse jwtResponse = new JwtResponse();
jwtResponse.setToken(token); jwtResponse.setToken(token);
jwtResponse.setEndTim(TokenConstants.EXPIRATION); jwtResponse.setEndTime(TokenConstants.EXPIRATION);
return Result.success(jwtResponse); return Result.success(jwtResponse);
} }
@ -149,11 +150,21 @@ public class AuthServiceImpl implements AuthService {
@Override @Override
public Result<UserInfo> userInfo() { public Result<UserInfo> userInfo() {
String token = request.getHeader(TokenConstants.LOGIN_TOKEN_KEY); String token = request.getHeader(TokenConstants.TOKEN);
if (StringUtils.isBlank(token)){ if (StringUtils.isBlank(token)){
throw new BizException(401,"未登录"); throw new BizException(401,"未登录");
} }
UserInfo userInfo = redisCache.getCacheObject(TokenConstants.LOGIN_TOKEN_KEY + token); UserInfo userInfo = redisCache.getCacheObject(TokenConstants.TOKEN + token);
return Result.success(userInfo); return Result.success(userInfo);
} }
@Override
public Result refreshToken(String refreshToken) {
UserInfo userInfo = redisCache.getCacheObject(TokenConstants.REFRESH_TOKEN + refreshToken);
HashMap<String, Object> map = new HashMap<>();
map.put(JwtConstants.USER_KEY, IdUtils.genId());
String token = JwtUtils.createToken(map);
redisCache.setCacheObject(TokenConstants.TOKEN+token,userInfo,TokenConstants.EXPIRATION,TimeUnit.MINUTES);
return Result.success(token);
}
} }

View File

@ -16,9 +16,13 @@ public class TokenConstants {
/** /**
* *
*/ */
public final static String LOGIN_TOKEN_KEY = "login_tokens:"; public final static String LOGIN_TOKEN_KEY = "login_tokens";
/** /**
* token * token
*/ */
public static final String TOKEN = "token"; public static final String TOKEN = "token";
/**
* refreshToken
*/
public static final String REFRESH_TOKEN = "refresh_token";
} }

View File

@ -17,5 +17,9 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor @NoArgsConstructor
public class JwtResponse { public class JwtResponse {
private String token; private String token;
private Long endTim; private Long endTime;
private String refreshToken;
private Long refreshEndTime;
} }

View File

@ -1,6 +1,9 @@
package com.mall.gateway.filters; package com.mall.gateway.filters;
import com.mall.common.constant.JwtConstants;
import com.mall.common.constant.TokenConstants; import com.mall.common.constant.TokenConstants;
import com.mall.common.domain.UserInfo;
import com.mall.common.redis.RedisCache; import com.mall.common.redis.RedisCache;
import com.mall.common.utils.IdUtils;
import com.mall.common.utils.JwtUtils; import com.mall.common.utils.JwtUtils;
import com.mall.common.utils.StringUtils; import com.mall.common.utils.StringUtils;
import com.mall.gateway.config.IgnoreWhiteConfig; import com.mall.gateway.config.IgnoreWhiteConfig;
@ -18,6 +21,10 @@ import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/** /**
* @description: * @description:
* @author DongZl * @author DongZl
@ -58,7 +65,7 @@ public class AuthFilter implements GlobalFilter, Ordered {
Claims claims = JwtUtils.parseToken(token); Claims claims = JwtUtils.parseToken(token);
if (claims == null) { if (claims == null) {
return GatewayUtils.errorResponse(exchange, "令牌已过期或验证不正确!"); return GatewayUtils.errorResponse(exchange, "验证不正确!");
} }
boolean login = redisCache.hasKey(TokenConstants.TOKEN + token); boolean login = redisCache.hasKey(TokenConstants.TOKEN + token);

View File

@ -206,11 +206,11 @@ public class SpikesServiceImpl extends ServiceImpl<SpikesMapper, SpikesEntity>
} }
public UserInfo getLogin(){ public UserInfo getLogin(){
String token = request.getHeader(TokenConstants.LOGIN_TOKEN_KEY); String token = request.getHeader(TokenConstants.TOKEN);
if (StringUtils.isBlank(token)){ if (StringUtils.isBlank(token)){
throw new BizException(401,"未登录"); throw new BizException(401,"未登录");
} }
UserInfo userInfo = redisCache.getCacheObject(TokenConstants.LOGIN_TOKEN_KEY + token); UserInfo userInfo = redisCache.getCacheObject(TokenConstants.TOKEN + token);
return userInfo; return userInfo;
} }