zoukankan      html  css  js  c++  java
  • springboot拦截器

    1.创建拦截类实现HandlerInterceptor

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    import com.pengsheng.bugcollect.po.PersonInfo;
    
    public class loginFilter implements HandlerInterceptor{
    	
    	@Override
        public void afterCompletion(HttpServletRequest request,
                                    HttpServletResponse response, Object obj, Exception err)
                throws Exception {
    		
        }
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response,
                               Object obj, ModelAndView mav) throws Exception {
        }
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                                 Object obj) throws Exception {
            //获取session里的登录状态值
        	PersonInfo str = (PersonInfo) request.getSession().getAttribute("personInfo");
               //如果登录状态不为空则返回true,返回true则会执行相应controller的方法
               if (str != null) {
                   return true;
                }
               //如果登录状态为空则重定向到登录页面,并返回false,不执行原来controller的方法
              response.sendRedirect("/initLogin.action");
              return false;
        }
    }
    

      2.编写InterceptorConfigurerAdapter类继承WebMvcConfigurerAdapter

    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    @Configuration
    public class InterceptorConfigurerAdapter extends WebMvcConfigurerAdapter {
        /**
         * 该方法用于注册拦截器
         * 可注册多个拦截器,多个拦截器组成一个拦截器链
         */
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            // addPathPatterns 添加路径
            // excludePathPatterns 排除路径
            registry.addInterceptor(new loginFilter()).addPathPatterns("/*.*").excludePathPatterns("/initLogin.action", "/jsp/login.jsp","/login.action");
            super.addInterceptors(registry);
        }
    }
    

      

  • 相关阅读:
    利用wikipedia 的API实现对其内容的查询
    Android 无标题 全屏设置
    anroid中ScrollView嵌套ListView
    ListView的不同item加载不同Layout,listviewlayout
    ListView缓存机制
    演化理解 Android 异步加载图片
    【自定义控件】自定义属性
    【自定义控件】组合控件
    【自定义控件】的基本步骤
    ContentResolver如何获取本地媒体文件
  • 原文地址:https://www.cnblogs.com/liuna369-4369/p/12177158.html
Copyright © 2011-2022 走看看