【问题标题】:access static content in secured Spring Boot application访问安全 Spring Boot 应用程序中的静态内容
【发布时间】:2014-05-14 11:15:09
【问题描述】:

我有一个独立的 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();
    }

}

【问题讨论】:

    标签: spring spring-boot


    【解决方案1】:

    classpath:/static 中的静态内容服务于应用程序的根目录(即/*),无论应用程序是否安全,因此您需要匹配根目录下的特定路径。 Spring Boot 默认允许对/js/**、/css/**、/images/** 的所有访问(有关详细信息,请参阅SpringBootWebSecurityConfiguration),但您可能已将其关闭(看不到其余代码)。

    【讨论】:

    • 谢谢,当我使用.antMatchers("/css/**").permitAll() 时,它可以工作。我已经编辑了我的帖子以包含完整的安全配置。我看不出还有什么地方可以关闭默认行为?
    • @EnableWebSecurity 关闭默认设置。
    猜你喜欢
    • 1970-01-01
    • 2015-05-24
    • 2017-03-01
    • 1970-01-01
    • 2019-05-05
    • 2022-01-02
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多