@Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // 获取当前线程的用户信息 Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); String userId = authentication.getName(); String redisKey = "login:" + userId; LoginUser loginUser = JSON.parseObject(redisCache.getCacheObject(redisKey), LoginUser.class); String headerUrl = loginUser.getUser().getHeaderUrl(); System.out.println("获取到的图片路径为:" + headerUrl); if (loginUser != null && modelAndView != null){ modelAndView.addObject("loginUser",loginUser.getUser()); } } 对于以上代码,以下代码要写什么才能清空redis缓存, // 在模板引擎 TemplateEngine 之后执行 @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { }
时间: 2023-12-17 17:02:23 浏览: 102
在 `afterCompletion` 方法中,可以通过 Redis Cache 的 `deleteObject` 方法清空缓存,示例代码如下:
```java
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
// 获取当前线程的用户信息
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String userId = authentication.getName();
String redisKey = "login:" + userId;
redisCache.deleteObject(redisKey); // 清空缓存
}
```
相关问题
@Slf4j public class LoopCallInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //调用接口 // 头部获取当前请求所经过且没结束请求的path列表 // 判定path列表中是否包含当前path //无则正常访问 并记录到列表中 //有则告警循环调用,并终止调用,返回异常 return true; //return HandlerInterceptor.super.preHandle(request, response, handler); } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { //HandlerInterceptor.super.postHandle(request, response, handler, modelAndView); //正常结束接口 //获取头部path列表 //从path列表中删除当前path } }
This is a Java class named `LoopCallInterceptor` that implements the `HandlerInterceptor` interface. The `HandlerInterceptor` interface is part of the Spring MVC framework and provides a way to intercept and handle requests and responses for Spring MVC applications.
The purpose of the `LoopCallInterceptor` class is to prevent circular or recursive calls to a Spring MVC controller method, which can cause infinite loops and consume excessive resources.
The `preHandle` method is called before a request is handled by a controller method. It takes in the `HttpServletRequest` and `HttpServletResponse` objects, as well as the `Object` representing the handler method that will handle the request.
The `preHandle` method first checks if the current request path is already present in the request header path list. If it is not present, it adds the current request path to the path list and returns `true` to indicate that the request can proceed normally. If it is already present, it logs a warning message for the circular or recursive call and returns `false` to indicate that the request should be stopped.
The `postHandle` method is called after a request has been handled by a controller method. It takes in the `HttpServletRequest`, `HttpServletResponse`, `Object` representing the handler method, and the `ModelAndView` object that contains the view and model data returned by the handler method.
The `postHandle` method removes the current request path from the request header path list.
Note that the `HandlerInterceptor` interface has three methods, but the `LoopCallInterceptor` class only implements the `preHandle` and `postHandle` methods. The `afterCompletion` method is called after the response has been rendered, but before the response has been committed.
public class UserAccessInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // String token = TokenHelper.getFromReq(request); String token = request.getHeader(Authentication.TOKEN_NAME); if (StringUtils.isEmpty(token)) { // mute for OPTIONS if (StringUtils.isEmpty(token)) { // mute for OPTIONS if (request.getMethod().equalsIgnoreCase(HttpMethod.OPTIONS.name())) { return true; } // need log return false; } } UserContext userContext = Authentication.verify(token); if (userContext == null) { log.error(""); return false; } UserContextHolder.set(userContext); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { HandlerInterceptor.super.postHandle(request, response, handler, modelAndView); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { HandlerInterceptor.super.afterCompletion(request, response, handler, ex); } }解释一下这个类中方法用处
这个类是一个拦截器(Interceptor),用于在用户访问请求被处理前进行一些预处理操作。它实现了HandlerInterceptor接口,并重写了其中的三个方法。
1. preHandle方法:
这个方法在请求被处理之前被调用。它接收HttpServletRequest、HttpServletResponse和Object类型的参数。preHandle方法返回一个boolean值,表示是否继续执行后续的处理流程。
在这个方法中,首先从请求头中获取token,然后判断token是否为空。如果为空,还会进一步判断请求方法是否为OPTIONS,如果是则返回true,表示允许跨域请求的预检请求通过;否则,返回false,表示需要进行记录日志等操作。
如果token不为空,会调用Authentication.verify(token)方法进行验证。如果验证通过,将用户信息存入UserContextHolder中,并返回true;否则,记录错误日志,并返回false。
2. postHandle方法:
这个方法在请求被处理之后、视图渲染之前调用。它接收HttpServletRequest、HttpServletResponse、Object和ModelAndView类型的参数。postHandle方法没有返回值。
在这个方法中,可以对响应进行一些处理操作,比如设置响应头、添加响应结果等。
3. afterCompletion方法:
这个方法在整个请求处理完成后调用,包括视图渲染完毕。它接收HttpServletRequest、HttpServletResponse、Object和Exception类型的参数。afterCompletion方法没有返回值。
在这个方法中,可以进行一些清理操作,比如释放资源、记录请求处理时间等。
总结:UserAccessInterceptor类中的preHandle方法用于在请求被处理前进行预处理,postHandle和afterCompletion方法用于在请求处理完成后进行后续操作。这个类可以用于实现用户访问权限验证、日志记录等功能。
阅读全文