我有一个独立的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();
}
}
答案 0 :(得分:16)
classpath:/static
中的静态内容在应用程序的根目录(即/*
)提供,无论应用程序是否安全,因此您需要匹配根目录下的特定路径。 Spring Boot默认允许所有访问权限/js/**
,/css/**
,/images/**
(有关详细信息,请参阅SpringBootWebSecurityConfiguration
),但您可能已将其关闭(无法查看)其余的代码。)