一,配置攔截器
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("====攔截到了方法:{},在該方法執(zhí)行之前執(zhí)行====" );
// 返回true才會繼續(xù)執(zhí)行,返回false則取消當(dāng)前請求
return true;
}
}二,解決靜態(tài)資源被攔截
方法一:
@Configuration
public class MyInterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
//配置攔截器
registry.addInterceptor(new MyInterceptor())
//對所有的資源進(jìn)行攔截,包括靜態(tài)資源
.addPathPatterns("/**")
.excludePathPatterns("/js/**", "/images/**","/css/**");
}
}方法二:
@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 #服務(wù)器統(tǒng)一請求接口路徑 local: #上傳到本地配置 url-prefix: /uploads/ #"/"結(jié)尾 自定義上傳時字段無意義 physical-path: F:/java/uploads/ #存儲文件的絕對路徑 必須使用標(biāo)準(zhǔn)路徑"/"作為分隔符 自定義上傳時字段無意義
