• Springboot登录拦截器


    Springboot登录拦截器 和 swagger框架接口自动生成html文档

    使用开发工具:IDEA

    实现步骤如下

    1.需导入的依赖如下:

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
           
            <!--swagger api 依赖 -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.9.2</version>
            </dependency>
    
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.9.2</version>
            </dependency>
    
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>

    2.登录拦截器类

    package com.ckf.springbootinterceptor.config;
    
    import org.springframework.web.servlet.HandlerInterceptor;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * 登录拦截器
     */
    public class LoginInterceptor implements HandlerInterceptor {
    
    
        /**
         * 用于存储排除拦截的url  (登录/login.html, /css,/js,/img)
         */
        private List<String> urls = new ArrayList<String>();
    
        /**
         * 进入控制器之前拦截
         */
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    
            HttpSession session = request.getSession();
            if (session.getAttribute("admin") != null) {
                //已登录,放行。。
                return true;
            } else {
    
                System.out.println("你还没登录,没有权限");
               /* response.sendRedirect("/no_login");*/
          /*  session.setAttribute("no_login",session);
                request.setAttribute("msg", "没有权限,先登录");*/
                //未登录,拦截 返回login
                response.sendRedirect("/login");    //未登录,拦截跳转到登录页
                return false;
    
            }
        }
    
    
        /*设置能通过的url*/
        public List<String> getUrls() {
            urls.add("/login");   //login url请求
            urls.add("/doLogin");  //登录请求
            urls.add("/no_login");
            urls.add("/swagger-ui.html");
    
            //静态资源
            urls.add("/img/*");
            urls.add("/js/*");
            urls.add("/css/*");
            return urls;
        }
    }

    3.注册拦截器配置类

    package com.ckf.springbootinterceptor.config;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    /**
     * 注册拦截器配置类
     */
    @Configuration
    public class WebappAdrapter implements WebMvcConfigurer {
    
        /**
         * 将登录拦截器添加进来
         * addPathPatterns()添加拦截
         * excludePathPatterns()排除拦截
         */
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            //实例化登录拦截器,用于注册
            LoginInterceptor loginInterceptor = new LoginInterceptor();
    
            /***
             * 所有路径都被拦截  addPathPatterns("/**")
             * 允许通过   excludePathPatterns("/login", "/register")
             */
            registry.addInterceptor(loginInterceptor).addPathPatterns("/**").excludePathPatterns("/swagger-resources/**").excludePathPatterns(loginInterceptor.getUrls());
            WebMvcConfigurer.super.addInterceptors(registry);
    
        }
    }

    项目已托管码云

    地址:https://gitee.com/ckfeng/springboot-interceptor.git

     
  • 相关阅读:
    Laravel schema构建器列类型
    wkhtmltopdf docker + java(环境搭建及一些坑)
    dockerfile,仓库,私有仓库流程(转载)
    wkhtmltopdf参数详解及精讲使用方法(转载)
    传统前端项目中进行模块化编程并引入使用vue、elementui 前端
    vue3 + vuex4 实践 前端
    elementplus 原生开发 日期国际化语言 前端
    Vite2.0打包elementplus UI报错 前端
    vue3 Vetur报错:has no default export 组件没导出 前端
    windows版的HbuilderX连接iPad真机测试(uniapp)
  • 原文地址:https://www.cnblogs.com/ckfeng/p/12815243.html
Copyright © 2020-2023  润新知