zoukankan      html  css  js  c++  java
  • 登陆拦截器Interceptor

    拦截器的目的是什么?

    当然是,拦截非法操作,我们需要登陆的,和不需要登陆的需求。

    开始进行操作:

    第一步:

    建立需要拦截的对象,如我自己建立的Iterceptor

    public class LoginInterceptor implements HandlerInterceptor{
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception {
            // TODO Auto-generated method stub
            HttpSession session=request.getSession();
            String contextPath=session.getServletContext().getContextPath();
            String[] requirePath=new String[] {
                    "admin_userlist",
                    "admin_reservelist",
                    "admin_about",
                    "admin_post_list",
                    "admin_config_list"
            };
            String uri=request.getRequestURI();
            uri=org.apache.commons.lang3.StringUtils.remove(uri, contextPath+"/");
            String page=uri;
            if(Beginwith(page,requirePath)) {
                User user=(User) session.getAttribute("user");
                if(user== null) {
                    response.sendRedirect("hello");
                    return false;
                }
            }
            return true;
        }
        private boolean Beginwith(String page,String[] requirePath) {
            boolean result=false;
            for(String requirePa :requirePath) {
                if(StringUtils.startsWith(page, requirePa)) {
                    result=true;
                    return result;
                }
            }
            return result;
            
        }
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                ModelAndView modelAndView) throws Exception {
            // TODO Auto-generated method stub
            
        }
    
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
                throws Exception {
            // TODO Auto-generated method stub
            
        }
    
    }

    代码不讲解,low

    第二步,配置拦截器configure

    @Configuration
    public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
    
        @Bean
        public LoginInterceptor loginInterceptor() {
            return new LoginInterceptor();
        }
        
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(loginInterceptor())
            .addPathPatterns("/**");
        }
    }
    该拦截器在springboot框架基础上进行配置
  • 相关阅读:
    poj 3461 (模式串T在主串S中出现的次数)
    hdu 1711( 模式串T在主串S中首次出现的位置)
    HDU 3980 (SG 环变成链 之前的先手变成后手)
    数据结构 Redo or Undo (模拟)
    数据结构 DNA序列 (KMP+暴力,或者STL+暴力)
    数据结构 英语词典 (STL+ set)
    数据结构 领取礼品的顺序 (STL+模拟)
    数据结构 求表达式串的后缀表达式和值 (栈+模拟)
    数据结构 下车的顺序 (STL+stack)
    数据结构 击鼓传花 (STL+模拟)
  • 原文地址:https://www.cnblogs.com/DIVEY/p/10876756.html
Copyright © 2011-2022 走看看