1
0
mirror of https://github.com/FatttSnake/Pinnacle-OA.git synced 2026-04-05 23:11:24 +08:00

Added token automatic renew

This commit is contained in:
2023-06-02 02:33:05 +08:00
parent 1b186a8e9e
commit 1b6ac6ade8
9 changed files with 58 additions and 11 deletions

View File

@@ -28,7 +28,7 @@ public class LoginController {
@PostMapping("/login")
public ResponseResult<HashMap<String, String>> login(@RequestBody User user) {
HashMap<String, String> hashMap = loginService.login(user);
return ResponseResult.build(ResponseCode.LOGIN_SUCCESS, "Login Success", hashMap);
return ResponseResult.build(ResponseCode.LOGIN_SUCCESS, "Login success", hashMap);
}
@Operation(summary = "登出")
@@ -36,9 +36,17 @@ public class LoginController {
public ResponseResult<?> logout(HttpServletRequest request) {
boolean result = loginService.logout(request.getHeader("token"));
if (result) {
return ResponseResult.build(ResponseCode.LOGOUT_SUCCESS, "Logout Success", null);
return ResponseResult.build(ResponseCode.LOGOUT_SUCCESS, "Logout success", null);
} else {
return ResponseResult.build(ResponseCode.LOGOUT_FAILED, "Logout Failed", null);
return ResponseResult.build(ResponseCode.LOGOUT_FAILED, "Logout failed", null);
}
}
@Operation(summary = "更新 Token")
@GetMapping("/token")
public ResponseResult<HashMap<String, String >> renewToken(HttpServletRequest request) {
String token = request.getHeader("token");
HashMap<String, String> hashMap = loginService.renewToken(token);
return ResponseResult.build(ResponseCode.TOKEN_RENEW_SUCCESS, "Token renew success", hashMap);
}
}

View File

@@ -12,6 +12,7 @@ public class ResponseCode {
public static final int LOGOUT_FAILED = 20016;
public static final int TOKEN_IS_ILLEGAL = 20017;
public static final int TOKEN_HAS_EXPIRED = 20018;
public static final int TOKEN_RENEW_SUCCESS = 20019;
public static final int DATABASE_SELECT_OK = 20021;
public static final int DATABASE_SAVE_OK = 20022;
public static final int DATABASE_UPDATE_OK = 20023;

View File

@@ -19,6 +19,7 @@ import org.springframework.web.filter.OncePerRequestFilter;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
@Component
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
@@ -45,6 +46,7 @@ public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
if (Objects.isNull(loginUser)) {
throw new TokenHasExpiredException();
}
redisCache.expire(redisKey, 20, TimeUnit.MINUTES);
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginUser, null, loginUser.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authenticationToken);

View File

@@ -8,4 +8,6 @@ public interface ILoginService {
HashMap<String, String> login(User user);
boolean logout(String token);
HashMap<String, String> renewToken(String token);
}

View File

@@ -5,6 +5,7 @@ import com.cfive.pinnacle.entity.permission.LoginUser;
import com.cfive.pinnacle.service.permission.ILoginService;
import com.cfive.pinnacle.utils.JwtUtil;
import com.cfive.pinnacle.utils.RedisCache;
import com.cfive.pinnacle.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@@ -46,7 +47,7 @@ public class LoginServiceImpl implements ILoginService {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("token", jwt);
redisCache.setCacheObject("login:" + jwt, loginUser, 30, TimeUnit.MINUTES);
redisCache.setCacheObject("login:" + jwt, loginUser, 20, TimeUnit.MINUTES);
return hashMap;
}
@@ -55,4 +56,15 @@ public class LoginServiceImpl implements ILoginService {
public boolean logout(String token) {
return redisCache.deleteObject("login:" + token);
}
@Override
public HashMap<String, String> renewToken(String token) {
String oldRedisKey = "login:" + token;
redisCache.deleteObject(oldRedisKey);
String jwt = JwtUtil.createJWT(WebUtil.getLoginUser().getUser().getId().toString());
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("token", jwt);
redisCache.setCacheObject("login:" + jwt, WebUtil.getLoginUser(), 20, TimeUnit.MINUTES);
return hashMap;
}
}

View File

@@ -17,7 +17,7 @@ import java.util.UUID;
public class JwtUtil {
// 有效期
public static final Long JWT_TTL = 60 * 60 * 1000L; // 60 * 60 * 1000 个小时
public static final Long JWT_TTL = 2 * 60 * 60 * 1000L; // 2 * 60 * 60 * 1000 个小时
// 秘钥明文
public static final String JWT_KEY = "pinnacle";
//签发者