@ -26,7 +26,6 @@ import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest ;
import org.springframework.beans.factory.ObjectProvider ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.boot.actuate.endpoint.Endpoint ;
import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping ;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint ;
@ -42,11 +41,11 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplicat
import org.springframework.boot.autoconfigure.condition.SpringBootCondition ;
import org.springframework.boot.autoconfigure.security.AuthenticationManagerConfiguration ;
import org.springframework.boot.autoconfigure.security.FallbackWebSecurityAutoConfiguration ;
import org.springframework.boot.autoconfigure.security.IgnoredRequestCustomizer ;
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration ;
import org.springframework.boot.autoconfigure.security.SecurityPrerequisite ;
import org.springframework.boot.autoconfigure.security.SecurityProperties ;
import org.springframework.boot.autoconfigure.security.SpringBootWebSecurityConfiguration ;
import org.springframework.boot.autoconfigure.web.ErrorController ;
import org.springframework.boot.autoconfigure.web.ServerProperties ;
import org.springframework.boot.context.properties.EnableConfigurationProperties ;
import org.springframework.context.ApplicationContext ;
@ -56,9 +55,7 @@ import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration ;
import org.springframework.core.annotation.Order ;
import org.springframework.core.type.AnnotatedTypeMetadata ;
import org.springframework.security.config.annotation.web.WebSecurityConfigurer ;
import org.springframework.security.config.annotation.web.builders.HttpSecurity ;
import org.springframework.security.config.annotation.web.builders.WebSecurity ;
import org.springframework.security.config.annotation.web.builders.WebSecurity.IgnoredRequestConfigurer ;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity ;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration ;
@ -72,7 +69,6 @@ import org.springframework.security.web.util.matcher.AnyRequestMatcher;
import org.springframework.security.web.util.matcher.NegatedRequestMatcher ;
import org.springframework.security.web.util.matcher.OrRequestMatcher ;
import org.springframework.security.web.util.matcher.RequestMatcher ;
import org.springframework.util.ObjectUtils ;
import org.springframework.util.StringUtils ;
/ * *
@ -102,9 +98,34 @@ public class ManagementWebSecurityAutoConfiguration {
AnyRequestMatcher . INSTANCE ) ;
@Bean
@ConditionalOnMissingBean ( { IgnoredPathsWebSecurityConfigurerAdapter . class } )
public IgnoredPathsWebSecurityConfigurerAdapter ignoredPathsWebSecurityConfigurerAdapter ( ) {
return new IgnoredPathsWebSecurityConfigurerAdapter ( ) ;
public IgnoredRequestCustomizer managementIgnoredRequestCustomizer (
ManagementServerProperties management ,
ObjectProvider < ManagementContextResolver > contextResolverProvider ) {
return new ManagementIgnoredRequestCustomizer ( management ,
contextResolverProvider . getIfAvailable ( ) ) ;
}
private class ManagementIgnoredRequestCustomizer implements IgnoredRequestCustomizer {
private final ManagementServerProperties management ;
private final ManagementContextResolver contextResolver ;
ManagementIgnoredRequestCustomizer ( ManagementServerProperties management ,
ManagementContextResolver contextResolver ) {
this . management = management ;
this . contextResolver = contextResolver ;
}
@Override
public void customize ( IgnoredRequestConfigurer configurer ) {
if ( ! this . management . getSecurity ( ) . isEnabled ( ) ) {
RequestMatcher requestMatcher = LazyEndpointPathRequestMatcher
. getRequestMatcher ( this . contextResolver ) ;
configurer . requestMatchers ( requestMatcher ) ;
}
}
}
@Configuration
@ -132,80 +153,6 @@ public class ManagementWebSecurityAutoConfiguration {
}
// Get the ignored paths in early
@Order ( SecurityProperties . IGNORED_ORDER + 1 )
private static class IgnoredPathsWebSecurityConfigurerAdapter
implements WebSecurityConfigurer < WebSecurity > {
@Autowired ( required = false )
private ErrorController errorController ;
@Autowired
private SecurityProperties security ;
@Autowired
private ManagementServerProperties management ;
@Autowired ( required = false )
private ManagementContextResolver contextResolver ;
@Autowired ( required = false )
private ServerProperties server ;
@Override
public void configure ( WebSecurity builder ) throws Exception {
}
@Override
public void init ( WebSecurity builder ) throws Exception {
if ( this . server = = null ) {
return ;
}
IgnoredRequestConfigurer ignoring = builder . ignoring ( ) ;
// The ignores are not cumulative, so to prevent overwriting the defaults
// we add them back.
Set < String > ignored = new LinkedHashSet < String > (
SpringBootWebSecurityConfiguration . getIgnored ( this . security ) ) ;
if ( ignored . contains ( "none" ) ) {
ignored . remove ( "none" ) ;
}
if ( this . errorController ! = null ) {
ignored . add ( normalizePath ( this . errorController . getErrorPath ( ) ) ) ;
}
RequestMatcher requestMatcher = getRequestMatcher ( ) ;
String [ ] paths = this . server . getPathsArray ( ignored ) ;
if ( ! ObjectUtils . isEmpty ( paths ) ) {
List < RequestMatcher > matchers = new ArrayList < RequestMatcher > ( ) ;
for ( String pattern : paths ) {
matchers . add ( new AntPathRequestMatcher ( pattern , null ) ) ;
}
if ( requestMatcher ! = null ) {
matchers . add ( requestMatcher ) ;
}
requestMatcher = new OrRequestMatcher ( matchers ) ;
}
if ( requestMatcher ! = null ) {
ignoring . requestMatchers ( requestMatcher ) ;
}
}
private RequestMatcher getRequestMatcher ( ) {
if ( this . management . getSecurity ( ) . isEnabled ( ) ) {
return null ;
}
return LazyEndpointPathRequestMatcher . getRequestMatcher ( this . contextResolver ) ;
}
private String normalizePath ( String errorPath ) {
String result = StringUtils . cleanPath ( errorPath ) ;
if ( ! result . startsWith ( "/" ) ) {
result = "/" + result ;
}
return result ;
}
}
@Configuration
@ConditionalOnMissingBean ( WebSecurityConfiguration . class )
@Conditional ( WebSecurityEnablerCondition . class )
@ -310,9 +257,7 @@ public class ManagementWebSecurityAutoConfiguration {
// Permit access to the non-sensitive endpoints
requests . requestMatchers ( new LazyEndpointPathRequestMatcher (
this . contextResolver , EndpointPaths . NON_SENSITIVE ) ) . permitAll ( ) ;
// Restrict the rest to the configured roles
List < String > roles = this . management . getSecurity ( ) . getRoles ( ) ;
requests . anyRequest ( ) . hasAnyRole ( roles . toArray ( new String [ roles . size ( ) ] ) ) ;
requests . anyRequest ( ) . authenticated ( ) ;
}
}