拦截器在ssm和springboot中的使用

拦截器在ssm和springboot中的使用

ssm拦截器的使用

  1. 在spring-mvc引入
     
        <mvc:interceptors>
            <mvc:interceptor>
                
                <mvc:mapping path="/**"/>
                
                <mvc:exclude-mapping path="/login" />
                
                <bean class="com.w.interceptor.LoginInterceptor">bean>
            mvc:interceptor>
        mvc:interceptors>
    
  2. 编写拦截处理类

    package com.w.interceptor;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    
    import org.springframework.web.servlet.HandlerInterceptor;
    
    public class LoginInterceptor implements HandlerInterceptor {
        
      //1、执行完毕,返回前拦截 --afterCompletion
      //2、在处理过程中,执行拦截-- postHandle
      //3、执行前进行拦截—preHandle(常用)
        
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    
            Integer uid = (Integer) request.getSession().getAttribute("uid");
            if(uid != null){
                // 登录成功不拦截
                return true;
            }else{
                // 拦截后进入登录页面
                response.sendRedirect(request.getContextPath()+"/login");//重定向
                return false;
            }
    
        }
    }
    
    

    springboot拦截器

    1. 编写拦截类实现HandlerInterceptor接口

      package com.w.interceptor;
      
      import lombok.extern.slf4j.Slf4j;
      import org.springframework.web.servlet.HandlerInterceptor;
      
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import javax.servlet.http.HttpSession;
      
      @Slf4j
      public class Interceptor implements HandlerInterceptor {
      	
          //方法执行前
          @Override
          public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
      
      
              String requestURI = request.getRequestURI();
              log.info("preHandle拦截的请求是:",requestURI);
      
              HttpSession session = request.getSession();
              Object ok = session.getAttribute("ok");
      
              if (ok != null){
                  //放行
                  return true;
              }else{
                  //拦截到,跳转到留言界面
                  request.setAttribute("msg","先发个留言呗!");
                  request.getRequestDispatcher("/wall").forward(request,response);
                  return false;
              }
      
      
          }
      }
      
      
      1. 拦截器注册到容器中(实现WebMvcConfigurer的addInterceptors)

        package com.w.config;
        
        import com.w.interceptor.Interceptor;
        import org.springframework.context.annotation.Configuration;
        import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
        import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
        
        /**
         * 1、编写一个拦截器实现HandlerInterceptor接口
         * 2、拦截器注册到容器中(实现WebMvcConfigurer的addInterceptors)
         * 3、指定拦截规则【如果是拦截所有,静态资源也会被拦截】
         */
        @Configuration
        public class LybWebConfig implements WebMvcConfigurer {
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(new Interceptor())
                        .addPathPatterns("/getAll");
        			// .addPathPatterns("/**")  //所有请求都被拦截包括静态资源
                     //   .excludePathPatterns("/","/login","/css/**","/fonts/**","/images/**","/js/**"); //放行的请求
            }
        }
        
        

你可能感兴趣的:(Java,过滤器,servlet,web.xml,java,spring,boot)