溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Springboot引入攔截器并放行swagger代碼實(shí)例

發(fā)布時(shí)間:2020-08-23 13:52:59 來(lái)源:腳本之家 閱讀:1014 作者:濤先森の日常 欄目:編程語(yǔ)言

這篇文章主要介紹了Springboot引入攔截器并放行swagger代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

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{
  
  /**
   * 請(qǐng)求處理之后
   */
  @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);
  }

  /**
   * 請(qǐng)求處理之后調(diào)用
   */
  @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);
  }

  /**
   * 請(qǐng)求處理之前
   */
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
      throws Exception {
    return HandlerInterceptor.super.preHandle(request, response, handler);
  }
}

將攔截器添加到springmvc配置中,并放行swagger的相關(guān)資源

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;
@Configuration
public 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方法中添加攔截器時(shí),是new的一個(gè)攔截器。而并不是引入springBean容器初始化時(shí),已經(jīng)初始化好了的bean實(shí)例,并不是同一個(gè)對(duì)象,故而訪問時(shí)肯定是無(wú)法引入Interceptor類里面引入的bean實(shí)例,可能產(chǎn)生空指針異常。

即無(wú)法引入默認(rèn)注入的Bean,那么我們就自己定義一個(gè)Bean,然后在攔截器添加方法內(nèi)引入這個(gè)bean即可,上面主要對(duì)此進(jìn)行了處理。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI