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

Added login expiration reminder. Add logout reminder.

This commit is contained in:
2023-05-08 09:37:13 +08:00
parent 219f8cca3d
commit 881be1b0f9
15 changed files with 138 additions and 70 deletions

View File

@@ -92,6 +92,10 @@
<artifactId>java-jwt</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>

View File

@@ -5,6 +5,7 @@ import com.cfive.pinnacle.entity.common.ResponseCode;
import com.cfive.pinnacle.entity.common.ResponseResult;
import com.cfive.pinnacle.service.permission.ILoginService;
import com.cfive.pinnacle.utils.WebUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@@ -28,8 +29,8 @@ public class LoginController {
}
@RequestMapping("/logout")
public ResponseResult logout() {
boolean result = loginService.logout();
public ResponseResult logout(HttpServletRequest request) {
boolean result = loginService.logout(request.getHeader("token"));
if (result) {
return ResponseResult.build(ResponseCode.LOGOUT_SUCCESS, "Logout Success", null);
} else {

View File

@@ -10,6 +10,8 @@ public class ResponseCode {
public static final int LOGIN_USERNAME_PASSWORD_ERROR = 20011;
public static final int LOGOUT_SUCCESS = 20015;
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 DATABASE_SELECT_OK = 20021;
public static final int DATABASE_SAVE_OK = 20022;
public static final int DATABASE_UPDATE_OK = 20023;

View File

@@ -1,10 +1,12 @@
package com.cfive.pinnacle.filter;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.cfive.pinnacle.entity.common.ResponseCode;
import com.cfive.pinnacle.entity.permission.LoginUser;
import com.cfive.pinnacle.utils.JwtUtil;
import com.cfive.pinnacle.utils.RedisCache;
import com.cfive.pinnacle.utils.WebUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Nonnull;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
@@ -29,26 +31,29 @@ public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
protected void doFilterInternal(HttpServletRequest request, @Nonnull HttpServletResponse response, @Nonnull FilterChain filterChain) throws ServletException, IOException {
String token = request.getHeader("token");
if (!StringUtils.hasText(token)) {
filterChain.doFilter(request, response);
return;
}
String userId;
try {
DecodedJWT decodedJWT = JwtUtil.parseJWT(token);
userId = decodedJWT.getSubject();
JwtUtil.parseJWT(token);
} catch (Exception e) {
throw new RuntimeException("Token is illegal");
String objectResponse = WebUtil.objectResponse(ResponseCode.TOKEN_IS_ILLEGAL, "Token is illegal", null);
WebUtil.renderString(response, objectResponse);
return;
}
String redisKey = "login:" + userId;
String redisKey = "login:" + token;
LoginUser loginUser = new ObjectMapper().convertValue(redisCache.getCacheObject(redisKey), LoginUser.class);
if (Objects.isNull(loginUser)) {
throw new RuntimeException("Not logged in");
String objectResponse = WebUtil.objectResponse(ResponseCode.TOKEN_HAS_EXPIRED, "Token has expired", null);
WebUtil.renderString(response, objectResponse);
return;
}
// Todo 权限
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginUser, null, null);
SecurityContextHolder.getContext().setAuthentication(authenticationToken);

View File

@@ -18,7 +18,7 @@ public class AuthenticationEntryPointHandler implements AuthenticationEntryPoint
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
String objectResponse;
if (authException instanceof BadCredentialsException) {
objectResponse = WebUtil.objectResponse(ResponseCode.LOGOUT_FAILED, authException.getMessage(), null);
objectResponse = WebUtil.objectResponse(ResponseCode.LOGIN_USERNAME_PASSWORD_ERROR, authException.getMessage(), null);
} else if (authException instanceof InsufficientAuthenticationException) {
objectResponse = WebUtil.objectResponse(ResponseCode.UNAUTHORIZED, authException.getMessage(), null);
} else {

View File

@@ -7,5 +7,5 @@ import java.util.HashMap;
public interface ILoginService {
HashMap<String, String> login(User user);
boolean logout();
boolean logout(String token);
}

View File

@@ -9,11 +9,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
@Service
public class LoginServiceImpl implements ILoginService {
@@ -46,17 +46,13 @@ public class LoginServiceImpl implements ILoginService {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("token", jwt);
redisCache.setCacheObject("login:" + userId, loginUser);
redisCache.setCacheObject("login:" + jwt, loginUser, 10, TimeUnit.MINUTES);
return hashMap;
}
@Override
public boolean logout() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
Long userId = loginUser.getUser().getId();
return redisCache.deleteObject("login:" + userId);
public boolean logout(String token) {
return redisCache.deleteObject("login:" + token);
}
}

View File

@@ -1,6 +1,5 @@
package com.cfive.pinnacle.utils;
import com.cfive.pinnacle.entity.User;
import com.cfive.pinnacle.entity.common.ResponseResult;
import com.cfive.pinnacle.entity.permission.LoginUser;
import com.fasterxml.jackson.core.JsonProcessingException;