Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't Spring Security Java Config AntMatcher blocking this url?

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

like image 942
checketts Avatar asked Sep 04 '26 18:09

checketts


1 Answers

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

like image 51
checketts Avatar answered Sep 07 '26 09:09

checketts



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!