zoukankan      html  css  js  c++  java
  • 登录功能-拦截器应用

    登录功能-拦截器应用

    完整项目 GitHub:https://github.com/MaoYanMovieWeb/maoyanmovie

    在登录功能实现时,如果需要权限控制,若不使用安全框架,使用简单的方式可以使用拦截器。

    拦截器首先先自定义拦截器,将这个自定义拦截器注册,设置需要拦截和放行的域名。

    在目录文件下创建interceptor文件夹,创建Interceptor,java文件,继承HandlerInterceptor接口,同时OverRide三个方法。在preHandler中编写拦截处理代码。

    返回 true/false 表示放行/拦截。

    同时将静态文件放行,通过session判断是否登录,未登录则拦截。

    package com.hut.maoyanmovie.interceptor;
    
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    /**
     * @author HP
     * @data 2020-12-14
     *
     *新建拦截器
     *
     * 自定义拦截器
     */
    public class AuthInterceptor implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            // 拦截处理代码
    
            //静态资源不被拦截器拦截
            String uri = request.getRequestURI();
            if (uri.endsWith("js")||uri.endsWith("css")||uri.endsWith("jpg")||uri.endsWith("svg")||uri.endsWith("jpg")||uri.endsWith("png")){
                return true ;
            }
            HttpSession session = request.getSession();
            // 获取用户信息,如果没有用户信息直接返回提示信息
            Object userInfo = session.getAttribute("loginUser");
            if (userInfo == null) {
                request.setAttribute("msg","请先登录!");
                request.getRequestDispatcher("logging").forward(request, response);
                return false;
            }
    
            return true;
        }
    
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    
        }
    
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    
        }
    }

    自定义拦截器后,需要注册拦截器。

    新建WebAppConfig.java文件,继承WebMvcConfigurerAdapter类,重写addInterceptor方法。同时加入@Configuration注解,在springboot启动的时候就会该配置类就会被扫描并加载。

    package com.hut.maoyanmovie.interceptor;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    /**
     * @author HP
     * @data 2020-12-14
     * 注册拦截器
     * 新建配置类继承WebMvcConfigurerAdapter类,重写addInterceptors方法
     */
    @Configuration
    public class WebAppConfig extends WebMvcConfigurerAdapter {
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            //注册自己的拦截器并设置拦截的请求路径
            registry.addInterceptor(new AuthInterceptor())
                    .addPathPatterns("/interorders/**")
                    .addPathPatterns("/Getcid/**")
                    .addPathPatterns("/add/**")
                    .addPathPatterns("/upload/**")//拦截的路径 
                    .excludePathPatterns(""); //放行的路径
        }
    
    }

    此时已经可以拦截了,访问对应域名会弹出拒绝访问信息。

     

  • 相关阅读:
    Date日期对象
    JAVA适配器
    java 对象的多态性
    简单轮播
    ecshop 教程地址
    瀑布流js排列
    phpcms 搜索结果页面栏目不显示解决 方法
    手机自动跳转
    字串符转换数字、取小数点后两位数的方法
    js 判断鼠标进去方向
  • 原文地址:https://www.cnblogs.com/djhzzl/p/14157059.html
Copyright © 2011-2022 走看看