Spring Boot 拦截器的使用

1、编写一个拦截器实现 HandlerInterceptor 接口

package com.yanghui.admin.interceptor;

import lombok.extern.slf4j.Slf4j;
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;

/**
 * @ClassName LoginInterceptor
 * @Author YH
 * @Date 2021/6/3
 * @Version 1.0
 *
 * 登录检查
 * 1、配置好拦截器要拦截哪些请求
 * 2、把这些配置放在容器中
 */
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {

    /**
     * 目标方法执行之前
     * @return true 就是放行,false 就是拦截
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 打印日志
        String requestURI = request.getRequestURI();
        log.info(requestURI);
        // 登录检查
        HttpSession session = request.getSession();
        Object loginUser = session.getAttribute("loginUser");
        if (loginUser != null) {
            // 放行
            return true;
        }

        response.sendRedirect(request.getContextPath() + "/login");
        // 拦截
        return false;
    }

    /**
     * 目标方法执行之后
     */
    @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 {

    }
}



2、将拦截器组件添加到容器中

package com.yanghui.admin.config;

import com.yanghui.admin.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @ClassName AdminWebConfig
 * @Author YH
 * @Date 2021/6/3
 * @Version 1.0
 *
 * 实现 WebMvcConfigurer 接口的 addInterceptors() 方法
 * 目的是为了将拦截器添加到容器中
 */
@Configuration
public class AdminWebConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        /**
         * 给容器中添加一个拦截器组件
         * 链式编程
         * addPathPatterns() 要拦截的路径
         *      /** 拦截所有,静态资源同样也被拦截
         * excludePathPatterns() 要放行的路径
         * LoginInterceptor 是我们自定义的拦截器
         */
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/", "/login", "/css/**", "/fonts/**", "/images/**", "/js/**");
    }
}



3、指定拦截规则

@Override
public void addInterceptors(InterceptorRegistry registry) {
	/**
	* 给容器中添加一个拦截器组件
	* 链式编程
	* addPathPatterns() 要拦截的路径
	*      /** 拦截所有,静态资源同样也被拦截
	* excludePathPatterns() 要放行的路径
	* 注意要把静态资源的路径放行
	*/
	registry.addInterceptor(new LoginInterceptor())
			.addPathPatterns("/**")
			.excludePathPatterns("/", "/login", "/css/**", "/fonts/**", "/images/**", "/js/**");
}

你可能感兴趣的:(spring,boot,interceptor,web)