SpringBoot拦截器详细配置

SpringBoot拦截器详细配置

一、yml文件配置

path:
   include:
      - /*/*
   exclude:
      - /swagger-ui.html/**
      - /v2/**
      - /user/login
      - /dev/**
      - /test/**
二、拦截器Config读取yml文件信息
@Component
@ConfigurationProperties(prefix = "path")
public class PathConfig {

    private List include = new ArrayList<>();

    private List exclude = new ArrayList<>();

    public List getInclude() {
        return include;
    }

    public void setInclude(List include) {
        this.include = include;
    }

    public List getExclude() {
        return exclude;
    }

    public void setExclude(List exclude) {
        this.exclude = exclude;
    }
}

三、拦截器获取可访问和不可访问的集合

/**
 * @Author songmo
 * @Date 2020/4/17 11:26
 * @Description 拦截器配置
 */
@Configuration
public class LoginAdapter implements WebMvcConfigurer {

    @Autowired
    private PathConfig pathConfig;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        InterceptorRegistration ir = registry.addInterceptor(new LoginInterceptor());
        // 拦截
        ir.addPathPatterns(pathConfig.getInclude());
        // 配置不拦截
        ir.excludePathPatterns(pathConfig.getExclude());
    }
}

四、拦截控制类

/**
*@Author songmo
*@Date 2020/4/17 11:23
*@Description 拦截器
*/
public class LoginInterceptor implements HandlerInterceptor {
    private static Logger LOGGER = LoggerFactory.getLogger(LoginInterceptor.class);
    /**
    *@Author songmo
    *@Date 2020/4/17 11:25
    *@Description 在请求处理之前进行调用(Controller方法调用之前)
    */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        //统一拦截(查询当前session是否存在user)(这里user会在每次登陆成功后,写入session)
        User user=(User)request.getSession().getAttribute("user");
        if(user != null){
            return true;
        }
        //设置response状态值为403
//        response.setStatus(403);
        try {
            PrintWriter out = response.getWriter();
            ResponseInfo responseInfo = ResponseInfo.ok();
            responseInfo.setCode(403);
            out.append(JSON.toJSONString(responseInfo));
        }catch (Exception e){
            LOGGER.error("拦截器配置出错", e);
            response.setStatus(500);
        }
        //如果设置为false时,被请求时,拦截器执行到此处将不会继续操作
        //如果设置为true时,请求将会继续执行后面的操作
        return false;
    }

    /**
    *@Author songmo
    *@Date 2020/4/17 11:24
    *@Description 请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)
    */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
//        LOGGER.info("执行了TestInterceptor的postHandle方法");
    }

    /**
    *@Author songmo
    *@Date 2020/4/17 11:25
    *@Description 在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)
    */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
//        LOGGER.info("执行了TestInterceptor的afterCompletion方法");
    }
    
}

 

你可能感兴趣的:(java)