mirror of
https://github.com/201206030/novel.git
synced 2025-04-27 07:30:50 +00:00
fix(interceptor): handler 执行异常时没有清理掉当前线程保存的用户登录信息
This commit is contained in:
parent
b5be909415
commit
3c93f90fad
@ -9,14 +9,15 @@ import io.github.xxyopen.novel.core.constant.ApiRouterConsts;
|
|||||||
import io.github.xxyopen.novel.core.constant.SystemConfigConsts;
|
import io.github.xxyopen.novel.core.constant.SystemConfigConsts;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.Map;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.servlet.HandlerInterceptor;
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
import org.springframework.web.servlet.ModelAndView;
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 认证授权 拦截器:为了注入其它的 Spring beans,需要通过 @Component 注解将该拦截器注册到 Spring 上下文
|
* 认证授权 拦截器:为了注入其它的 Spring beans,需要通过 @Component 注解将该拦截器注册到 Spring 上下文
|
||||||
*
|
*
|
||||||
@ -31,6 +32,9 @@ public class AuthInterceptor implements HandlerInterceptor {
|
|||||||
|
|
||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* handle 执行前调用
|
||||||
|
*/
|
||||||
@SuppressWarnings("NullableProblems")
|
@SuppressWarnings("NullableProblems")
|
||||||
@Override
|
@Override
|
||||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
|
||||||
@ -60,12 +64,26 @@ public class AuthInterceptor implements HandlerInterceptor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* handler 执行后调用,出现异常不调用
|
||||||
|
*/
|
||||||
@SuppressWarnings("NullableProblems")
|
@SuppressWarnings("NullableProblems")
|
||||||
@Override
|
@Override
|
||||||
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
|
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
|
||||||
ModelAndView modelAndView) throws Exception {
|
ModelAndView modelAndView) throws Exception {
|
||||||
// 清理当前线程保存的用户数据
|
|
||||||
UserHolder.clear();
|
|
||||||
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
|
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DispatcherServlet 完全处理完请求后调用,出现异常照常调用
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("NullableProblems")
|
||||||
|
@Override
|
||||||
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
|
||||||
|
throws Exception {
|
||||||
|
// 清理当前线程保存的用户数据
|
||||||
|
UserHolder.clear();
|
||||||
|
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,6 @@ import lombok.RequiredArgsConstructor;
|
|||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.web.servlet.HandlerInterceptor;
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
import org.springframework.web.servlet.ModelAndView;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Token 解析拦截器
|
* Token 解析拦截器
|
||||||
@ -23,6 +22,7 @@ public class TokenParseInterceptor implements HandlerInterceptor {
|
|||||||
|
|
||||||
private final JwtUtils jwtUtils;
|
private final JwtUtils jwtUtils;
|
||||||
|
|
||||||
|
@SuppressWarnings("NullableProblems")
|
||||||
@Override
|
@Override
|
||||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
|
||||||
Object handler) throws Exception {
|
Object handler) throws Exception {
|
||||||
@ -35,11 +35,16 @@ public class TokenParseInterceptor implements HandlerInterceptor {
|
|||||||
return HandlerInterceptor.super.preHandle(request, response, handler);
|
return HandlerInterceptor.super.preHandle(request, response, handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DispatcherServlet 完全处理完请求后调用,出现异常照常调用
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("NullableProblems")
|
||||||
@Override
|
@Override
|
||||||
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
|
||||||
ModelAndView modelAndView) throws Exception {
|
throws Exception {
|
||||||
// 清理当前线程保存的用户数据
|
// 清理当前线程保存的用户数据
|
||||||
UserHolder.clear();
|
UserHolder.clear();
|
||||||
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
|
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user