Skip to content

Commit f565b23

Browse files
kse-musicjzheaux
authored andcommitted
Restore Method Parameter Inheritance Support
Closes gh-16177
1 parent 40f8ac6 commit f565b23

File tree

6 files changed

+144
-24
lines changed

6 files changed

+144
-24
lines changed

messaging/src/main/java/org/springframework/security/messaging/context/AuthenticationPrincipalArgumentResolver.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.lang.annotation.Annotation;
2020

2121
import org.springframework.core.MethodParameter;
22+
import org.springframework.core.annotation.AnnotationUtils;
23+
import org.springframework.core.annotation.MergedAnnotations;
2224
import org.springframework.expression.Expression;
2325
import org.springframework.expression.ExpressionParser;
2426
import org.springframework.expression.spel.standard.SpelExpressionParser;
@@ -95,8 +97,12 @@ public final class AuthenticationPrincipalArgumentResolver implements HandlerMet
9597

9698
private ExpressionParser parser = new SpelExpressionParser();
9799

100+
private final Class<AuthenticationPrincipal> annotationType = AuthenticationPrincipal.class;
101+
98102
private SecurityAnnotationScanner<AuthenticationPrincipal> scanner = SecurityAnnotationScanners
99-
.requireUnique(AuthenticationPrincipal.class);
103+
.requireUnique(this.annotationType);
104+
105+
private boolean useAnnotationTemplate = false;
100106

