• springmvc拦截器配置


    1、目的:拦截器的配置主要是对请求进行相应的处理(在登录和session过时的时候是一个很好的方式)

    2、由于Spring容器的优越性,在配置的时候全部交给容器管理是一个很不错的方式

    3、配置拦截器:

      1)在对应的方式类中实现HandlerInterceptor接口,会要求实现3个方法

      

    @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
    		
    	}
    
    	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object obj) throws Exception {
    		
    		return true;
    	}
    

      2)所有的请求都会在拦截器里面经过,这样在做登录拦截的时候会起到数据的保护和相关的展示限权

      在preHandle方法里面加入自己的逻辑

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object obj) throws Exception {
    		
    		//获取判定登陆的session是否存在
    		String token = (String) request.getSession().getAttribute("token");
    		String postId = (String) request.getSession().getAttribute("postId");
    		if(token == null || token == ""){
    			String XRequested =request.getHeader("X-Requested-With");
    			if("XMLHttpRequest".equals(XRequested)){
    				response.getWriter().write("IsAjax");
    			}else{
    				response.sendRedirect("/m-web/");
    			}
    			return false;
    		}
    		if(postId == null || postId == ""){
    			String XRequested =request.getHeader("X-Requested-With");
    			if("XMLHttpRequest".equals(XRequested)){
    				response.getWriter().write("IsAjax");
    			}else{
    				response.sendRedirect("/m-web/");
    			}
    			return false;
    		}
    		return true;
    	}
    

      里面存在ajax请求拦截的处理详情见:http://www.cnblogs.com/ll409546297/p/6203403.html

    3、在Spring的配置文件里面加入

    <mvc:interceptors>
            <mvc:interceptor>
                <mvc:mapping path="/**"/>
                <mvc:exclude-mapping path="/user/login"/>
                <mvc:exclude-mapping path="/user/loginOut"/>
                <mvc:exclude-mapping path="/user/setPostId"/>
                <mvc:exclude-mapping path="/user/getPostId"/>
                <mvc:exclude-mapping path="/resources/**"/>
                <mvc:exclude-mapping path="/assets/**"/>
                <mvc:exclude-mapping path="/css/**"/>
                <mvc:exclude-mapping path="/fonts/**"/>
                <mvc:exclude-mapping path="/images/**"/>
                <mvc:exclude-mapping path="/img/**"/>
                <mvc:exclude-mapping path="/js/**"/>
                <mvc:exclude-mapping path="/pic/**"/>
                <mvc:exclude-mapping path="/plugins/**"/>
                <mvc:exclude-mapping path="/static/**"/>
                <mvc:exclude-mapping path="/ui/**"/>
                <bean class="com.troy.ai.web.Interceptors.LoginInterceptor"/>
            </mvc:interceptor>
        </mvc:interceptors>

    解释:

    <mvc:mapping path="/**"/>  //是对所有路径进行过滤
    <mvc:exclude-mapping path="/user/login"/>  //是对该路径进行放行
    <mvc:exclude-mapping path="/ui/**"/>  //是对静态资源加载放行
    <bean class="com.troy.ai.web.Interceptors.LoginInterceptor"/>  //指定处理拦截的相关类

    4、拦截器的配置相对简单,但是在实际的应用过程却非常常见,逻辑处理方面需要根据实际来处理

  • 相关阅读:
    hdu 4768 Flyer 二分
    hdu 4767 Bell
    hdu 4759 Poker Shuffle 二进制
    uva 12589
    zoj 3057 Beans Game 博弈论
    poj 2480 Longge's problem 积性函数
    重新启程
    【Luogu P1502】 窗口的星星
    【BZOJ1855】[Scoi2010] 股票交易
    【BZOJ1122】[POI2008] 账本BBB
  • 原文地址:https://www.cnblogs.com/ll409546297/p/6434102.html
Copyright © 2020-2023  润新知