spring - 在 protected Spring Boot应用程序中访问静态内容

标签 spring spring-boot

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

}

最佳答案

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

关于spring - 在 protected Spring Boot应用程序中访问静态内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22823863/

相关文章:

java - Spring Data Rest 的异常

Spring + Web MVC : dispatcher-servlet. xml 与 applicationContext.xml(加上共享安全性)

google-app-engine - 使用 appengineRun 运行 Spring Boot webapp

java - 在 Spring Boot 的 application.properties 中使用系统变量

eclipse - 如何在 Spring Boot 中使用 Bean 创建配置类?

java - 具有多个 JdbcTemplate 和 Spring Boot 的 NoSuchBeanDefinitionException

java - 如何在 spring web-flux Controller 中接收 GET 请求的多部分或有效负载数据

java - 如何在 Java POJO(文本、图像、Pdf、Doc 等)中为消息指定单个属性/字段?

java - Spring 在哪里创建 RestTemplate?

java - 如何通过 Spring Boot 从 Redis 缓存中获取所有 key ?