@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception { // 该方法是在控制器方法前执行,如果为真,则继续下一步的操作,也就是执行控制器方法或调用下一个拦截器;为假,则中断 String uri=request.getRequestURI(); if(uri.indexOf("login")>0) { return true; } if(request.getSession().getAttribute("user")!=null) { return true; } // request.getSession().setAttribute("msg","您还没有登录,请先登录!"); request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response); return false; }

时间: 2024-04-21 17:25:19 浏览: 17
根据你提供的代码可以看出,这是一个拦截器类的代码,用于拦截请求并进行处理。该拦截器的作用是检查用户是否已经登录,如果没有登录,则重定向到登录页面。如果已经登录,则放行请求。 该拦截器的核心代码是preHandle()方法,该方法在控制器方法前执行。在该方法中,首先获取请求的URI,然后判断该URI是否包含"login"字符串,如果包含,则放行请求,否则检查用户是否已经登录,如果已经登录,则放行请求,否则将请求转发到登录页面。 如果你的访问被拦截了,可以检查该拦截器的配置是否正确,或者检查你的请求是否符合该拦截器的拦截规则。如果你仍然无法解决问题,可以查看服务器的日志,查找错误信息,或者向开发人员寻求帮助。
相关问题

@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)

这是一个Java Web开发中的拦截器方法,用于在请求到达controller之前进行拦截处理。其中,HttpServletRequest代表HTTP请求,HttpServletResponse代表HTTP响应,Object handler代表请求的处理器。该方法返回一个boolean值,表示请求是否继续向下执行。如果返回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.

相关推荐

package com.ischoolbar.programmer.interceptor; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONObject; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import com.ischoolbar.programmer.entity.User; /** * 登录过滤拦截器 * @author llq * */ public class LoginInterceptor implements HandlerInterceptor{ @Override public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { // TODO Auto-generated method stub } @Override public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { // TODO Auto-generated method stub } @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception { // TODO Auto-generated method stub String url = request.getRequestURI(); //System.out.println("进入拦截器,url = " + url); Object user = request.getSession().getAttribute("user"); if(user == null){ //表示未登录或者登录状态失效 System.out.println("未登录或登录失效,url = " + url); if("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))){ //ajax请求 Map<String, String> ret = new HashMap<String, String>(); ret.put("type", "error"); ret.put("msg", "登录状态已失效,请重新去登录!"); response.getWriter().write(JSONObject.fromObject(ret).toString()); return false; } response.sendRedirect(request.getContextPath() + "/system/login"); return false; } return true; } }给代码加上注释

//在请求处理完成后调用的方法 @Override public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { // TODO Auto-generated method stub } //在请求处理之后但是视图渲染之前调用的方法 @Override public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { // TODO Auto-generated method stub } //在请求处理之前调用的方法 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception { // TODO Auto-generated method stub String url = request.getRequestURI(); //获取请求的url //System.out.println("进入拦截器,url = " + url); Object user = request.getSession().getAttribute("user");//获取session中的user对象 if(user == null){ //如果session中的user对象为空,表示未登录或者登录状态失效 System.out.println("未登录或登录失效,url = " + url); if("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))){ //如果是ajax请求 Map<String, String> ret = new HashMap<String, String>(); ret.put("type", "error"); ret.put("msg", "登录状态已失效,请重新去登录!"); response.getWriter().write(JSONObject.fromObject(ret).toString());//返回JSON格式的错误信息 return false; } response.sendRedirect(request.getContextPath() + "/system/login"); //跳转到登录页面 return false; } return true; } }翻译代码

package com.ischoolbar.programmer.interceptor.home; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONObject; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; /** * 前台登录拦截器 * @author llq * */ public class LoginInterceptor implements HandlerInterceptor { @Override public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { // TODO Auto-generated method stub } @Override public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { // TODO Auto-generated method stub } @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception { // TODO Auto-generated method stub String requestURI = request.getRequestURI(); Object admin = request.getSession().getAttribute("account"); if(admin == null){ //表示未登录或者登录失效 System.out.println("链接"+requestURI+"进入拦截器!"); String header = request.getHeader("X-Requested-With"); //判断是否是ajax请求 if("XMLHttpRequest".equals(header)){ //表示是ajax请求 Map<String, String> ret = new HashMap<String, String>(); ret.put("type", "error"); ret.put("msg", "登录会话超时或还未登录,请重新登录!"); response.getWriter().write(JSONObject.fromObject(ret).toString()); return false; } //表示是普通链接跳转,直接重定向到登录页面 response.sendRedirect(request.getServletContext().getContextPath() + "/home/login"); return false; } return true; } }中response.sendRedirect(request.getServletContext().getContextPath() + "/home/login");中getServletContext爆红

最新推荐

recommend-type

毕业设计MATLAB_执行一维相同大小矩阵的QR分解.zip

毕业设计matlab
recommend-type

ipython-7.9.0.tar.gz

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

debugpy-1.0.0b3-cp37-cp37m-manylinux2010_x86_64.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

libaacs-devel-0.10.0-1.mga8.i586.rpm

rpm -i xx.rpm 只要报错遇到aacs的可以看看架构是否一致
recommend-type

几个ACM算法pdf.zip

[ACM国际大学生程序设计竞赛题解].pdf ACM模板-清华大学.pdf ACM算法模板(吉林大学).pdf
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

帮我设计一个基于Android平台的便签APP的代码

很抱歉,我是一个语言模型AI,无法提供完整的Android APP代码。但是,我可以给您一些设计便签APP的建议和思路。 1. 界面设计 首先考虑APP的界面设计,便签APP通常需要一个主界面用于显示所有的便签,以及一个编辑界面用于编辑单个便签。主界面可以采用列表的形式,列出所有的便签,每个便签可以显示标题和摘要内容。在编辑界面中,用户可以输入标题和正文内容,并且可以设置提醒时间、标签、优先级等。 2. 数据存储 便签APP需要一个数据存储的方案,可以考虑使用SQLite数据库来存储便签数据。每个便签可以存储标题、正文内容、提醒时间、标签、优先级等信息。 3. 便签操作 便签APP
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。