如何在Springboot中引入拦截器

发布时间:2021-05-10 16:10:02 作者:Leah
来源:亿速云 阅读:298

本篇文章为大家展示了如何在Springboot中引入拦截器,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

springboot是什么

springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。

Springboot引入拦截器

自定义的拦截器类 Interceptor

package cn.zytao.taosir.auth.config;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;public class AuthInterceptor implements HandlerInterceptor{    /**   * 请求处理之后   */  @Override  public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)      throws Exception {    // TODO Auto-generated method stub    HandlerInterceptor.super.afterCompletion(request, response, handler, ex);  }  /**   * 请求处理之后调用   */  @Override  public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,      ModelAndView modelAndView) throws Exception {    // TODO Auto-generated method stub    HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);  }  /**   * 请求处理之前   */  @Override  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)      throws Exception {    return HandlerInterceptor.super.preHandle(request, response, handler);  }}

将拦截器添加到springmvc配置中,并放行swagger的相关资源

package cn.zytao.taosir.auth.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;@Configurationpublic class SpringMVCConfig extends WebMvcConfigurationSupport{  @Bean  public AuthInterceptor getAuthInterceptor() {    return new AuthInterceptor();  }  @Override  public void addInterceptors(InterceptorRegistry registry) {    registry    .addInterceptor(getAuthInterceptor())    .addPathPatterns("/**")    .excludePathPatterns("/login")    .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");    super.addInterceptors(registry);  }  @Override  protected void addResourceHandlers(ResourceHandlerRegistry registry) {    registry.addResourceHandler("swagger-ui.html")    .addResourceLocations("classpath:/META-INF/resources/");    registry.addResourceHandler("/webjars/**")    .addResourceLocations("classpath:/META-INF/resources/webjars/");    super.addResourceHandlers(registry);  }}

在addInterceptors方法中添加拦截器时,是new的一个拦截器。而并不是引入springBean容器初始化时,已经初始化好了的bean实例,并不是同一个对象,故而访问时肯定是无法引入Interceptor类里面引入的bean实例,可能产生空指针异常。

即无法引入默认注入的Bean,那么我们就自己定义一个Bean,然后在拦截器添加方法内引入这个bean即可,上面主要对此进行了处理。

上述内容就是如何在Springboot中引入拦截器,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. Springboot引入拦截器并放行swagger代码实例
  2. 怎么在Springboot中引入多个yml

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot

上一篇:如何在HTML5中引入css和选择器

下一篇:如何在vue中引入bootstrap

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》