<b id="wionh"></b>

<u id="wionh"><address id="wionh"><meter id="wionh"></meter></address></u>

Spring Boot中使用攔截器解決靜態資源被攔截

時間:2022-12-31 20:52:29 類型:JAVA
字號:    

一,配置攔截器

public class MyInterceptor implements HandlerInterceptor {
    private static final Logger logger = LoggerFactory.getLogger(MyInterceptor.class);
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse
            response, Object handler) throws Exception {
        logger.info("====攔截到了方法:{},在該方法執行之前執行====" );
    // 返回true才會繼續執行,返回false則取消當前請求
        return true;
    }
}

二,解決靜態資源被攔截

@Configuration
public class MyInterceptorConfig extends WebMvcConfigurationSupport {
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }


    @Value("${ue.local.physical-path}")
    private String ueDir;

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        //類路徑 下的 static目錄可以直接訪問 
        registry.addResourceHandler("/uploads/**").addResourceLocations("file:"+ueDir);
        //file: + 絕對路徑
        super.addResourceHandlers(registry);
    }


}

三. appliaction.yml文件

ue:
  config-file: static/ueditor/jsp/config.json #resources目錄下配置文件的位置
  server-url: /ueditor.do #服務器統一請求接口路徑
  local: #上傳到本地配置
    url-prefix: /uploads/ #"/"結尾 自定義上傳時字段無意義
    physical-path: F:/java/uploads/ #存儲文件的絕對路徑 必須使用標準路徑"/"作為分隔符  自定義上傳時字段無意義


中文无码妇乱子二区三区_久久亚洲精品无码福利播放_毛片一级_日韩aⅴ无码久久精品免费

<b id="wionh"></b>

<u id="wionh"><address id="wionh"><meter id="wionh"></meter></address></u>

<