拦截异常登录

news/2024/7/10 22:55:53 标签: jsp, interceptor

一、拦截jsp
创建InterceptorJspFilter类实现Filter接口,重写doFilter

@WebFilter("*.jsp")
public class InterceptorJspFilter implements Filter{

	public void destroy() {
		// TODO Auto-generated method stub
		
	}
	
	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain chain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest  httpRequest = (HttpServletRequest)arg0;
		HttpServletResponse httptResponse = (HttpServletResponse) arg1;
		String uri = httpRequest.getRequestURI();
		UserEntity userEntity = (UserEntity) httpRequest.getSession().getAttribute("user");
		if(userEntity!=null){
			chain.doFilter(httpRequest, httptResponse);
			
		}
		//包含登录界面就不拦截
		else if (uri.contains("login.jsp") {
			chain.doFilter(httpRequest, httptResponse);
		}
		else {
			try {
				ThisAssection.getThisRuntimeException("用户非法登录已打回登录界面");
				
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				httptResponse.sendRedirect("troIndex.jsp");
			}
			
		}
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}

拦截controller
springmvc配置拦截器

public class InterceptHandlerController implements HandlerInterceptor{
	public List<String> excludeUrls;
	
	public List<String> getExcludeUrls() {
		return excludeUrls;
	}

	public void setExcludeUrls(List<String> excludeUrls) {
		this.excludeUrls = excludeUrls;
	}

	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
		
	}

	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
		// TODO Auto-generated method stub
		arg0.setCharacterEncoding("utf-8");
		arg1.setCharacterEncoding("utf-8");
		UserEntity userEntity = (UserEntity) arg0.getSession().getAttribute("user");
		String uri = arg0.getRequestURI();
		for(String str:excludeUrls){
			if(uri.contains(str))
				return true;
		}
		if(userEntity == null){
			try {
				ThisAssection.getThisRuntimeException("用户非法登录已打回界面");
			} catch (Exception e) {
				//在命令行打印异常信息在程序中出错的位置及原因
				e.printStackTrace();
				// TODO: handle exception
				arg1.sendRedirect("index.jsp");
			}
			
		}
		return true;
	}

}

ThisAssection.java

public class ThisAssection {
	/**
	 * 自定义异常
	 * @param message
	 */
	public static void getThisRuntimeException(final String message)throws ThisRuntimeException{
		throw new ThisRuntimeException(message);
	}
	
	/**
	 * 断言一个对象为空
	 * @param message 提示用户的信息
	 * @param obj 需要断言的对象
	 * @return Object 返回类型
	 * @throws ThisRuntimeException
	 */
	public static Object isObjectNull(final String message,Object obj) throws ThisRuntimeException{
		if(obj!=null){
			getThisRuntimeException(message);
		}
		return obj;
	}
	
		
}

ThisRuntimeException.java

public class ThisRuntimeException extends RuntimeException{

	public ThisRuntimeException() {
		super();
		// TODO Auto-generated constructor stub
	}

	public ThisRuntimeException(String message, Throwable cause) {
		super(message, cause);
		// TODO Auto-generated constructor stub
	}

	public ThisRuntimeException(String message) {
		super(message);
		// TODO Auto-generated constructor stub
	}
	

}

springmvc.xml配置controller拦截器

<!-- 配置controller拦截器 -->
	<mvc:interceptors>
		<mvc:interceptor>
			<!-- 拦截地址,拦截所有 -->
			<mvc:mapping path="/**"/>
			<!-- 排除不需要拦截的地址 -->
			<mvc:exclude-mapping path="/register"/>
			<mvc:exclude-mapping path="/login"/>
			<mvc:exclude-mapping path="/findArticleListView"/>
			<mvc:exclude-mapping path="/findFileListView"/>
			<mvc:exclude-mapping path="/addAdView"/>
			<mvc:exclude-mapping path="/findChartView"/>
			<!-- <mvc:exclude-mapping path="/accountActivationCod"/> -->
			<!-- 指向自定义的拦截器 -->
			<bean class="com.xxx.utils.InterceptHandlerController">
				<property name="excludeUrls">
					<list>
						<value>register.jsp</value>
						<value>index.jsp</value>
						<value>/view/static</value>
					
					</list>
				</property>
			</bean>
		</mvc:interceptor>
	</mvc:interceptors>

http://www.niftyadmin.cn/n/1094004.html

相关文章

架构之路(一):目标

标准来源于目标 前文说过&#xff0c;评价架构好坏是一个很主观的东西。既然大家写出来的程序都能跑&#xff0c;凭什么就说你架构好&#xff0c;我的架构就差&#xff1f;拿出来大家评评理&#xff0c;张三说好&#xff0c;李四说不行&#xff0c;王五说将就……究竟谁说了算&…

java2实用教程 第十章 输入输出流

IO流 该图来自牛客网 File对象 构造方法 File(String filename) File(String derectoryPath,String filename); File(File dir,String filename) package fix_error; import java.io.*; public class test {public static void main (String args[]) {File f new File("…

java2实用教程 第四章 类与对象 复习

类&#xff1a;类声明类体&#xff08;变量声明方法定义&#xff08;方法头方法体&#xff08;局部变量声明语句&#xff09;&#xff09;&#xff09; 类名 驼峰习惯 面向对象编程&#xff1a;继承、多态、封装 继承&#xff1a;子类可以继承父类的属性和行为 多态&#xff…

java 2实用教程 第六章 接口与实现 复习

interface关键字、implements关键字 interface定义接口&#xff0c;implements关键字声明该类实现一个或多个接口 接口&#xff1a;接口声明接口体 接口体中只有抽象方法&#xff0c;所有的常量和抽象方法访问权限一定是public&#xff0c; 而且常量是static常量 1.在接口中 不…

Spynner的安装过程

想要用python来获取动态网页的内容&#xff0c;搜索到Spynner可以实现JQuery的调用。 安装Spynner花了好几个小时才完成。记录一下&#xff1a; Spynner的使用依赖于PyQt&#xff0c;而PyQt又依赖于Sip&#xff0c;所以安装步骤是&#xff1a;1&#xff09;先安装Sip&#xff1…

java2实用教程 第七章 常用实用类 复习

String类 final类&#xff0c;不能扩展 1.常量对象&#xff1a;放入常量池 “你好” 2.String对象&#xff1a;动态区&#xff0c;非常量池&#xff0c;堆 String s new String("你好“&#xff09;&#xff1b;//s存放引用 构造方法&#xff1a; char a[]{‘J’,‘A’,…

oracle例题

oracle scott用户下emp表 注意查询&#xff1a; 记得缺失的表用 查询雇员姓名,工作,领导姓名及部门名称 select e1.ename,e1.job,(select e2.ename from emp e2 where e1.mgr e2.empno ),d.dname from emp e1 ,dept d where e1.deptno d.deptno;查询每个部门中工资最高的雇…

账号安全逻辑

修改密码 修改手机号/绑定手机号 修改手机号 手机号还在用&#xff1a;验证手机号后跳转绑定手机界面手机号不再用&#xff0c;通过邮箱修改手机号 1.验证邮箱后跳转绑定手机界面 2.绑定邮箱 绑定手机号 绑定界面–> 绑定成功 修改邮箱/绑定邮箱 1.验证邮箱后 2.验证…