springboot2 webmvcconfig的配置【靜態資源】、【swagger】、【文件訪問映射】、【攔截器】

package cn.lonwin.rcs.rcsapi.conf;

import cn.lonwin.rcs.rcsapi.interceptor.AuthorizationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.*;

/**
 *
 * @author liweining
 * addInterceptors token 攔截器
 */
@Component
@Configuration
public class WebConfigurer **implements WebMvcConfigurer** {

    @Value("${upload.folder}")
    private String uploadPath;

    @Value("${file.staticAccessPath}")
    private String staticAccessPath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

        registry.addResourceHandler(staticAccessPath).addResourceLocations("file:///"+uploadPath);
        //super.addResourceHandlers(registry);
    }

    @Autowired
    private AuthorizationInterceptor authorizationInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authorizationInterceptor)
                .excludePathPatterns("/login/**")//放行登錄接口
                .excludePathPatterns("/sys/attach/preview/**")//放行文件預覽接口
                .excludePathPatterns("/upload/**")//放行文件訪問靜態資源url
                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**")//放行swagger接口文檔地址
                .addPathPatterns("/**/**");
        //super.addInterceptors(registry);

    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章