Skip to content

Use permitAll for CloudFoundry endpoints #32622

Open
@jzheaux

Description

@jzheaux

Spring Security 5.8/6 supports delaying the lookup of the SecurityContext until an authorization rule requires it.

As such, it's preferred to use authorizeHttpRequests#permitAll over web.ignoring(). In the past web.ignoring() was added as a quick workaround to address the performance impact of looking up the SecurityContext on every request. Now, Spring Security defers that work until authorization-time and in the case of permitAll, no authorization is performed.

Consider the following application:

@Bean 
SecurityFilterChain app(HttpSecurity http) {
    http
        .authorizeHttpRequests((authorize) -> authorize
                .anyRequest().authenticated()
        )
        // ...

    return http.build();
}

@Bean 
WebSecurityCustomizer ignore() {
    return (web) -> web.ignoring().antMatchers("/cloudfoundry/**");
}

The behavior of the above application asks Spring Security to protect all endpoints other than /cloudfoundry.

As of Spring Security 5.7, this produces a warning that web.ignoring() is not recommended since this prevents Spring Security from using its WAF and writing secure HTTP response headers for those ignored endpoints.

Alternatively, the application can do the following:

@Bean 
SecurityFilterChain app(HttpSecurity http) {
    http
        .authorizeHttpRequests((authorize) -> authorize
                .mvcMatchers("/cloudfoundry/**").permitAll()
                .anyRequest().authenticated()
        )
        // ...

    return http.build();
}

Or, if it should be considered entirely separate:

@Bean 
SecurityFilterChain app(HttpSecurity http) {
    http
        .authorizeHttpRequests((authorize) -> authorize.anyRequest().authenticated())
        // ...

    return http.build();
}

@Bean 
@Order(-1)
SecurityFilterChain cloudfoundry(HttpSecurity http) {
    http
        .securityMatchers((matches) -> matches.requestMatchers("/cloudfoundry/**"))
        .authorizeHttpRequests((authorize) -> authorize.anyRequest().permitAll());

    return http.build();
}

This has the additional benefit of removing Spring Security's warning message.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions