Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore Spring Security config for every thing except a pattern

I have a rest webservice configured as a spring boot application. All my rest urls have a base path "/api/...". I am also serving static content from my application. I need to configure security ONLY for the web service i.e., URLs that start with "/api/..." but give the other static content w/o applying security.

I've only seen examples where we filter some url patterns via:

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/resources/*");
}

but not otherwise ...

like image 465
David Nimmagadda Avatar asked Feb 10 '23 16:02

David Nimmagadda


1 Answers

Use the antMatcher method of HttpSecurity class:

@Configuration
@EnableWebSecurity
public class MySecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.antMatcher("/api/**");
        // add security constraints for /api/... here
    }

    /* rest of config */
}
like image 136
holmis83 Avatar answered Feb 13 '23 04:02

holmis83