SpringBoot的URL拦截器

1、数据验证拦截器类

package com.yt.interceptor;

import lombok.extern.log4j.Log4j2;
import org.springframework.http.MediaType;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 拦截器数据验证
 * @author tyg
 * @date   2020年8月5日下午3:46:09
 */
@Log4j2
@Component
public class AuthHandlerInterceptor extends HandlerInterceptorAdapter {

	private long startTime = 0;

	/**
	 * 请求处理前调用
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		startTime = System.currentTimeMillis();
		return super.preHandle(request, response, handler);
	}

	/**
	 * 请求处理完后渲染视图前调用
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
		log.info("\n======请求地址:{},耗时:{}ms", request.getServletPath(), System.currentTimeMillis() - startTime);
		super.postHandle(request, response, handler, modelAndView);
	}

	/**
	 * 渲染视图后调用
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}
}

2、将数据验证拦截器类加入到拦截器中

package com.yt.interceptor;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import javax.annotation.Resource;

/**
 * 加入URL拦截
 * @author tyg
 * @date 2020-12-31 14:59
 */
@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport {

    @Resource
    private AuthHandlerInterceptor authHandlerInterceptor;

    /**
     * 加入URL拦截
     * @param registry
     * @author tyg
     * @date 2020-12-31 14:59
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authHandlerInterceptor).addPathPatterns("/**");
        super.addInterceptors(registry);
    }

    /**
     * 设置默认数据
     */
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        // 默认返回格式 JSON
        configurer.defaultContentType(MediaType.APPLICATION_JSON);
    }
}

3、第2步中,会存在一个问题,如果在application.yml设置了返回时间格式为如下,是不会生效的,还是会返回时间戳,如果要返回格式化了的时间,那么就要将第2步进行替换。

spring:
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss

替换为:

package com.yt.interceptor;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * 加入URL拦截
 * @author tyg
 * @date 2020-12-31 14:59
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Resource
    private AuthHandlerInterceptor authHandlerInterceptor;

    /**
     * 加入URL拦截
     * @param registry
     * @author tyg
     * @date 2020-12-31 14:59
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authHandlerInterceptor).addPathPatterns("/**");
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值