以下是springboot箱子:
场景:在某些规则下大多数接口路径不需要通过窃听器,但其中一些接口还需要通过窃听器。
例如,在: '/api/register/** "模式匹配的路径中,大多数接口不需要登录,但其中“/api/register/logout”接口(请不要想为什么。 只是个样品。 )
意识下反应的可能操作是:
publicvoidaddinterceptors (interceptorregistryregistry ) registry.add interceptor//log in interceptor//log in interceptore excludepathpatterns (/API/register/* * ' )/2 .不拦截(/API/register/'下的所有接口. addpathpatterns //3 尝试将3的构成写在1处,或者放在1、2之间也无效。
正确的方法是调用addInterceptor方法,如下所示: 也就是说,添加了拦截器配置。 3360
publicvoidaddinterceptors (interceptorregistryregistry ) registry.add interceptor//log in interceptor//log in interceptore excludepathpatterns (/API/register/* * ); //2 .再做一个在拦截器'/api/register/下的所有接口//,针对该特定接口屏蔽拦截器。 但是,该拦截器与上面拦截器的业务代码一样,具有registry.add interceptor (log in interceptor )/llog //3.接口路径'/API/reptor
registry.addInterceptor表示每个InterceptorRegistration或addInterceptor都是一个新对象,具体取决于您加入new。 这不是和我们声明不同的拦截器,监听不同的路径一样的理由吗? 但是,这里的两个拦截器的业务是一样的。 如果可能的话,可以声明两个拦截器,但是他们的业务逻辑完全一致,在上述代码中使用。
以下是addInterceptor的内部实现:
//所属类的完全限定名称org.spring framework.web.servlet.config.annotation.interceptor registry # add interceptor//解释器是registry.add解释器(log in interceptor )的publicinterceptorregistrationaddinterceptor ) handler解释器this.registrations.add (registration; 返回注册; }