我的项目版本是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);
}
}