public class AdminInterceptor implements HandlerInterceptor { /** * 在请求处理之前进行调用(Controller方法调用之前) */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { try { //统一拦截(查询当前session是否存在user)(这里user会在每次登陆成功后,写入session) String user=(String) request.getSession().getAttribute("userid"); if(user!=null){ return true; } response.sendRedirect(request.getContextPath()+"/"); } catch (IOException e) { e.printStackTrace(); } return false;//如果设置为false时,被请求时,拦截器执行到此处将不会继续操作 //如果设置为true时,请求将会继续执行后面的操作 }
时间: 2024-02-15 08:28:12 浏览: 65
这是一个 Java 的拦截器类,实现了 HandlerInterceptor 接口。它的作用是在 Controller 方法被调用之前拦截请求,检查当前 session 是否存在名为 "userid" 的属性,如果存在则返回 true,否则重定向到应用的根路径。如果返回 false,拦截器将不会继续执行后续操作。该拦截器通常用于验证用户的登录状态,以确保用户已经登录才能访问应用的某些功能。
相关问题
@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.
修改一下这个inceptor,我要求查看请求访问”/user/api"的用户的role_id是否为2(通过token),否则拒绝访问:public class MyInceptor implements HandlerInterceptor { // 处理指定接口 private static final String TOKEN_LOGIN = "/user/token"; private static final String USER_NO_URL = "/user/no"; // 拦截不存在的接口路径 // 不要要校验的接口 private static final List<String> NO_NEED_LOGIN = new ArrayList<>(Arrays.asList("/user/health", "/logout")); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 在请求处理之前执行的操作 System.out.println("=====preHandle====="); return true; // 返回true表示继续处理请求,返回false表示终止请求处理 } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // 在请求处理之后,视图渲染之前执行的操作 System.out.println("=====postHandle====="); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // 在请求完成之后执行的操作,即视图渲染完成后 System.out.println("=====afterCompletion====="); } }
好的,您可以按照以下方式修改 MyInceptor 类的 preHandle 方法来实现您的要求:
```java
public class MyInceptor implements HandlerInterceptor {
// 处理指定接口
private static final String TOKEN_LOGIN = "/user/token";
private static final String USER_NO_URL = "/user/no"; // 拦截不存在的接口路径
// 不需要校验的接口
private static final List<String> NO_NEED_LOGIN = new ArrayList<>(Arrays.asList("/user/health", "/logout"));
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// 在请求处理之前执行的操作
System.out.println("=====preHandle=====");
// 获取请求的路径
String requestURI = request.getRequestURI();
// 如果请求的路径是 /user/api,则需要进行校验
if ("/user/api".equals(requestURI)) {
// 获取请求头中的 token
String token = request.getHeader("token");
// 判断 token 是否有效,如果无效则拒绝访问
if (!isValidToken(token)) {
response.sendError(401, "Unauthorized");
return false;
}
// 获取 token 中的 role_id
int roleId = getRoleIdFromToken(token);
// 判断 role_id 是否为 2,如果不是则拒绝访问
if (roleId != 2) {
response.sendError(403, "Forbidden");
return false;
}
}
return true; // 返回true表示继续处理请求,返回false表示终止请求处理
}
private boolean isValidToken(String token) {
// TODO: 判断 token 是否有效
return true;
}
private int getRoleIdFromToken(String token) {
// TODO: 从 token 中获取 role_id
return 0;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// 在请求处理之后,视图渲染之前执行的操作
System.out.println("=====postHandle=====");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
Exception ex) throws Exception {
// 在请求完成之后执行的操作,即视图渲染完成后
System.out.println("=====afterCompletion=====");
}
}
```
在 preHandle 方法中,首先判断请求的路径是否为 /user/api。如果是,则获取请求头中的 token,并判断其是否有效。如果 token 无效,则返回 401 错误(未授权)。如果 token 有效,则从中获取 role_id,并判断其是否为 2。如果 role_id 不为 2,则返回 403 错误(禁止访问)。如果请求的路径不是 /user/api,则直接返回 true,继续处理请求。
阅读全文