Using Spring Boot and Spring Security I configure a super simple application and add security based on URLs but it isn't respected. For example, if I log in as a 'scout' and hit an admin URL like /api/leaders, I can still see it.
I've tried many permutations of the URLs, even trying anyRequest().denyAll() and the scout user can still access it.
@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = {"com.ssoward.scouts"})
public class MainConfiguration {
public static void main(String[] args) throws Exception {
SpringApplication.run(MainConfiguration.class, args);
}
//security
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
static class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return authenticationManager();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/api/leaders*").hasRole("ADMIN") //url level security
.antMatchers("/api/scouts*").hasRole("USER"); //url level security
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("leader").password("password").roles("USER", "ADMIN").and()
.withUser("scout").password("password").roles("USER");
}
}
}
The entire project is on Github: https://github.com/ssoward/scouts
Spring Actuator auto configures Spring Security, however when I added the configuration noted above it added it didn't remove the config that Spring Actuator had added.
The solution: disable the Spring Actuator added security by adding the following to your application property file: security.basic.enabled=false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With