101107
@Override
102108
public boolean supportsParameter(MethodParameter parameter) {
@@ -149,6 +155,7 @@ public void setSecurityContextHolderStrategy(SecurityContextHolderStrategy secur
149155
* @since 6.4
150156
*/
151157
public void setTemplateDefaults(AnnotationTemplateExpressionDefaults templateDefaults) {
158+
this.useAnnotationTemplate = templateDefaults != null;
152159
this.scanner = SecurityAnnotationScanners.requireUnique(AuthenticationPrincipal.class, templateDefaults);
153160
}
154161

@@ -158,9 +165,22 @@ public void setTemplateDefaults(AnnotationTemplateExpressionDefaults templateDef
158165
* @param parameter the {@link MethodParameter} to search for an {@link Annotation}
159166
* @return the {@link Annotation} that was found or null.
160167
*/
161-
@SuppressWarnings("unchecked")
162-
private <T extends Annotation> T findMethodAnnotation(MethodParameter parameter) {
163-
return (T) this.scanner.scan(parameter.getParameter());
168+
private AuthenticationPrincipal findMethodAnnotation(MethodParameter parameter) {
169+
if (this.useAnnotationTemplate) {
170+
return this.scanner.scan(parameter.getParameter());
171+
}
172+
AuthenticationPrincipal annotation = parameter.getParameterAnnotation(this.annotationType);
173+
if (annotation != null) {
174+
return annotation;
175+
}
176+
Annotation[] annotationsToSearch = parameter.getParameterAnnotations();
177+
for (Annotation toSearch : annotationsToSearch) {
178+
annotation = AnnotationUtils.findAnnotation(toSearch.annotationType(), this.annotationType);
179+
if (annotation != null) {
180+
return MergedAnnotations.from(toSearch).get(this.annotationType).synthesize();
181+
}
182+
}
183+
return null;
164184
}
165185

166186
}

messaging/src/main/java/org/springframework/security/messaging/handler/invocation/reactive/AuthenticationPrincipalArgumentResolver.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.springframework.core.ReactiveAdapter;
2626
import org.springframework.core.ReactiveAdapterRegistry;
2727
import org.springframework.core.ResolvableType;
28+
import org.springframework.core.annotation.AnnotationUtils;
29+
import org.springframework.core.annotation.MergedAnnotations;
2830
import org.springframework.expression.BeanResolver;
2931
import org.springframework.expression.Expression;
3032
import org.springframework.expression.ExpressionParser;
@@ -99,8 +101,12 @@ public class AuthenticationPrincipalArgumentResolver implements HandlerMethodArg
99101

100102
private ExpressionParser parser = new SpelExpressionParser();
101103

104+
private final Class<AuthenticationPrincipal> annotationType = AuthenticationPrincipal.class;
105+
102106
private SecurityAnnotationScanner<AuthenticationPrincipal> scanner = SecurityAnnotationScanners
103-
.requireUnique(AuthenticationPrincipal.class);
107+
.requireUnique(this.annotationType);
108+
109+
private boolean useAnnotationTemplate = false;
104110

105111
private BeanResolver beanResolver;
106112

@@ -190,6 +196,7 @@ private boolean isInvalidType(MethodParameter parameter, Object principal) {
190196
* @since 6.4
191197
*/
192198
public void setTemplateDefaults(AnnotationTemplateExpressionDefaults templateDefaults) {
199+
this.useAnnotationTemplate = templateDefaults != null;
193200
this.scanner = SecurityAnnotationScanners.requireUnique(AuthenticationPrincipal.class, templateDefaults);
194201
}
195202

@@ -199,9 +206,22 @@ public void setTemplateDefaults(AnnotationTemplateExpressionDefaults templateDef
199206
* @param parameter the {@link MethodParameter} to search for an {@link Annotation}
200207
* @return the {@link Annotation} that was found or null.
201208
*/
202-
@SuppressWarnings("unchecked")
203-
private <T extends Annotation> T findMethodAnnotation(MethodParameter parameter) {
204-
return (T) this.scanner.scan(parameter.getParameter());
209+
private AuthenticationPrincipal findMethodAnnotation(MethodParameter parameter) {
210+
if (this.useAnnotationTemplate) {
211+
return this.scanner.scan(parameter.getParameter());
212+
}
213+
AuthenticationPrincipal annotation = parameter.getParameterAnnotation(this.annotationType);
214+
if (annotation != null) {
215+
return annotation;
216+
}
217+
Annotation[] annotationsToSearch = parameter.getParameterAnnotations();
218+
for (Annotation toSearch : annotationsToSearch) {
219+
annotation = AnnotationUtils.findAnnotation(toSearch.annotationType(), this.annotationType);
220+
if (annotation != null) {
221+
return MergedAnnotations.from(toSearch).get(this.annotationType).synthesize();
222+
}
223+
}
224+
return null;
205225
}
206226

207227
}

messaging/src/main/java/org/springframework/security/messaging/handler/invocation/reactive/CurrentSecurityContextArgumentResolver.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.springframework.core.ReactiveAdapter;
2626
import org.springframework.core.ReactiveAdapterRegistry;
2727
import org.springframework.core.ResolvableType;
28+
import org.springframework.core.annotation.AnnotationUtils;
29+
import org.springframework.core.annotation.MergedAnnotations;
2830
import org.springframework.expression.BeanResolver;
2931
import org.springframework.expression.Expression;
3032
import org.springframework.expression.ExpressionParser;
@@ -97,8 +99,12 @@ public class CurrentSecurityContextArgumentResolver implements HandlerMethodArgu
9799

98100
private ExpressionParser parser = new SpelExpressionParser();
99101

102+
private final Class<CurrentSecurityContext> annotationType = CurrentSecurityContext.class;
103+
100104
private SecurityAnnotationScanner<CurrentSecurityContext> scanner = SecurityAnnotationScanners
101-
.requireUnique(CurrentSecurityContext.class);
105+
.requireUnique(this.annotationType);
106+
107+
private boolean useAnnotationTemplate = false;
102108

103109
private BeanResolver beanResolver;
104110

@@ -208,6 +214,7 @@ private boolean isInvalidType(MethodParameter parameter, Object value) {
208214
* @since 6.4
209215
*/
210216
public void setTemplateDefaults(AnnotationTemplateExpressionDefaults templateDefaults) {
217+
this.useAnnotationTemplate = templateDefaults != null;
211218
this.scanner = SecurityAnnotationScanners.requireUnique(CurrentSecurityContext.class, templateDefaults);
212219
}
213220

@@ -216,9 +223,22 @@ public void setTemplateDefaults(AnnotationTemplateExpressionDefaults templateDef
216223
* @param parameter the {@link MethodParameter} to search for an {@link Annotation}
217224
* @return the {@link Annotation} that was found or null.
218225
*/
219-
@SuppressWarnings("unchecked")
220-
private <T extends Annotation> T findMethodAnnotation(MethodParameter parameter) {
221-
return (T) this.scanner.scan(parameter.getParameter());
226+
private CurrentSecurityContext findMethodAnnotation(MethodParameter parameter) {
227+
if (this.useAnnotationTemplate) {
228+
return this.scanner.scan(parameter.getParameter());
229+
}
230+
CurrentSecurityContext annotation = parameter.getParameterAnnotation(this.annotationType);
231+
if (annotation != null) {
232+
return annotation;
233+
}
234+
Annotation[] annotationsToSearch = parameter.getParameterAnnotations();
235+
for (Annotation toSearch : annotationsToSearch) {
236+
annotation = AnnotationUtils.findAnnotation(toSearch.annotationType(), this.annotationType);
237+
if (annotation != null) {
238+
return MergedAnnotations.from(toSearch).get(this.annotationType).synthesize();
239+
}
240+
}
241+
return null;
222242
}
223243

224244
}

web/src/main/java/org/springframework/security/web/method/annotation/CurrentSecurityContextArgumentResolver.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.lang.annotation.Annotation;
2020

2121
import org.springframework.core.MethodParameter;
22+
import org.springframework.core.annotation.AnnotationUtils;
23+
import org.springframework.core.annotation.MergedAnnotations;
2224
import org.springframework.expression.BeanResolver;
2325
import org.springframework.expression.Expression;
2426
import org.springframework.expression.ExpressionParser;
@@ -84,8 +86,12 @@ public final class CurrentSecurityContextArgumentResolver implements HandlerMeth
8486

8587
private ExpressionParser parser = new SpelExpressionParser();
8688

89+
private final Class<CurrentSecurityContext> annotationType = CurrentSecurityContext.class;
90+
8791
private SecurityAnnotationScanner<CurrentSecurityContext> scanner = SecurityAnnotationScanners
88-
.requireUnique(CurrentSecurityContext.class);
92+
.requireUnique(this.annotationType);
93+
94+
private boolean useAnnotationTemplate = false;
8995

9096
private BeanResolver beanResolver;
9197

@@ -140,6 +146,7 @@ public void setBeanResolver(BeanResolver beanResolver) {
140146
* @since 6.4
141147
*/
142148
public void setTemplateDefaults(AnnotationTemplateExpressionDefaults templateDefaults) {
149+
this.useAnnotationTemplate = templateDefaults != null;
143150
this.scanner = SecurityAnnotationScanners.requireUnique(CurrentSecurityContext.class, templateDefaults);
144151
}
145152

@@ -171,9 +178,22 @@ private Object resolveSecurityContextFromAnnotation(MethodParameter parameter, C
171178
* @param parameter the {@link MethodParameter} to search for an {@link Annotation}
172179
* @return the {@link Annotation} that was found or null.
173180
*/
174-
@SuppressWarnings("unchecked")
175-
private <T extends Annotation> T findMethodAnnotation(MethodParameter parameter) {
176-
return (T) this.scanner.scan(parameter.getParameter());
181+
private CurrentSecurityContext findMethodAnnotation(MethodParameter parameter) {
182+
if (this.useAnnotationTemplate) {
183+
return this.scanner.scan(parameter.getParameter());
184+
}
185+
CurrentSecurityContext annotation = parameter.getParameterAnnotation(this.annotationType);
186+
if (annotation != null) {
187+
return annotation;
188+
}
189+
Annotation[] annotationsToSearch = parameter.getParameterAnnotations();
190+
for (Annotation toSearch : annotationsToSearch) {
191+
annotation = AnnotationUtils.findAnnotation(toSearch.annotationType(), this.annotationType);
192+
if (annotation != null) {
193+
return MergedAnnotations.from(toSearch).get(this.annotationType).synthesize();
194+
}
195+
}
196+
return null;
177197
}
178198

179199
}

web/src/main/java/org/springframework/security/web/reactive/result/method/annotation/AuthenticationPrincipalArgumentResolver.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.springframework.core.ReactiveAdapter;
2626
import org.springframework.core.ReactiveAdapterRegistry;
2727
import org.springframework.core.ResolvableType;
28+
import org.springframework.core.annotation.AnnotationUtils;
29+
import org.springframework.core.annotation.MergedAnnotations;
2830
import org.springframework.expression.BeanResolver;
2931
import org.springframework.expression.Expression;
3032
import org.springframework.expression.ExpressionParser;
@@ -53,8 +55,12 @@ public class AuthenticationPrincipalArgumentResolver extends HandlerMethodArgume
5355

5456
private ExpressionParser parser = new SpelExpressionParser();
5557

58+
private final Class<AuthenticationPrincipal> annotationType = AuthenticationPrincipal.class;
59+
5660
private SecurityAnnotationScanner<AuthenticationPrincipal> scanner = SecurityAnnotationScanners
57-
.requireUnique(AuthenticationPrincipal.class);
61+
.requireUnique(this.annotationType);
62+
63+
private boolean useAnnotationTemplate = false;
5864

5965
private BeanResolver beanResolver;
6066

@@ -134,6 +140,7 @@ private boolean isInvalidType(MethodParameter parameter, Object principal) {
134140
* @since 6.4
135141
*/
136142
public void setTemplateDefaults(AnnotationTemplateExpressionDefaults templateDefaults) {
143+
this.useAnnotationTemplate = templateDefaults != null;
137144
this.scanner = SecurityAnnotationScanners.requireUnique(AuthenticationPrincipal.class, templateDefaults);
138145
}
139146

@@ -143,9 +150,22 @@ public void setTemplateDefaults(AnnotationTemplateExpressionDefaults templateDef
143150
* @param parameter the {@link MethodParameter} to search for an {@link Annotation}
144151
* @return the {@link Annotation} that was found or null.
145152
*/
146-
@SuppressWarnings("unchecked")
147-
private <T extends Annotation> T findMethodAnnotation(MethodParameter parameter) {
148-
return (T) this.scanner.scan(parameter.getParameter());
153+
private AuthenticationPrincipal findMethodAnnotation(MethodParameter parameter) {
154+
if (this.useAnnotationTemplate) {
155+
return this.scanner.scan(parameter.getParameter());
156+
}
157+
AuthenticationPrincipal annotation = parameter.getParameterAnnotation(this.annotationType);
158+
if (annotation != null) {
159+
return annotation;
160+
}
161+
Annotation[] annotationsToSearch = parameter.getParameterAnnotations();
162+
for (Annotation toSearch : annotationsToSearch) {
163+
annotation = AnnotationUtils.findAnnotation(toSearch.annotationType(), this.annotationType);
164+
if (annotation != null) {
165+
return MergedAnnotations.from(toSearch).get(this.annotationType).synthesize();
166+
}
167+
}
168+
return null;
149169
}
150170

151171
}

web/src/main/java/org/springframework/security/web/reactive/result/method/annotation/CurrentSecurityContextArgumentResolver.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.springframework.core.ReactiveAdapter;
2626
import org.springframework.core.ReactiveAdapterRegistry;
2727
import org.springframework.core.ResolvableType;
28+
import org.springframework.core.annotation.AnnotationUtils;
29+
import org.springframework.core.annotation.MergedAnnotations;
2830
import org.springframework.expression.BeanResolver;
2931
import org.springframework.expression.Expression;
3032
import org.springframework.expression.ExpressionParser;
@@ -53,8 +55,12 @@ public class CurrentSecurityContextArgumentResolver extends HandlerMethodArgumen
5355

5456
private ExpressionParser parser = new SpelExpressionParser();
5557

58+
private final Class<CurrentSecurityContext> annotationType = CurrentSecurityContext.class;
59+
5660
private SecurityAnnotationScanner<CurrentSecurityContext> scanner = SecurityAnnotationScanners
57-
.requireUnique(CurrentSecurityContext.class);
61+
.requireUnique(this.annotationType);
62+
63+
private boolean useAnnotationTemplate = false;
5864

5965
private BeanResolver beanResolver;
6066

@@ -81,6 +87,7 @@ public void setBeanResolver(BeanResolver beanResolver) {
8187
* @since 6.4
8288
*/
8389
public void setTemplateDefaults(AnnotationTemplateExpressionDefaults templateDefaults) {
90+
this.useAnnotationTemplate = templateDefaults != null;
8491
this.scanner = SecurityAnnotationScanners.requireUnique(CurrentSecurityContext.class, templateDefaults);
8592
}
8693

@@ -183,9 +190,22 @@ private boolean isInvalidType(MethodParameter parameter, Object reactiveSecurity
183190
* @param parameter the {@link MethodParameter} to search for an {@link Annotation}
184191
* @return the {@link Annotation} that was found or null.
185192
*/
186-
@SuppressWarnings("unchecked")
187-
private <T extends Annotation> T findMethodAnnotation(MethodParameter parameter) {
188-
return (T) this.scanner.scan(parameter.getParameter());
193+
private CurrentSecurityContext findMethodAnnotation(MethodParameter parameter) {
194+
if (this.useAnnotationTemplate) {
195+
return this.scanner.scan(parameter.getParameter());
196+
}
197+
CurrentSecurityContext annotation = parameter.getParameterAnnotation(this.annotationType);
198+
if (annotation != null) {
199+
return annotation;
200+
}
201+
Annotation[] annotationsToSearch = parameter.getParameterAnnotations();
202+
for (Annotation toSearch : annotationsToSearch) {
203+
annotation = AnnotationUtils.findAnnotation(toSearch.annotationType(), this.annotationType);
204+
if (annotation != null) {
205+
return MergedAnnotations.from(toSearch).get(this.annotationType).synthesize();
206+
}
207+
}
208+
return null;
189209
}
190210

191211
}

0 commit comments

Comments
 (0)