拦截器的注解

拦截器的注解

springboot项目使用过滤器有两种方案:

方案一:@WebFilter + @ServletComponentScan

- 启动类上面加@ServletComponentScan,开启扫描sevlet组件的扫描。Filter类上加@WebFilter注解生成过滤器。 
- 缺点是filter中不能引用Spring容器中的bean.

方案二: @Component + @Configuration

- Filter类上加@Component注解,filter类被载入Spring容器。
- 缺点:无法配置拦截路径等详细信息。
- @Configuration 在配置类中配置一个FilterRegistrationBean,将@Componen修饰的fitler注册到FilterRegistrationBean中,
	FilterRegistrationBean可以设置filter名称/拦截路径等。
@Configuration
		public class WebAuthFilterConfig {

		    @Bean
		    public FilterRegistrationBean webAuthFilterRegistration() {
		        FilterRegistrationBean registration = new FilterRegistrationBean();
		        registration.setFilter(webAuthFilter());
		        registration.setName("WebAuthFilter");
		        registration.addUrlPatterns("/web/*");
		        registration.addInitParameter("excludeUrls", "/web/login");
		        registration.setOrder(0);
		        return registration;
		    }
			
			/**WebAuthFilter可以使用@Bean注解创建实例,也可以使用@Component注解,然后在此类中引入*/
		    @Bean
		    public Filter webAuthFilter() {
		        return new WebAuthFilter();
		    }
		}

注意:@Component 和 @WebFilter 同时使用时同一个过滤器会生成两个实例,拦截两次,请注意。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 中,我们可以使用注解来定义拦截器。 首先,我们需要创建一个拦截器类,并实现 `HandlerInterceptor` 接口。例如: ```java import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 在请求处理之前进行拦截处理 return true; // 返回 true 表示继续执行后续的拦截器或请求处理,返回 false 则表示中断请求处理 } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // 在请求处理之后、视图渲染之前进行拦截处理 } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // 在整个请求完成之后进行拦截处理,可以用来释放资源等操作 } } ``` 接下来,我们可以使用 `@Configuration` 注解创建一个配置类,然后通过 `addInterceptors()` 方法将拦截器注册到拦截器链中。例如: ```java import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class MyInterceptorConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MyInterceptor()) .addPathPatterns("/**"); // 设置拦截路径 } } ``` 在上述示例中,我们创建了一个名为 `MyInterceptorConfig` 的配置类,并通过 `addInterceptors()` 方法将 `MyInterceptor` 拦截器添加到拦截器链中,并设置了拦截的路径为 "/*",即匹配所有请求路径。 这样,当请求进入时,会首先经过拦截器的 `preHandle()` 方法,在请求处理前进行拦截处理。如果 `preHandle()` 方法返回 true,则继续执行后续的拦截器或请求处理;如果返回 false,则中断请求处理。 需要注意的是,拦截器的注册顺序与执行顺序是相关的,可以通过 `addInterceptor()` 方法的顺序来确定拦截器的执行顺序。 希望对你有帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值