springboot 本地文件映射

该配置文件详细描述了一个基于SpringSecurity的项目中,如何设置2.7.0版本项目的目录结构,文件路径,以及允许匿名访问的URL。同时,它展示了如何进行权限控制,包括使用JWT令牌过滤器,处理未授权和拒绝访问的情况,并利用BCryptPasswordEncoder处理密码加密。此外,还提到了SpringWebMVC的配置,用于映射和访问本地文件系统中的资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我的项目版本是2.7.0

路径配置

#项目目录
project_dictionary: /data/dry
#文件访问中间路径
file_middle_path: /files
#文件上传根目录
file_upload_path: ${project_dictionary}/${file_middle_path}

配置文件访问路径允许匿名访问

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true) 
@PreAuthorize("hasRole('admin')") @PreAuthorize("hasAuthority('sys:user:save')")
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Autowired
    private AuthenticationEntryPoint authenticationEntryPoint;

    @Autowired
    private AccessDeniedHandler accessDeniedHandler;

    @Value("${file_middle_path}")
    private String file_middle_path;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        String[] URL_WHITELISTS ={
                "/swagger-ui.html",
                "/swagger-resources/**",
                "/sys/register",
                "/sys/loginIn",
                file_middle_path+"/**"
        };
        http
                //关闭csrf
                .csrf().disable()
                //不通过Session获取SecurityContext
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                //允许匿名访问
                .antMatchers(URL_WHITELISTS)
                .permitAll()
                // 除上面外的所有请求全部需要鉴权认证
                .anyRequest().authenticated();
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
        http.exceptionHandling()
                .authenticationEntryPoint(authenticationEntryPoint)
                .accessDeniedHandler(accessDeniedHandler);
        http.cors();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

}

本地文件映射

路径一定要以反斜杠为结尾不然访问不了

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class FilePathConfig implements WebMvcConfigurer {
    @Value("${file_upload_path}")
    private String fileUploadPath;
    @Value("${file_middle_path}")
    private String fileMiddlePath;
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String path = fileUploadPath;
        //指定路径需要以/结尾不然访问不了
        if(!path.endsWith("/")){
            path+="/";
        } 
        registry.addResourceHandler(fileMiddlePath+"/**").addResourceLocations("file:"+path);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值