首页 文章

访问受保护的Spring Boot应用程序中的静态内容

提问于
浏览
8

我有一个独立的Spring Boot应用程序,其中包含/ src / main / resources / templates中的模板和/ src / main / resources / static中的静态内容 . 我希望在身份验证之前可以访问静态内容,因此CSS也会在登录页面上加载 . 现在它只在验证后加载 . 我的安全配置如下所示:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private static final Logger logger = Logger.getLogger(SecurityConfig.class);

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) {
        try {
            auth.inMemoryAuthentication()
            ...
        } catch (Exception e) {
            logger.error(e);
        }
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf().disable()
            .formLogin()
                .defaultSuccessUrl("/projects", true)
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout", "GET"))
                .permitAll()
                .and()
            .authorizeRequests()
                .antMatchers("/static/**").permitAll()
                .anyRequest().authenticated();
    }

}

1 回答

  • 16

    无论应用程序是否安全, classpath:/static 中的静态内容都在应用程序的根目录(即 /* )中提供,因此您需要匹配根目录下的特定路径 . Spring Boot默认允许所有访问权限为 /js/** ,_ /css/**/images/** (详见 SpringBootWebSecurityConfiguration ),但您可能已将其关闭(无法查看其余代码) .

相关问题