@@ -167,9 +167,13 @@ private void validateAttributeDefs(Collection<ConfigAttribute> attributeDefs) {
167
167
}
168
168
}
169
169
if (unsupportedAttrs .size () != 0 ) {
170
+ this .logger
171
+ .trace ("Did not validate configuration attributes since validateConfigurationAttributes is false" );
170
172
throw new IllegalArgumentException ("Unsupported configuration attributes: " + unsupportedAttrs );
171
173
}
172
- this .logger .debug ("Validated configuration attributes" );
174
+ else {
175
+ this .logger .trace ("Validated configuration attributes" );
176
+ }
173
177
}
174
178
175
179
protected InterceptorStatusToken beforeInvocation (Object object ) {
@@ -186,34 +190,43 @@ protected InterceptorStatusToken beforeInvocation(Object object) {
186
190
+ " was denied as public invocations are not allowed via this interceptor. "
187
191
+ "This indicates a configuration error because the "
188
192
+ "rejectPublicInvocations property is set to 'true'" );
189
- this .logger .debug ("Public object - authentication not attempted" );
193
+ if (this .logger .isDebugEnabled ()) {
194
+ this .logger .debug (LogMessage .format ("Authorized public object %s" , object ));
195
+ }
190
196
publishEvent (new PublicInvocationEvent (object ));
191
197
return null ; // no further work post-invocation
192
198
}
193
- this .logger .debug (LogMessage .format ("Secure object: %s; Attributes: %s" , object , attributes ));
194
199
if (SecurityContextHolder .getContext ().getAuthentication () == null ) {
195
200
credentialsNotFound (this .messages .getMessage ("AbstractSecurityInterceptor.authenticationNotFound" ,
196
201
"An Authentication object was not found in the SecurityContext" ), object , attributes );
197
202
}
198
203
Authentication authenticated = authenticateIfRequired ();
204
+ if (this .logger .isTraceEnabled ()) {
205
+ this .logger .trace (LogMessage .format ("Authorizing %s with attributes %s" , object , attributes ));
206
+ }
199
207
// Attempt authorization
200
208
attemptAuthorization (object , attributes , authenticated );
201
- this .logger .debug ("Authorization successful" );
209
+ if (this .logger .isDebugEnabled ()) {
210
+ this .logger .debug (LogMessage .format ("Authorized %s with attributes %s" , object , attributes ));
211
+ }
202
212
if (this .publishAuthorizationSuccess ) {
203
213
publishEvent (new AuthorizedEvent (object , attributes , authenticated ));
204
214
}
205
215
206
216
// Attempt to run as a different user
207
217
Authentication runAs = this .runAsManager .buildRunAs (authenticated , object , attributes );
208
218
if (runAs != null ) {
209
- this .logger .debug (LogMessage .format ("Switching to RunAs Authentication: %s" , runAs ));
210
219
SecurityContext origCtx = SecurityContextHolder .getContext ();
211
220
SecurityContextHolder .setContext (SecurityContextHolder .createEmptyContext ());
212
221
SecurityContextHolder .getContext ().setAuthentication (runAs );
222
+
223
+ if (this .logger .isDebugEnabled ()) {
224
+ this .logger .debug (LogMessage .format ("Switched to RunAs authentication %s" , runAs ));
225
+ }
213
226
// need to revert to token.Authenticated post-invocation
214
227
return new InterceptorStatusToken (origCtx , true , attributes , object );
215
228
}
216
- this .logger .debug ( "RunAsManager did not change Authentication object " );
229
+ this .logger .trace ( "Did not switch RunAs authentication since RunAsManager returned null " );
217
230
// no further work post-invocation
218
231
return new InterceptorStatusToken (SecurityContextHolder .getContext (), false , attributes , object );
219
232
@@ -225,6 +238,13 @@ private void attemptAuthorization(Object object, Collection<ConfigAttribute> att
225
238
this .accessDecisionManager .decide (authenticated , object , attributes );
226
239
}
227
240
catch (AccessDeniedException ex ) {
241
+ if (this .logger .isTraceEnabled ()) {
242
+ this .logger .trace (LogMessage .format ("Failed to authorize %s with attributes %s using %s" , object ,
243
+ attributes , this .accessDecisionManager ));
244
+ }
245
+ else if (this .logger .isDebugEnabled ()) {
246
+ this .logger .debug (LogMessage .format ("Failed to authorize %s with attributes %s" , object , attributes ));
247
+ }
228
248
publishEvent (new AuthorizationFailureEvent (object , attributes , authenticated , ex ));
229
249
throw ex ;
230
250
}
@@ -239,9 +259,11 @@ private void attemptAuthorization(Object object, Collection<ConfigAttribute> att
239
259
*/
240
260
protected void finallyInvocation (InterceptorStatusToken token ) {
241
261
if (token != null && token .isContextHolderRefreshRequired ()) {
242
- this .logger .debug (LogMessage .of (
243
- () -> "Reverting to original Authentication: " + token .getSecurityContext ().getAuthentication ()));
244
262
SecurityContextHolder .setContext (token .getSecurityContext ());
263
+ if (this .logger .isDebugEnabled ()) {
264
+ this .logger .debug (LogMessage .of (
265
+ () -> "Reverted to original authentication " + token .getSecurityContext ().getAuthentication ()));
266
+ }
245
267
}
246
268
}
247
269
@@ -284,12 +306,16 @@ protected Object afterInvocation(InterceptorStatusToken token, Object returnedOb
284
306
private Authentication authenticateIfRequired () {
285
307
Authentication authentication = SecurityContextHolder .getContext ().getAuthentication ();
286
308
if (authentication .isAuthenticated () && !this .alwaysReauthenticate ) {
287
- this .logger .debug (LogMessage .format ("Previously Authenticated: %s" , authentication ));
309
+ if (this .logger .isTraceEnabled ()) {
310
+ this .logger .trace (LogMessage .format ("Did not re-authenticate %s before authorizing" , authentication ));
311
+ }
288
312
return authentication ;
289
313
}
290
314
authentication = this .authenticationManager .authenticate (authentication );
291
315
// Don't authenticated.setAuthentication(true) because each provider does that
292
- this .logger .debug (LogMessage .format ("Successfully Authenticated: %s" , authentication ));
316
+ if (this .logger .isDebugEnabled ()) {
317
+ this .logger .debug (LogMessage .format ("Re-authenticated %s before authorizing" , authentication ));
318
+ }
293
319
SecurityContextHolder .getContext ().setAuthentication (authentication );
294
320
return authentication ;
295
321
}
0 commit comments