SpringBoot拦截器----addInterceptors

Posted @云心以出岫 i

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot拦截器----addInterceptors相关的知识,希望对你有一定的参考价值。

1.创建我们自己的拦截器类并实现 HandlerInterceptor 接口

2.实现WebMvcConfigurer接口中的addInterceptors方法把自定义的拦截器类添加进来即可

ps:Spring boot 1.x是继承WebMvcConfigurerAdapter,等下会说到,但下面会是以spring boot 2.x为例子。

自定义拦截器代码:

package com.glj.componemt;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created with IntelliJ IDEA.
 * User: GuoLiangJun
 * Date: 2019/9/8
 * Time: 23:21
 */
public class LoginHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object user = request.getSession().getAttribute("loginUser");
        if(user == null){
            //未登陆,返回登陆页面
            request.setAttribute("msg","没有权限请先登陆");
            request.getRequestDispatcher("/index.html").forward(request,response);
            return false;
        }else{
            //已登陆,放行请求
            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 {

    }
}
package com.glj.componemt;
 
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * Created with IntelliJ IDEA.
 * User: GuoLiangJun
 * Date: 2019/9/8
 * Time: 23:21
 */
public class LoginHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object user = request.getSession().getAttribute("loginUser");
        if(user == null){
            //未登陆,返回登陆页面
            request.setAttribute("msg","没有权限请先登陆");
            request.getRequestDispatcher("/index.html").forward(request,response);
            return false;
        }else{
            //已登陆,放行请求
            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 {
 
    }
}

上面的代码就是判定session是否存在,存在则放行(辣鸡例子而已)

实现WebMvcConfigurer接口中的addInterceptors方法把自定义的拦截器类添加进来代码:


package com.glj.config;

import com.glj.componemt.LoginHandlerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.*;

/**
 * Created with IntelliJ IDEA.
 * User: GuoLiangJun
 * Date: 2019/09/04
 * Time: 16:51
 */
@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {

    /**
     * 添加拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration interceptorRegistration = registry.addInterceptor(new LoginHandlerInterceptor());
        interceptorRegistration.excludePathPatterns("/asserts/**","/error/**","/index.html","/","/user/login");
        interceptorRegistration.addPathPatterns("/**");
    }

}

package com.glj.config;
 
import com.glj.componemt.LoginHandlerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.*;
 
/**
 * Created with IntelliJ IDEA.
 * User: GuoLiangJun
 * Date: 2019/09/04
 * Time: 16:51
 */
@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {
 
    /**
     * 添加拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration interceptorRegistration = registry.addInterceptor(new LoginHandlerInterceptor());
        interceptorRegistration.excludePathPatterns("/asserts/**","/error/**","/index.html","/","/user/login");
        interceptorRegistration.addPathPatterns("/**");
    }
 
}

其中addPathPatterns("/**")对所有请求都拦截,但是(excludePathPatterns)排除了一些连接请求的拦截。

以上是关于SpringBoot拦截器----addInterceptors的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot.12.SpringBoot中的拦截器

SpringBoot.12.SpringBoot中的拦截器

SpringBoot.12.SpringBoot中的拦截器

SpringBoot.12.SpringBoot中的拦截器

SpringBoot——SpringBoot中使用拦截器

spring boot怎么配置拦截器