Springboot登录拦截器

2020-05-01 16:02:53来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

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

 

原文链接:https://www.cnblogs.com/ckfeng/p/12815243.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:使用Mongodb实现打卡签到系统

下一篇:synchronized内置锁原理