spring boot + security 映射 本地磁盘 静态文件 实现在线预览

本文介绍了如何使用SpringBoot结合Security配置,实现对本地磁盘中的静态文件进行在线预览。通过在配置文件中设置静态资源路径和权限开放,允许访问E盘下test目录的文件。配置包括添加静态资源处理器和拦截器,以及在Security中忽略相关路径,使得用户可以在线预览如word文档等文件。
摘要由CSDN通过智能技术生成

spring boot + security 映射 本地磁盘 静态文件 实现在线预览

  1. 配置文件中增加:
  resources:
    static-locations: file:E:\\test\\
  mvc:
    static-path-pattern: /files/**       #改配置是请求路径 缩写  
  1. 开放权限(本人这里是 security)
@Configuration
@Slf4j
public class InterceptorConfig extends WebMvcConfigurationSupport {
  
  //静态资源拦截器
   @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler(" /files/** ").addResourceLocations(" file:E:\\test\\ ");
        super.addResourceHandlers(registry);
    }
    //添加拦截器
       @Override
   public void addInterceptors(InterceptorRegistry registry)
    {
        InterceptorRegistration registration = registry.addInterceptor(new AuthIntercepter());
        registration.excludePathPatterns(new String[] { "/error" });
        registration.excludePathPatterns(new String[] { "/files/**" });
        registration.addPathPatterns(new String[] { "/**" });
    }


}

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
    //接口开发
    @Override
    public void configure(WebSecurity web) throws Exception {
        //ignore
        web.ignoring()
                .antMatchers("/user/listNopagging")
                .antMatchers("/signature/signPc")
                .antMatchers("/static/**")
                .antMatchers("/files/**");
    }

}

在线预览方式 http://localhost:8002/clouddo-tranning/files/osofUpload/10180/word/1.doc

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值