页面拦截管理 HandleInteceptor拦截器实现

HandleInteceptor拦截器做内容检查

设置拦截和放行区域

登录和静态资源放行,其他看session是否存在再考虑放行

定义inteceptors包中的LoginProtectInterceptor类

实现HandInterceptor接口

实现逻辑判断是否拦截

如果session中有userInfo,就放行,返回true;否则重定向到index首页,返回false。

LoginProtectInterceptor:

@Component
public class LoginProtectInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //false拦截 true执行
        Object userInfo = request.getSession().getAttribute("userInfo");
        if(userInfo !=null){
            return true;
        }else{
            response.sendRedirect(request.getContextPath()+"index.html");
            return false;
        }
    }
}

然后定义Interceptor的配置类:

MvcConfiguration:主要是配置拦截的路径 排除登录和资源路径

  @Configuration
    public class MvcConfiguration implements WebMvcConfigurer {

        /**
         * 用户注册拦截器
         * @param registry
         */
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            //拦截后台管理模块的路径  排除登录和资源路径
            registry.addInterceptor(new LoginProtectInterceptor()).addPathPatterns("/**")
                    .excludePathPatterns("/","/index.html","/index","/static/**",
                            "/user/login", "/user/logout",
                            "/api/**", "/css/**", "/images/**",
                            "/js/**", "/lib/**","/captcha"
                    );
        }
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值