Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Spring Security Filter to lock down everything except a few routes

We're reworking our product to remove the default "anonymousUser" behavior in SpringSecurity and would like to lock down all URLs (via filter security) with the exception of just a few endpoints. What we can't figure out is how to specify "lock down everything except X, Y, and Z"

Our security setup essentially boils down to the following:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            // disable anonymous users
            .anonymous().disable()

            // don't add ROLE_ to the role...
            .authorizeRequests()
                .regexMatchers("^/", "^/login", "^/mobile/login", "^/api/auth/.*")
                    .authenticated()
                .and()  
        ;
    }
}

Other routes I've taken have been akin to :

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            // disable anonymous users
            .anonymous().disable()

            // don't add ROLE_ to the role...
            .authorizeRequests()
            .antMatchers("/**")
                .authenticated()
            .antMatchers("/", "/login", "/mobile/login", "/api/auth/**", "/reservations/**")
                .permitAll()
            .and()
        ;
    }
}

Any advice / input would be appreciated.

Thanks!

like image 240
David Welch Avatar asked Apr 08 '15 18:04

David Welch


People also ask

How do I disable Spring Security for all URLs?

Show activity on this post. When using permitAll it means every authenticated user, however you disabled anonymous access so that won't work. What you want is to ignore certain URLs for this override the configure method that takes WebSecurity object and ignore the pattern. Show activity on this post.

Will Spring Security secures all the applications?

If Spring Security is on the classpath, Spring Boot automatically secures all HTTP endpoints with “basic” authentication. However, you can further customize the security settings. The first thing you need to do is add Spring Security to the classpath.

How does a Spring Security filter work?

Spring Security maintains a filter chain internally where each of the filters has a particular responsibility and filters are added or removed from the configuration depending on which services are required. The ordering of the filters is important as there are dependencies between them.


2 Answers

We're reworking our product to remove the default "anonymousUser" behavior in Spring Security

I'm wondering what you mean by this. Based on the rest of the description, I don't think you need the following (i.e. you should remove it):

anonymous().disabled()

The above says that the user will be null if no user is authenticated, which tends to lead to NullPointerExceptions.

Remember that for authorizeRequests() (or for <intercept-url> ) ordering matters. The Java configuration you have (reformatted slightly for readability)

.authorizeRequests()
    .antMatchers("/**").authenticated()
    .antMatchers("/", "/login", "/mobile/login", "/api/auth/**", "/reservations/**").permitAll()
    .and()

is going to use the following logic:

  • Does this request match "/**"?
    • Yes, everything matches "/**". So every request requires the user to be authenticated.
  • Ignore every other rule because we already matched

Instead you should use the following:

.authorizeRequests()
    .antMatchers("/", "/login", "/mobile/login", "/api/auth/**", "/reservations/**").permitAll()
    .anyRequest().authenticated()
    .and()
  • Does the request match "/", or "/login", or ...?
    • If yes, then anyone is allowed to access it and STOP (no more rules are used).
    • If the request does not match, then continue.
  • Does the request match any request?
    • Yes, so if the request does not match a previous rule, then it will require the user to be authenticated.

NOTE: antMatchers("/**") is more concisely represented as anyRequest().

like image 169
Rob Winch Avatar answered Oct 18 '22 22:10

Rob Winch


The answer from Rob Winch will be the correct answer in nearly all cases and is the approach that I take in my projects. I do think that it is also worth noting that another possible approach could be to do the following:

public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/assets/**", "/index.html");
}

PLEASE NOTE that this is a separate method from the one in the examples you submitted earlier. That method has a parameter of type HttpSecurity while this one is of type WebSecurity.

What this code sample will do is find any requests that match and completely skip the HTTP security filters all together.

So if you want to optimize some requests that you know will need ZERO of the features that HttpSecurity provides then this could be a good solution. This means that if you use features like csrf(), requestCache(), headers() they WILL NOT be applied to the matching requests from the example above ("/assets/**", "/index.html")

like image 26
danlangford Avatar answered Oct 18 '22 22:10

danlangford