22
22
import org .aopalliance .intercept .MethodInterceptor ;
23
23
import org .apache .commons .logging .Log ;
24
24
import org .apache .commons .logging .LogFactory ;
25
+
26
+ import org .springframework .beans .factory .SmartInitializingSingleton ;
25
27
import org .springframework .beans .factory .annotation .Autowired ;
26
28
import org .springframework .context .ApplicationContext ;
27
29
import org .springframework .context .annotation .AdviceMode ;
76
78
* @see EnableGlobalMethodSecurity
77
79
*/
78
80
@ Configuration
79
- public class GlobalMethodSecurityConfiguration implements ImportAware {
81
+ public class GlobalMethodSecurityConfiguration
82
+ implements ImportAware , SmartInitializingSingleton {
80
83
private static final Log logger = LogFactory
81
84
.getLog (GlobalMethodSecurityConfiguration .class );
82
85
private ObjectPostProcessor <Object > objectPostProcessor = new ObjectPostProcessor <Object >() {
@@ -94,6 +97,7 @@ public <T> T postProcess(T object) {
94
97
private ApplicationContext context ;
95
98
private MethodSecurityExpressionHandler expressionHandler ;
96
99
private Jsr250MethodSecurityMetadataSource jsr250MethodSecurityMetadataSource ;
100
+ private MethodSecurityInterceptor methodSecurityInterceptor ;
97
101
98
102
/**
99
103
* Creates the default MethodInterceptor which is a MethodSecurityInterceptor using
@@ -117,18 +121,42 @@ public <T> T postProcess(T object) {
117
121
*/
118
122
@ Bean
119
123
public MethodInterceptor methodSecurityInterceptor () throws Exception {
120
- MethodSecurityInterceptor methodSecurityInterceptor = isAspectJ () ? new AspectJMethodSecurityInterceptor ()
124
+ this .methodSecurityInterceptor = isAspectJ ()
125
+ ? new AspectJMethodSecurityInterceptor ()
121
126
: new MethodSecurityInterceptor ();
122
127
methodSecurityInterceptor .setAccessDecisionManager (accessDecisionManager ());
123
128
methodSecurityInterceptor .setAfterInvocationManager (afterInvocationManager ());
124
- methodSecurityInterceptor .setAuthenticationManager (authenticationManager ());
125
129
methodSecurityInterceptor
126
130
.setSecurityMetadataSource (methodSecurityMetadataSource ());
127
131
RunAsManager runAsManager = runAsManager ();
128
132
if (runAsManager != null ) {
129
133
methodSecurityInterceptor .setRunAsManager (runAsManager );
130
134
}
131
- return methodSecurityInterceptor ;
135
+
136
+ return this .methodSecurityInterceptor ;
137
+ }
138
+
139
+ /*
140
+ * (non-Javadoc)
141
+ *
142
+ * @see org.springframework.beans.factory.SmartInitializingSingleton#
143
+ * afterSingletonsInstantiated()
144
+ */
145
+ @ Override
146
+ public void afterSingletonsInstantiated () {
147
+ try {
148
+ initializeMethodSecurityInterceptor ();
149
+ }
150
+ catch (Exception e ) {
151
+ throw new RuntimeException (e );
152
+ }
153
+ }
154
+
155
+ private void initializeMethodSecurityInterceptor () throws Exception {
156
+ if (this .methodSecurityInterceptor == null ) {
157
+ return ;
158
+ }
159
+ this .methodSecurityInterceptor .setAuthenticationManager (authenticationManager ());
132
160
}
133
161
134
162
/**
0 commit comments