zoukankan      html  css  js  c++  java
  • SpringBoot 使用拦截器

    文章来自:https://www.cnblogs.com/tangou/p/8432325.html

    相比springmvc,springboot中拦截器不需要在xml中配置,

    只需定义拦截器类 implements HandlerInterceptor和拦截器拦截路径的配置类extends WebMvcConfigurerAdapter

    1.SessionInterceptor

    import org.springframework.web.servlet.HandlerInterceptor;
    
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    
    import javax.servlet.http.HttpServletResponse;
    
    public class SessionInterceptor implements HandlerIntercepto{    @Override    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
    
            System.out.println("uri="+request.getRequestURI());
            //登录不做拦截
            if(request.getRequestURI().equals("/userbg/login") || request.getRequestURI().equals("/user/login_view"))
            {
                return true;
            }
            //验证session是否存在
            Object obj = request.getSession().getAttribute("_session_user");
            if(obj == null)
            {
                response.sendRedirect("/user/login_view");
                return false;
            }
            return true;
        }
        @Override
        public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    
        }
        @Override
        public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    
        }
    }

    2.SessionConfiguration
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    /**
     * Created by 20160216 on 2018/2/8.
     */
    @Configuration
    public class SessionConfiguration extends WebMvcConfigurerAdapter
    {
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(new SessionInterceptor()).addPathPatterns("/**");
        }
    }
    

      

     
     
  • 相关阅读:
    FreeModbus TCP
    C#之用户自定义控件
    使用C库制作DLL
    C# Unable to load DLL 'WzCanDll.dll':找不到指定的模块
    C#之改变窗体icon图标、新建类文件、调用dll库
    sim800c GPRS模块的透传模式
    STM32上使用JSON
    STM32之串口IAP更新升级
    Fortran语言的REWIND和BACKSPACE 函数
    Code blocks 编译Fortran(转载)
  • 原文地址:https://www.cnblogs.com/FondWang/p/10994844.html
Copyright © 2011-2022 走看看