刚学了SpringSecurity,拿来做一个SpringBoot+SSMP项目,想着用最新版本边做边学,慢慢摸索。参照这个配置的:

都搭好了以后,想测试能不能直接访问静态资源。 

以下是SecurityConfig.class内容:

// Springboot2.7.0以及SrpignSecurity5.7版本以上应采用如下配置方式
@Configuration
@EnableWebSecurity  // 启用SpringSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)  // 启用方法级别的权限认证
public class SecurityConfig {

    /**
     * 配置全局的某些通用事物,例如静态资源等
     * @return
     */
    @Bean
    public WebSecurityCustomizer securityCustomizer() {
        return (web) -> web.ignoring().antMatchers("/static/**");
    }

    /**
     * http接口拦截
     * @param http
     * @return
     * @throws Exception
     */
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .antMatchers("/user/login", "/user/register", "/user/code").anonymous()
                .anyRequest().authenticated();

        http.addFilterBefore(tokenFilter, UsernamePasswordAuthenticationFilter.class);
        // 配置异常处理器
        http.exceptionHandling()
                .authenticationEntryPoint(entryPoint);  
        // SpringSecurity设置允许跨域
        http.cors();
        return http.build();
    }
}

resources目录结构:

springsecurity配置放行 springsecurity放行固定请求_mybatis

 正常按道理是可以访问的,然而并没有...

springsecurity配置放行 springsecurity放行固定请求_springsecurity配置放行_02

 资源仍然被拦截了...

如果在HttpSecurity中放行静态资源,试了以下,倒是不会被拦截了,但404了...

@Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .antMatchers("/user/login", "/user/register", "/user/code").anonymous()
                /* 倒是不会拦截了,但是直接找不到
                .antMatchers("/static/head/123.jpeg").permitAll()
                */
                .anyRequest().authenticated();

        http.addFilterBefore(tokenFilter, UsernamePasswordAuthenticationFilter.class);
        // 配置异常处理器
        http.exceptionHandling()
                .authenticationEntryPoint(entryPoint);   // 配置认证异常处理器
        // SpringSecurity设置允许跨域
        http.cors();
        return http.build();
    }

我还想着至少被放行了,算有点进步,浪费了大量时间去解决这个404问题。但其实这是一个错误的方向,之后仔细研究了一下,发现这边主要放行的端口,静态资源似乎并不适用...

于是回到了上一步,继续解决被拦截问题,看了一篇文章:

找不到链接了,大致就是说SpringMVC静态资源映射配置,于是自己各种捣鼓、尝试,总算访问成功了,以下是两种解决方案:


1.静态资源不放在static文件夹

我在resources下新建了一个head文件夹,里面放上图片,然后把路径配置到WebSecurityCustomizer中,然后发现可以访问到head里面的图片

@Bean
    public WebSecurityCustomizer securityCustomizer() {
        return (web) -> web.ignoring().antMatchers("/static/**", "/head/**");
    }

springsecurity配置放行 springsecurity放行固定请求_springsecurity配置放行_03

访问成功!!!可喜可贺

static文件夹为什么访问不了具体啥原因也不太清楚,可能是SpringMVC(或是SpringBoot?)和SpringSecurity映射冲突了,static 被SpringMVC锁了还是怎么的,因为我的static文件夹是创建项目时自动生成的,望大佬解答。

总之不用static文件夹就行!


2.配置SpringMVC静态资源映射路径

代码如下:

@Configuration
public class WebConfig extends WebMvcConfigurationSupport {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
}

大概就是重新映射静态资源路径到 classpath:/static/ 下面,通过/static/**访问,这样的话static文件夹就可以访问了!

结果:

springsecurity配置放行 springsecurity放行固定请求_springsecurity配置放行_04

 这样也可以访问。需要注意的是,这样配置的话,就只能访问到static文件夹下的东西,其它文件夹就访问不到了,除非继续配置。