|
16 | 16 |
|
17 | 17 | package org.springframework.security.config.annotation.method.configuration; |
18 | 18 |
|
| 19 | +import io.micrometer.observation.ObservationRegistry; |
| 20 | + |
19 | 21 | import org.springframework.aop.Advisor; |
20 | | -import org.springframework.beans.factory.annotation.Autowired; |
| 22 | +import org.springframework.beans.factory.ObjectProvider; |
21 | 23 | import org.springframework.beans.factory.config.BeanDefinition; |
22 | 24 | import org.springframework.context.ApplicationContext; |
23 | 25 | import org.springframework.context.annotation.Bean; |
|
26 | 28 | import org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler; |
27 | 29 | import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler; |
28 | 30 | import org.springframework.security.authorization.AuthorizationEventPublisher; |
29 | | -import org.springframework.security.authorization.SpringAuthorizationEventPublisher; |
| 31 | +import org.springframework.security.authorization.AuthorizationManager; |
| 32 | +import org.springframework.security.authorization.ObservationAuthorizationManager; |
30 | 33 | import org.springframework.security.authorization.method.AuthorizationManagerAfterMethodInterceptor; |
31 | 34 | import org.springframework.security.authorization.method.AuthorizationManagerBeforeMethodInterceptor; |
32 | 35 | import org.springframework.security.authorization.method.PostAuthorizeAuthorizationManager; |
|
48 | 51 | @Role(BeanDefinition.ROLE_INFRASTRUCTURE) |
49 | 52 | final class PrePostMethodSecurityConfiguration { |
50 | 53 |
|
51 | | - private final PreFilterAuthorizationMethodInterceptor preFilterAuthorizationMethodInterceptor = new PreFilterAuthorizationMethodInterceptor(); |
52 | | - |
53 | | - private final AuthorizationManagerBeforeMethodInterceptor preAuthorizeAuthorizationMethodInterceptor; |
54 | | - |
55 | | - private final PreAuthorizeAuthorizationManager preAuthorizeAuthorizationManager = new PreAuthorizeAuthorizationManager(); |
56 | | - |
57 | | - private final AuthorizationManagerAfterMethodInterceptor postAuthorizeAuthorizaitonMethodInterceptor; |
58 | | - |
59 | | - private final PostAuthorizeAuthorizationManager postAuthorizeAuthorizationManager = new PostAuthorizeAuthorizationManager(); |
60 | | - |
61 | | - private final PostFilterAuthorizationMethodInterceptor postFilterAuthorizationMethodInterceptor = new PostFilterAuthorizationMethodInterceptor(); |
62 | | - |
63 | | - private final DefaultMethodSecurityExpressionHandler expressionHandler = new DefaultMethodSecurityExpressionHandler(); |
64 | | - |
65 | | - @Autowired |
66 | | - PrePostMethodSecurityConfiguration(ApplicationContext context) { |
67 | | - this.preAuthorizeAuthorizationManager.setExpressionHandler(this.expressionHandler); |
68 | | - this.preAuthorizeAuthorizationMethodInterceptor = AuthorizationManagerBeforeMethodInterceptor |
69 | | - .preAuthorize(this.preAuthorizeAuthorizationManager); |
70 | | - this.postAuthorizeAuthorizationManager.setExpressionHandler(this.expressionHandler); |
71 | | - this.postAuthorizeAuthorizaitonMethodInterceptor = AuthorizationManagerAfterMethodInterceptor |
72 | | - .postAuthorize(this.postAuthorizeAuthorizationManager); |
73 | | - this.preFilterAuthorizationMethodInterceptor.setExpressionHandler(this.expressionHandler); |
74 | | - this.postFilterAuthorizationMethodInterceptor.setExpressionHandler(this.expressionHandler); |
75 | | - this.expressionHandler.setApplicationContext(context); |
76 | | - AuthorizationEventPublisher publisher = new SpringAuthorizationEventPublisher(context); |
77 | | - this.preAuthorizeAuthorizationMethodInterceptor.setAuthorizationEventPublisher(publisher); |
78 | | - this.postAuthorizeAuthorizaitonMethodInterceptor.setAuthorizationEventPublisher(publisher); |
79 | | - } |
80 | | - |
81 | 54 | @Bean |
82 | 55 | @Role(BeanDefinition.ROLE_INFRASTRUCTURE) |
83 | | - Advisor preFilterAuthorizationMethodInterceptor() { |
84 | | - return this.preFilterAuthorizationMethodInterceptor; |
| 56 | + static Advisor preFilterAuthorizationMethodInterceptor(ObjectProvider<GrantedAuthorityDefaults> defaultsProvider, |
| 57 | + ObjectProvider<MethodSecurityExpressionHandler> expressionHandlerProvider, |
| 58 | + ObjectProvider<SecurityContextHolderStrategy> strategyProvider, ApplicationContext context) { |
| 59 | + PreFilterAuthorizationMethodInterceptor preFilter = new PreFilterAuthorizationMethodInterceptor(); |
| 60 | + strategyProvider.ifAvailable(preFilter::setSecurityContextHolderStrategy); |
| 61 | + preFilter.setExpressionHandler( |
| 62 | + expressionHandlerProvider.getIfAvailable(() -> defaultExpressionHandler(defaultsProvider, context))); |
| 63 | + return preFilter; |
85 | 64 | } |
86 | 65 |
|
87 | 66 | @Bean |
88 | 67 | @Role(BeanDefinition.ROLE_INFRASTRUCTURE) |
89 | | - Advisor preAuthorizeAuthorizationMethodInterceptor() { |
90 | | - return this.preAuthorizeAuthorizationMethodInterceptor; |
| 68 | + static Advisor preAuthorizeAuthorizationMethodInterceptor(ObjectProvider<GrantedAuthorityDefaults> defaultsProvider, |
| 69 | + ObjectProvider<MethodSecurityExpressionHandler> expressionHandlerProvider, |
| 70 | + ObjectProvider<SecurityContextHolderStrategy> strategyProvider, |
| 71 | + ObjectProvider<AuthorizationEventPublisher> eventPublisherProvider, |
| 72 | + ObjectProvider<ObservationRegistry> registryProvider, ApplicationContext context) { |
| 73 | + PreAuthorizeAuthorizationManager manager = new PreAuthorizeAuthorizationManager(); |
| 74 | + manager.setExpressionHandler( |
| 75 | + expressionHandlerProvider.getIfAvailable(() -> defaultExpressionHandler(defaultsProvider, context))); |
| 76 | + AuthorizationManagerBeforeMethodInterceptor preAuthorize = AuthorizationManagerBeforeMethodInterceptor |
| 77 | + .preAuthorize(manager(manager, registryProvider)); |
| 78 | + strategyProvider.ifAvailable(preAuthorize::setSecurityContextHolderStrategy); |
| 79 | + eventPublisherProvider.ifAvailable(preAuthorize::setAuthorizationEventPublisher); |
| 80 | + return preAuthorize; |
91 | 81 | } |
92 | 82 |
|
93 | 83 | @Bean |
94 | 84 | @Role(BeanDefinition.ROLE_INFRASTRUCTURE) |
95 | | - Advisor postAuthorizeAuthorizationMethodInterceptor() { |
96 | | - return this.postAuthorizeAuthorizaitonMethodInterceptor; |
| 85 | + static Advisor postAuthorizeAuthorizationMethodInterceptor( |
| 86 | + ObjectProvider<GrantedAuthorityDefaults> defaultsProvider, |
| 87 | + ObjectProvider<MethodSecurityExpressionHandler> expressionHandlerProvider, |
| 88 | + ObjectProvider<SecurityContextHolderStrategy> strategyProvider, |
| 89 | + ObjectProvider<AuthorizationEventPublisher> eventPublisherProvider, |
| 90 | + ObjectProvider<ObservationRegistry> registryProvider, ApplicationContext context) { |
| 91 | + PostAuthorizeAuthorizationManager manager = new PostAuthorizeAuthorizationManager(); |
| 92 | + manager.setExpressionHandler( |
| 93 | + expressionHandlerProvider.getIfAvailable(() -> defaultExpressionHandler(defaultsProvider, context))); |
| 94 | + AuthorizationManagerAfterMethodInterceptor postAuthorize = AuthorizationManagerAfterMethodInterceptor |
| 95 | + .postAuthorize(manager(manager, registryProvider)); |
| 96 | + strategyProvider.ifAvailable(postAuthorize::setSecurityContextHolderStrategy); |
| 97 | + eventPublisherProvider.ifAvailable(postAuthorize::setAuthorizationEventPublisher); |
| 98 | + return postAuthorize; |
97 | 99 | } |
98 | 100 |
|
99 | 101 | @Bean |
100 | 102 | @Role(BeanDefinition.ROLE_INFRASTRUCTURE) |
101 | | - Advisor postFilterAuthorizationMethodInterceptor() { |
102 | | - return this.postFilterAuthorizationMethodInterceptor; |
103 | | - } |
104 | | - |
105 | | - @Autowired(required = false) |
106 | | - void setMethodSecurityExpressionHandler(MethodSecurityExpressionHandler methodSecurityExpressionHandler) { |
107 | | - this.preFilterAuthorizationMethodInterceptor.setExpressionHandler(methodSecurityExpressionHandler); |
108 | | - this.preAuthorizeAuthorizationManager.setExpressionHandler(methodSecurityExpressionHandler); |
109 | | - this.postAuthorizeAuthorizationManager.setExpressionHandler(methodSecurityExpressionHandler); |
110 | | - this.postFilterAuthorizationMethodInterceptor.setExpressionHandler(methodSecurityExpressionHandler); |
111 | | - } |
112 | | - |
113 | | - @Autowired(required = false) |
114 | | - void setSecurityContextHolderStrategy(SecurityContextHolderStrategy strategy) { |
115 | | - this.preFilterAuthorizationMethodInterceptor.setSecurityContextHolderStrategy(strategy); |
116 | | - this.preAuthorizeAuthorizationMethodInterceptor.setSecurityContextHolderStrategy(strategy); |
117 | | - this.postAuthorizeAuthorizaitonMethodInterceptor.setSecurityContextHolderStrategy(strategy); |
118 | | - this.postFilterAuthorizationMethodInterceptor.setSecurityContextHolderStrategy(strategy); |
| 103 | + static Advisor postFilterAuthorizationMethodInterceptor(ObjectProvider<GrantedAuthorityDefaults> defaultsProvider, |
| 104 | + ObjectProvider<MethodSecurityExpressionHandler> expressionHandlerProvider, |
| 105 | + ObjectProvider<SecurityContextHolderStrategy> strategyProvider, ApplicationContext context) { |
| 106 | + PostFilterAuthorizationMethodInterceptor postFilter = new PostFilterAuthorizationMethodInterceptor(); |
| 107 | + strategyProvider.ifAvailable(postFilter::setSecurityContextHolderStrategy); |
| 108 | + postFilter.setExpressionHandler( |
| 109 | + expressionHandlerProvider.getIfAvailable(() -> defaultExpressionHandler(defaultsProvider, context))); |
| 110 | + return postFilter; |
119 | 111 | } |
120 | 112 |
|
121 | | - @Autowired(required = false) |
122 | | - void setGrantedAuthorityDefaults(GrantedAuthorityDefaults grantedAuthorityDefaults) { |
123 | | - this.expressionHandler.setDefaultRolePrefix(grantedAuthorityDefaults.getRolePrefix()); |
| 113 | + private static MethodSecurityExpressionHandler defaultExpressionHandler( |
| 114 | + ObjectProvider<GrantedAuthorityDefaults> defaultsProvider, ApplicationContext context) { |
| 115 | + DefaultMethodSecurityExpressionHandler handler = new DefaultMethodSecurityExpressionHandler(); |
| 116 | + defaultsProvider.ifAvailable((d) -> handler.setDefaultRolePrefix(d.getRolePrefix())); |
| 117 | + handler.setApplicationContext(context); |
| 118 | + return handler; |
124 | 119 | } |
125 | 120 |
|
126 | | - @Autowired(required = false) |
127 | | - void setAuthorizationEventPublisher(AuthorizationEventPublisher eventPublisher) { |
128 | | - this.preAuthorizeAuthorizationMethodInterceptor.setAuthorizationEventPublisher(eventPublisher); |
129 | | - this.postAuthorizeAuthorizaitonMethodInterceptor.setAuthorizationEventPublisher(eventPublisher); |
| 121 | + static <T> AuthorizationManager<T> manager(AuthorizationManager<T> delegate, |
| 122 | + ObjectProvider<ObservationRegistry> registryProvider) { |
| 123 | + ObservationRegistry registry = registryProvider.getIfAvailable(() -> ObservationRegistry.NOOP); |
| 124 | + if (registry.isNoop()) { |
| 125 | + return delegate; |
| 126 | + } |
| 127 | + return new ObservationAuthorizationManager<>(registry, delegate); |
130 | 128 | } |
131 | 129 |
|
132 | 130 | } |
0 commit comments