11/*
2- * Copyright 2002-2024 the original author or authors.
2+ * Copyright 2002-2025 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1717package org .springframework .security .authorization .method ;
1818
1919import java .lang .reflect .Method ;
20- import java .util .Arrays ;
21- import java .util .function .BiFunction ;
2220
2321import reactor .util .annotation .NonNull ;
2422
2826import org .springframework .security .core .annotation .AnnotationTemplateExpressionDefaults ;
2927import org .springframework .security .core .annotation .SecurityAnnotationScanner ;
3028import org .springframework .security .core .annotation .SecurityAnnotationScanners ;
31- import org .springframework .util .Assert ;
32- import org .springframework .util .StringUtils ;
3329
3430/**
3531 * For internal use only, as this contract is likely to change.
4036 */
4137final class PostAuthorizeExpressionAttributeRegistry extends AbstractExpressionAttributeRegistry <ExpressionAttribute > {
4238
43- private final MethodAuthorizationDeniedHandler defaultHandler = new ThrowingMethodAuthorizationDeniedHandler ();
44-
45- private final SecurityAnnotationScanner <HandleAuthorizationDenied > handleAuthorizationDeniedScanner = SecurityAnnotationScanners
46- .requireUnique (HandleAuthorizationDenied .class );
47-
48- private BiFunction <String , Class <? extends MethodAuthorizationDeniedHandler >, MethodAuthorizationDeniedHandler > handlerResolver ;
39+ private final MethodAuthorizationDeniedHandlerResolver handlerResolver = new MethodAuthorizationDeniedHandlerResolver (
40+ PostAuthorizeAuthorizationManager .class );
4941
5042 private SecurityAnnotationScanner <PostAuthorize > postAuthorizeScanner = SecurityAnnotationScanners
5143 .requireUnique (PostAuthorize .class );
5244
53- PostAuthorizeExpressionAttributeRegistry () {
54- this .handlerResolver = (beanName , clazz ) -> new ReflectiveMethodAuthorizationDeniedHandler (clazz ,
55- PostAuthorizeAuthorizationManager .class );
56- }
57-
5845 @ NonNull
5946 @ Override
6047 ExpressionAttribute resolveAttribute (Method method , Class <?> targetClass ) {
@@ -63,19 +50,11 @@ ExpressionAttribute resolveAttribute(Method method, Class<?> targetClass) {
6350 return ExpressionAttribute .NULL_ATTRIBUTE ;
6451 }
6552 Expression expression = getExpressionHandler ().getExpressionParser ().parseExpression (postAuthorize .value ());
66- MethodAuthorizationDeniedHandler deniedHandler = resolveHandler (method , targetClass );
53+ MethodAuthorizationDeniedHandler deniedHandler = this .handlerResolver .resolve (method ,
54+ targetClass (method , targetClass ), PostAuthorizeAuthorizationManager .class );
6755 return new PostAuthorizeExpressionAttribute (expression , deniedHandler );
6856 }
6957
70- private MethodAuthorizationDeniedHandler resolveHandler (Method method , Class <?> targetClass ) {
71- Class <?> targetClassToUse = targetClass (method , targetClass );
72- HandleAuthorizationDenied deniedHandler = this .handleAuthorizationDeniedScanner .scan (method , targetClassToUse );
73- if (deniedHandler != null ) {
74- return this .handlerResolver .apply (deniedHandler .handler (), deniedHandler .handlerClass ());
75- }
76- return this .defaultHandler ;
77- }
78-
7958 private PostAuthorize findPostAuthorizeAnnotation (Method method , Class <?> targetClass ) {
8059 Class <?> targetClassToUse = targetClass (method , targetClass );
8160 return this .postAuthorizeScanner .scan (method , targetClassToUse );
@@ -87,31 +66,11 @@ private PostAuthorize findPostAuthorizeAnnotation(Method method, Class<?> target
8766 * @param context the {@link ApplicationContext} to use
8867 */
8968 void setApplicationContext (ApplicationContext context ) {
90- Assert .notNull (context , "context cannot be null" );
91- this .handlerResolver = (beanName , clazz ) -> resolveHandler (context , beanName , clazz );
69+ this .handlerResolver .setContext (context );
9270 }
9371
9472 void setTemplateDefaults (AnnotationTemplateExpressionDefaults templateDefaults ) {
9573 this .postAuthorizeScanner = SecurityAnnotationScanners .requireUnique (PostAuthorize .class , templateDefaults );
9674 }
9775
98- private MethodAuthorizationDeniedHandler resolveHandler (ApplicationContext context , String beanName ,
99- Class <? extends MethodAuthorizationDeniedHandler > handlerClass ) {
100- if (StringUtils .hasText (beanName )) {
101- return context .getBean (beanName , MethodAuthorizationDeniedHandler .class );
102- }
103- if (handlerClass == this .defaultHandler .getClass ()) {
104- return this .defaultHandler ;
105- }
106- String [] beanNames = context .getBeanNamesForType (handlerClass );
107- if (beanNames .length == 0 ) {
108- throw new IllegalStateException ("Could not find a bean of type " + handlerClass .getName ());
109- }
110- if (beanNames .length > 1 ) {
111- throw new IllegalStateException ("Expected to find a single bean of type " + handlerClass .getName ()
112- + " but found " + Arrays .toString (beanNames ));
113- }
114- return context .getBean (beanNames [0 ], handlerClass );
115- }
116-
11776}
0 commit comments