#138 WebInvocationPrivilegeEvaluator has default value

This commit is contained in:
Rob Winch 2013-07-01 08:46:57 -05:00
parent d8ed429370
commit 17bef05c3c
4 changed files with 53 additions and 6 deletions

View File

@ -88,6 +88,10 @@ public final class WebSecurity extends
private SecurityExpressionHandler<FilterInvocation> expressionHandler = new DefaultWebSecurityExpressionHandler(); private SecurityExpressionHandler<FilterInvocation> expressionHandler = new DefaultWebSecurityExpressionHandler();
private Runnable postBuildAction = new Runnable() {
public void run() {}
};
/** /**
* Creates a new instance * Creates a new instance
* @see WebSecurityConfiguration * @see WebSecurityConfiguration
@ -198,7 +202,7 @@ public final class WebSecurity extends
/** /**
* Set the {@link WebInvocationPrivilegeEvaluator} to be used. If this is * Set the {@link WebInvocationPrivilegeEvaluator} to be used. If this is
* null, then a {@link DefaultWebInvocationPrivilegeEvaluator} will be * null, then a {@link DefaultWebInvocationPrivilegeEvaluator} will be
* created when {@link #setSecurityInterceptor(FilterSecurityInterceptor)} * created when {@link #securityInterceptor(FilterSecurityInterceptor)}
* is non null. * is non null.
* *
* @param privilegeEvaluator * @param privilegeEvaluator
@ -246,9 +250,22 @@ public final class WebSecurity extends
/** /**
* Sets the {@link FilterSecurityInterceptor}. This is typically invoked by {@link WebSecurityConfigurerAdapter}. * Sets the {@link FilterSecurityInterceptor}. This is typically invoked by {@link WebSecurityConfigurerAdapter}.
* @param securityInterceptor the {@link FilterSecurityInterceptor} to use * @param securityInterceptor the {@link FilterSecurityInterceptor} to use
* @return the {@link WebSecurity} for further customizations
*/ */
public void setSecurityInterceptor(FilterSecurityInterceptor securityInterceptor) { public WebSecurity securityInterceptor(FilterSecurityInterceptor securityInterceptor) {
this.filterSecurityInterceptor = securityInterceptor; this.filterSecurityInterceptor = securityInterceptor;
return this;
}
/**
* Executes the Runnable immediately after the build takes place
*
* @param postBuildAction
* @return the {@link WebSecurity} for further customizations
*/
public WebSecurity postBuildAction(Runnable postBuildAction) {
this.postBuildAction = postBuildAction;
return this;
} }
@Override @Override
@ -278,6 +295,7 @@ public final class WebSecurity extends
"********************************************************************\n\n"); "********************************************************************\n\n");
result = new DebugFilter(filterChainProxy); result = new DebugFilter(filterChainProxy);
} }
postBuildAction.run();
return result; return result;
} }

View File

@ -243,12 +243,17 @@ public abstract class WebSecurityConfigurerAdapter implements SecurityConfigurer
} }
@Override @Override
public void init(WebSecurity web) throws Exception { public void init(final WebSecurity web) throws Exception {
HttpSecurity http = getHttp(); final HttpSecurity http = getHttp();
FilterSecurityInterceptor securityInterceptor = http.getSharedObject(FilterSecurityInterceptor.class);
web web
.addSecurityFilterChainBuilder(http) .addSecurityFilterChainBuilder(http)
.setSecurityInterceptor(securityInterceptor); .postBuildAction(new Runnable() {
@Override
public void run() {
FilterSecurityInterceptor securityInterceptor = http.getSharedObject(FilterSecurityInterceptor.class);
web.securityInterceptor(securityInterceptor);
}
});
} }
/** /**
@ -298,6 +303,7 @@ public abstract class WebSecurityConfigurerAdapter implements SecurityConfigurer
this.objectPostProcessor = objectPostProcessor; this.objectPostProcessor = objectPostProcessor;
} }
/** /**
* Delays the use of the {@link AuthenticationManager} build from the * Delays the use of the {@link AuthenticationManager} build from the
* {@link AuthenticationManagerBuilder} to ensure that it has been fully * {@link AuthenticationManagerBuilder} to ensure that it has been fully

View File

@ -106,6 +106,7 @@ abstract class AbstractInterceptUrlConfigurer<H extends HttpSecurityBuilder<H>,C
if(filterSecurityInterceptorOncePerRequest != null) { if(filterSecurityInterceptorOncePerRequest != null) {
securityInterceptor.setObserveOncePerRequest(filterSecurityInterceptorOncePerRequest); securityInterceptor.setObserveOncePerRequest(filterSecurityInterceptorOncePerRequest);
} }
securityInterceptor = postProcess(securityInterceptor);
http.addFilter(securityInterceptor); http.addFilter(securityInterceptor);
http.setSharedObject(FilterSecurityInterceptor.class, securityInterceptor); http.setSharedObject(FilterSecurityInterceptor.class, securityInterceptor);
} }

View File

@ -32,6 +32,7 @@ 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
import org.springframework.security.web.FilterChainProxy import org.springframework.security.web.FilterChainProxy
import org.springframework.security.web.SecurityFilterChain import org.springframework.security.web.SecurityFilterChain
import org.springframework.security.web.access.DefaultWebInvocationPrivilegeEvaluator;
import org.springframework.security.web.access.WebInvocationPrivilegeEvaluator; import org.springframework.security.web.access.WebInvocationPrivilegeEvaluator;
import org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler; import org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler;
import org.springframework.security.web.access.expression.WebSecurityExpressionHandler; import org.springframework.security.web.access.expression.WebSecurityExpressionHandler;
@ -235,4 +236,25 @@ class WebSecurityConfigurationTests extends BaseSpringSpec {
.anyRequest().authenticated() .anyRequest().authenticated()
} }
} }
def "#138 WebInvocationPrivilegeEvaluator defaults"() {
when:
loadConfig(WebInvocationPrivilegeEvaluatorDefaultsConfig)
then:
WebInvocationPrivilegeEvaluator wipe = context.getBean(WebInvocationPrivilegeEvaluator)
wipe instanceof DefaultWebInvocationPrivilegeEvaluator
wipe.securityInterceptor != null
}
@EnableWebSecurity
@Configuration
static class WebInvocationPrivilegeEvaluatorDefaultsConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeUrls()
.anyRequest().authenticated()
}
}
} }