Skip to content

Commit 0b996c6

Browse files
committed
SEC-2424: Document ObjectPostProcessor
1 parent 13c5af5 commit 0b996c6

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2002-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.config.annotation.web.configurers;
17+
18+
import org.springframework.context.ApplicationListener;
19+
import org.springframework.context.annotation.Bean;
20+
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.security.access.event.AuthorizedEvent;
22+
import org.springframework.security.config.annotation.ObjectPostProcessor;
23+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
24+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
25+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
26+
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
27+
28+
@Configuration
29+
@EnableWebSecurity
30+
public class AuthorizedRequestsWithPostProcessorConfig extends WebSecurityConfigurerAdapter {
31+
static ApplicationListener<AuthorizedEvent> AL;
32+
33+
@Override
34+
protected void configure(HttpSecurity http) throws Exception {
35+
http
36+
.authorizeRequests()
37+
.anyRequest().permitAll()
38+
.withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {
39+
public <O extends FilterSecurityInterceptor> O postProcess(
40+
O fsi) {
41+
fsi.setPublishAuthorizationSuccess(true);
42+
return fsi;
43+
}
44+
});
45+
}
46+
47+
@Bean
48+
public ApplicationListener<AuthorizedEvent> applicationListener() {
49+
return AL;
50+
}
51+
}

config/src/test/groovy/org/springframework/security/config/annotation/web/configurers/ExpressionUrlAuthorizationsTests.groovy

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ import static org.springframework.security.config.annotation.web.configurers.Exp
2020
import javax.servlet.http.HttpServletResponse
2121

2222
import org.springframework.beans.factory.BeanCreationException
23+
import org.springframework.context.ApplicationListener
2324
import org.springframework.context.annotation.Configuration
25+
import org.springframework.security.access.event.AuthorizedEvent
2426
import org.springframework.security.access.vote.AffirmativeBased
2527
import org.springframework.security.authentication.RememberMeAuthenticationToken
2628
import org.springframework.security.config.annotation.BaseSpringSpec
@@ -462,4 +464,15 @@ public class ExpressionUrlAuthorizationConfigurerTests extends BaseSpringSpec {
462464
then:
463465
noExceptionThrown()
464466
}
467+
468+
def "AuthorizedRequests withPostProcessor"() {
469+
setup:
470+
ApplicationListener al = Mock()
471+
AuthorizedRequestsWithPostProcessorConfig.AL = al
472+
loadConfig(AuthorizedRequestsWithPostProcessorConfig)
473+
when:
474+
springSecurityFilterChain.doFilter(request, response, chain)
475+
then:
476+
1 * al.onApplicationEvent(_ as AuthorizedEvent)
477+
}
465478
}

docs/manual/src/asciidoc/index.adoc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,29 @@ public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
730730

731731
For additional information about methods that can be overriden, refer to the `GlobalMethodSecurityConfiguration` Javadoc.
732732

733+
=== Post Processing Configured Objects
734+
735+
Spring Security's Java Configuration does not expose every property of every object that it configures. This simplifies the configuration for a majority of users. Afterall, if every property was exposed, users could use standard bean configuration.
736+
737+
While there are good reasons to not directly expose every property, users may still need more advanced configuration options. To address this Spring Security introduces the concept of an `ObjectPostProcessor` which can used to modify or replace many of the Object instances created by the Java Configuration. For example, if you wanted to configure the `filterSecurityPublishAuthorizationSuccess` property on `FilterSecurityInterceptor` you could use the following:
738+
739+
[source,java]
740+
----
741+
@Override
742+
protected void configure(HttpSecurity http) throws Exception {
743+
http
744+
.authorizeRequests()
745+
.anyRequest().authenticated()
746+
.withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {
747+
public <O extends FilterSecurityInterceptor> O postProcess(
748+
O fsi) {
749+
fsi.setPublishAuthorizationSuccess(true);
750+
return fsi;
751+
}
752+
});
753+
}
754+
----
755+
733756
[[ns-config]]
734757
== Security Namespace Configuration
735758

0 commit comments

Comments
 (0)