首页 > 解决方案 > 无法使用 Spring Security 配置访问静态文件夹中的内容

问题描述

我正在尝试访问“src\main\resources\static”文件夹中的内容,但在 chrome 的检查窗口中出现 401 错误。

在“静态”文件夹中,我有一个名为“assets”的子文件夹,结构有点像这样。

assets/css/** assets/js/** assets/fonts/** assets/image/** assets/css/boostrap/**

这是我的WebMvcConfigurerAdapter

@Configuration
public class OAuthWebFormConfiguration extends WebMvcConfigurerAdapter {

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/login").setViewName("login");

registry.addViewController("/oauth/confirm_access").setViewName("authorize");
}

@Configuration
@Order(-20)
protected static class LoginConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private CustomAuthenticationManager customAuthenticationManager;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin().loginPage("/login").permitAll()
                .and()
                .requestMatchers()
                .antMatchers("/login", "/oauth/authorize", "/oauth/confirm_access")//,"/resources/**", "/assets/**"
                .and()
                .authorizeRequests()
                .anyRequest()
                .authenticated();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.parentAuthenticationManager(customAuthenticationManager);
    }

    }
    }

标签: javaspring-bootspring-security

解决方案


添加以下内容:

.antMatchers("/assets/**").permitAll()

因此,通过这种方式,您允许所有请求匹配 /assets/** 的任何请求(因此您需要的所有可能性)

希望这可以帮助


推荐阅读