springboot——拦截器

一、目录结构(红色框)

springboot——拦截器_第1张图片

二、代码

config包中的代码

package demo.config;

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

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class SpringMVCConfig extends WebMvcConfigurerAdapter{
   

	@Override
	/**
	 * 重写一个方法sourse——Override——addInterceptors
	 */
	public void addInterceptors(InterceptorRegistry registry) {
   
		
		HandlerInterceptor handlerInterceptor = new HandlerInterceptor() {
   //匿名对象
			
			@Override
			/**
			 * preHandle
			 * 在请求controller之前执行
			 */
			public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)

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