springboot配置拦截器

1、拦截器代码(需要springboot扫描到注解)

@Component
public class InitInterceptor extends HandlerInterceptorAdapter {
	private static final Logger logger = LoggerFactory.getLogger(InitInterceptor.class);


	/**
	 * 在执行controller方法之前进行请求参数处理
	 */
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {

		if (handler instanceof HandlerMethod) {
			HandlerMethod h = (HandlerMethod) handler;
			//填写拦截逻辑(非静态资源)
			return true;	
			
		}
                return fase;
	}


	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

	}

}

 2、拦截器配置类(需要springboot扫描到注解)

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
	
	@Autowired
	private InitInterceptor initInterceptor; 
	
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(initInterceptor);
	}
}

 

你可能感兴趣的:(springboot,springMVC,ssm)