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 ();
4940
5041 private SecurityAnnotationScanner <PostAuthorize > postAuthorizeScanner = SecurityAnnotationScanners
5142 .requireUnique (PostAuthorize .class );
5243
53- PostAuthorizeExpressionAttributeRegistry () {
54- this .handlerResolver = (beanName , clazz ) -> new ReflectiveMethodAuthorizationDeniedHandler (clazz ,
55- PostAuthorizeAuthorizationManager .class );
56- }
57-
5844 @ NonNull
5945 @ Override
6046 ExpressionAttribute resolveAttribute (Method method , Class <?> targetClass ) {
@@ -63,19 +49,11 @@ ExpressionAttribute resolveAttribute(Method method, Class<?> targetClass) {
6349 return ExpressionAttribute .NULL_ATTRIBUTE ;
6450 }
6551 Expression expression = getExpressionHandler ().getExpressionParser ().parseExpression (postAuthorize .value ());
66- MethodAuthorizationDeniedHandler deniedHandler = resolveHandler (method , targetClass );
52+ MethodAuthorizationDeniedHandler deniedHandler = this .handlerResolver .resolve (method ,
53+ targetClass (method , targetClass ), PostAuthorizeAuthorizationManager .class );
6754 return new PostAuthorizeExpressionAttribute (expression , deniedHandler );
6855 }
6956
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-
7957 private PostAuthorize findPostAuthorizeAnnotation (Method method , Class <?> targetClass ) {
8058 Class <?> targetClassToUse = targetClass (method , targetClass );
8159 return this .postAuthorizeScanner .scan (method , targetClassToUse );
@@ -87,31 +65,11 @@ private PostAuthorize findPostAuthorizeAnnotation(Method method, Class<?> target
8765 * @param context the {@link ApplicationContext} to use
8866 */
8967 void setApplicationContext (ApplicationContext context ) {
90- Assert .notNull (context , "context cannot be null" );
91- this .handlerResolver = (beanName , clazz ) -> resolveHandler (context , beanName , clazz );
68+ this .handlerResolver .setContext (context );
9269 }
9370
9471 void setTemplateDefaults (AnnotationTemplateExpressionDefaults templateDefaults ) {
9572 this .postAuthorizeScanner = SecurityAnnotationScanners .requireUnique (PostAuthorize .class , templateDefaults );
9673 }
9774
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-
11775}
0 commit comments