27
27
28
28
import org .springframework .aop .MethodMatcher ;
29
29
import org .springframework .aop .support .JdkRegexpMethodPointcut ;
30
+ import org .springframework .beans .factory .BeanCreationException ;
30
31
import org .springframework .beans .factory .annotation .Autowired ;
31
32
import org .springframework .context .annotation .Bean ;
32
33
import org .springframework .security .access .AccessDeniedException ;
35
36
import org .springframework .security .access .annotation .ExpressionProtectedBusinessServiceImpl ;
36
37
import org .springframework .security .access .expression .method .DefaultMethodSecurityExpressionHandler ;
37
38
import org .springframework .security .access .expression .method .MethodSecurityExpressionHandler ;
39
+ import org .springframework .security .authorization .AuthorizationDecision ;
40
+ import org .springframework .security .authorization .AuthorizationManager ;
38
41
import org .springframework .security .authorization .method .AuthorizationManagerMethodBeforeAdvice ;
39
42
import org .springframework .security .authorization .method .AuthorizationMethodAfterAdvice ;
40
43
import org .springframework .security .authorization .method .AuthorizationMethodBeforeAdvice ;
41
44
import org .springframework .security .authorization .method .MethodAuthorizationContext ;
42
- import org .springframework .security .authorization .AuthorizationDecision ;
43
- import org .springframework .security .authorization .AuthorizationManager ;
44
45
import org .springframework .security .config .test .SpringTestRule ;
45
46
import org .springframework .security .core .Authentication ;
46
47
import org .springframework .security .test .context .annotation .SecurityTestExecutionListeners ;
@@ -103,7 +104,7 @@ public void preAuthorizeNotAnonymousWhenRoleUserThenPasses() {
103
104
@ WithMockUser
104
105
@ Test
105
106
public void securedWhenRoleUserThenAccessDeniedException () {
106
- this .spring .register (MethodSecurityServiceConfig .class ).autowire ();
107
+ this .spring .register (MethodSecurityServiceEnabledConfig .class ).autowire ();
107
108
assertThatExceptionOfType (AccessDeniedException .class ).isThrownBy (this .methodSecurityService ::secured )
108
109
.withMessage ("Access Denied" );
109
110
}
@@ -119,7 +120,7 @@ public void securedWhenRoleAdminThenPasses() {
119
120
@ WithMockUser (roles = "ADMIN" )
120
121
@ Test
121
122
public void securedUserWhenRoleAdminThenAccessDeniedException () {
122
- this .spring .register (MethodSecurityServiceConfig .class ).autowire ();
123
+ this .spring .register (MethodSecurityServiceEnabledConfig .class ).autowire ();
123
124
assertThatExceptionOfType (AccessDeniedException .class ).isThrownBy (this .methodSecurityService ::securedUser )
124
125
.withMessage ("Access Denied" );
125
126
}
@@ -244,15 +245,15 @@ public void securedUserWhenCustomAfterAdviceConfiguredAndNameNotBobThenAccessDen
244
245
@ WithMockUser (roles = "ADMIN" )
245
246
@ Test
246
247
public void jsr250WhenRoleAdminThenAccessDeniedException () {
247
- this .spring .register (MethodSecurityServiceConfig .class ).autowire ();
248
+ this .spring .register (MethodSecurityServiceEnabledConfig .class ).autowire ();
248
249
assertThatExceptionOfType (AccessDeniedException .class ).isThrownBy (this .methodSecurityService ::jsr250 )
249
250
.withMessage ("Access Denied" );
250
251
}
251
252
252
253
@ WithAnonymousUser
253
254
@ Test
254
255
public void jsr250PermitAllWhenRoleAnonymousThenPasses () {
255
- this .spring .register (MethodSecurityServiceConfig .class ).autowire ();
256
+ this .spring .register (MethodSecurityServiceEnabledConfig .class ).autowire ();
256
257
String result = this .methodSecurityService .jsr250PermitAll ();
257
258
assertThat (result ).isNull ();
258
259
}
@@ -272,7 +273,14 @@ public void rolesAllowedUserWhenRoleUserThenPasses() {
272
273
this .businessService .rolesAllowedUser ();
273
274
}
274
275
275
- @ EnableMethodSecurity (securedEnabled = true , jsr250Enabled = true )
276
+ @ Test
277
+ public void configureWhenCustomAdviceAndSecureEnabledThenException () {
278
+ assertThatExceptionOfType (BeanCreationException .class ).isThrownBy (() -> this .spring
279
+ .register (CustomAuthorizationManagerBeforeAdviceConfig .class , MethodSecurityServiceEnabledConfig .class )
280
+ .autowire ());
281
+ }
282
+
283
+ @ EnableMethodSecurity
276
284
static class MethodSecurityServiceConfig {
277
285
278
286
@ Bean
@@ -292,6 +300,16 @@ BusinessService businessService() {
292
300
293
301
}
294
302
303
+ @ EnableMethodSecurity (securedEnabled = true , jsr250Enabled = true )
304
+ static class MethodSecurityServiceEnabledConfig {
305
+
306
+ @ Bean
307
+ MethodSecurityService methodSecurityService () {
308
+ return new MethodSecurityServiceImpl ();
309
+ }
310
+
311
+ }
312
+
295
313
@ EnableMethodSecurity
296
314
static class CustomPermissionEvaluatorConfig {
297
315
0 commit comments