Browse Source

Restore Method Parameter Inheritance Support

Closes gh-16177
pull/16255/head
DingHao 1 year ago committed by Josh Cummings
parent
commit
f565b23b51
  1. 28
      messaging/src/main/java/org/springframework/security/messaging/context/AuthenticationPrincipalArgumentResolver.java
  2. 28
      messaging/src/main/java/org/springframework/security/messaging/handler/invocation/reactive/AuthenticationPrincipalArgumentResolver.java
  3. 28
      messaging/src/main/java/org/springframework/security/messaging/handler/invocation/reactive/CurrentSecurityContextArgumentResolver.java
  4. 28
      web/src/main/java/org/springframework/security/web/method/annotation/CurrentSecurityContextArgumentResolver.java
  5. 28
      web/src/main/java/org/springframework/security/web/reactive/result/method/annotation/AuthenticationPrincipalArgumentResolver.java
  6. 28
      web/src/main/java/org/springframework/security/web/reactive/result/method/annotation/CurrentSecurityContextArgumentResolver.java

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

@ -19,6 +19,8 @@ package org.springframework.security.messaging.context; @@ -19,6 +19,8 @@ package org.springframework.security.messaging.context;
import java.lang.annotation.Annotation;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
@ -95,8 +97,12 @@ public final class AuthenticationPrincipalArgumentResolver implements HandlerMet @@ -95,8 +97,12 @@ public final class AuthenticationPrincipalArgumentResolver implements HandlerMet
private ExpressionParser parser = new SpelExpressionParser();
private final Class<AuthenticationPrincipal> annotationType = AuthenticationPrincipal.class;
private SecurityAnnotationScanner<AuthenticationPrincipal> scanner = SecurityAnnotationScanners
.requireUnique(AuthenticationPrincipal.class);
.requireUnique(this.annotationType);
private boolean useAnnotationTemplate = false;
@Override
public boolean supportsParameter(MethodParameter parameter) {
@ -149,6 +155,7 @@ public final class AuthenticationPrincipalArgumentResolver implements HandlerMet @@ -149,6 +155,7 @@ public final class AuthenticationPrincipalArgumentResolver implements HandlerMet
* @since 6.4
*/
public void setTemplateDefaults(AnnotationTemplateExpressionDefaults templateDefaults) {
this.useAnnotationTemplate = templateDefaults != null;
this.scanner = SecurityAnnotationScanners.requireUnique(AuthenticationPrincipal.class, templateDefaults);
}
@ -158,9 +165,22 @@ public final class AuthenticationPrincipalArgumentResolver implements HandlerMet @@ -158,9 +165,22 @@ public final class AuthenticationPrincipalArgumentResolver implements HandlerMet
* @param parameter the {@link MethodParameter} to search for an {@link Annotation}
* @return the {@link Annotation} that was found or null.
*/
@SuppressWarnings("unchecked")
private <T extends Annotation> T findMethodAnnotation(MethodParameter parameter) {
return (T) this.scanner.scan(parameter.getParameter());
private AuthenticationPrincipal findMethodAnnotation(MethodParameter parameter) {
if (this.useAnnotationTemplate) {
return this.scanner.scan(parameter.getParameter());
}
AuthenticationPrincipal annotation = parameter.getParameterAnnotation(this.annotationType);
if (annotation != null) {
return annotation;
}
Annotation[] annotationsToSearch = parameter.getParameterAnnotations();
for (Annotation toSearch : annotationsToSearch) {
annotation = AnnotationUtils.findAnnotation(toSearch.annotationType(), this.annotationType);
if (annotation != null) {
return MergedAnnotations.from(toSearch).get(this.annotationType).synthesize();
}
}
return null;
}
}

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

@ -25,6 +25,8 @@ import org.springframework.core.MethodParameter; @@ -25,6 +25,8 @@ import org.springframework.core.MethodParameter;
import org.springframework.core.ReactiveAdapter;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.expression.BeanResolver;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
@ -99,8 +101,12 @@ public class AuthenticationPrincipalArgumentResolver implements HandlerMethodArg @@ -99,8 +101,12 @@ public class AuthenticationPrincipalArgumentResolver implements HandlerMethodArg
private ExpressionParser parser = new SpelExpressionParser();
private final Class<AuthenticationPrincipal> annotationType = AuthenticationPrincipal.class;
private SecurityAnnotationScanner<AuthenticationPrincipal> scanner = SecurityAnnotationScanners
.requireUnique(AuthenticationPrincipal.class);
.requireUnique(this.annotationType);
private boolean useAnnotationTemplate = false;
private BeanResolver beanResolver;
@ -190,6 +196,7 @@ public class AuthenticationPrincipalArgumentResolver implements HandlerMethodArg @@ -190,6 +196,7 @@ public class AuthenticationPrincipalArgumentResolver implements HandlerMethodArg
* @since 6.4
*/
public void setTemplateDefaults(AnnotationTemplateExpressionDefaults templateDefaults) {
this.useAnnotationTemplate = templateDefaults != null;
this.scanner = SecurityAnnotationScanners.requireUnique(AuthenticationPrincipal.class, templateDefaults);
}
@ -199,9 +206,22 @@ public class AuthenticationPrincipalArgumentResolver implements HandlerMethodArg @@ -199,9 +206,22 @@ public class AuthenticationPrincipalArgumentResolver implements HandlerMethodArg
* @param parameter the {@link MethodParameter} to search for an {@link Annotation}
* @return the {@link Annotation} that was found or null.
*/
@SuppressWarnings("unchecked")
private <T extends Annotation> T findMethodAnnotation(MethodParameter parameter) {
return (T) this.scanner.scan(parameter.getParameter());
private AuthenticationPrincipal findMethodAnnotation(MethodParameter parameter) {
if (this.useAnnotationTemplate) {
return this.scanner.scan(parameter.getParameter());
}
AuthenticationPrincipal annotation = parameter.getParameterAnnotation(this.annotationType);
if (annotation != null) {
return annotation;
}
Annotation[] annotationsToSearch = parameter.getParameterAnnotations();
for (Annotation toSearch : annotationsToSearch) {
annotation = AnnotationUtils.findAnnotation(toSearch.annotationType(), this.annotationType);
if (annotation != null) {
return MergedAnnotations.from(toSearch).get(this.annotationType).synthesize();
}
}
return null;
}
}

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

@ -25,6 +25,8 @@ import org.springframework.core.MethodParameter; @@ -25,6 +25,8 @@ import org.springframework.core.MethodParameter;
import org.springframework.core.ReactiveAdapter;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.expression.BeanResolver;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
@ -97,8 +99,12 @@ public class CurrentSecurityContextArgumentResolver implements HandlerMethodArgu @@ -97,8 +99,12 @@ public class CurrentSecurityContextArgumentResolver implements HandlerMethodArgu
private ExpressionParser parser = new SpelExpressionParser();
private final Class<CurrentSecurityContext> annotationType = CurrentSecurityContext.class;
private SecurityAnnotationScanner<CurrentSecurityContext> scanner = SecurityAnnotationScanners
.requireUnique(CurrentSecurityContext.class);
.requireUnique(this.annotationType);
private boolean useAnnotationTemplate = false;
private BeanResolver beanResolver;
@ -208,6 +214,7 @@ public class CurrentSecurityContextArgumentResolver implements HandlerMethodArgu @@ -208,6 +214,7 @@ public class CurrentSecurityContextArgumentResolver implements HandlerMethodArgu
* @since 6.4
*/
public void setTemplateDefaults(AnnotationTemplateExpressionDefaults templateDefaults) {
this.useAnnotationTemplate = templateDefaults != null;
this.scanner = SecurityAnnotationScanners.requireUnique(CurrentSecurityContext.class, templateDefaults);
}
@ -216,9 +223,22 @@ public class CurrentSecurityContextArgumentResolver implements HandlerMethodArgu @@ -216,9 +223,22 @@ public class CurrentSecurityContextArgumentResolver implements HandlerMethodArgu
* @param parameter the {@link MethodParameter} to search for an {@link Annotation}
* @return the {@link Annotation} that was found or null.
*/
@SuppressWarnings("unchecked")
private <T extends Annotation> T findMethodAnnotation(MethodParameter parameter) {
return (T) this.scanner.scan(parameter.getParameter());
private CurrentSecurityContext findMethodAnnotation(MethodParameter parameter) {
if (this.useAnnotationTemplate) {
return this.scanner.scan(parameter.getParameter());
}
CurrentSecurityContext annotation = parameter.getParameterAnnotation(this.annotationType);
if (annotation != null) {
return annotation;
}
Annotation[] annotationsToSearch = parameter.getParameterAnnotations();
for (Annotation toSearch : annotationsToSearch) {
annotation = AnnotationUtils.findAnnotation(toSearch.annotationType(), this.annotationType);
if (annotation != null) {
return MergedAnnotations.from(toSearch).get(this.annotationType).synthesize();
}
}
return null;
}
}

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

@ -19,6 +19,8 @@ package org.springframework.security.web.method.annotation; @@ -19,6 +19,8 @@ package org.springframework.security.web.method.annotation;
import java.lang.annotation.Annotation;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.expression.BeanResolver;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
@ -84,8 +86,12 @@ public final class CurrentSecurityContextArgumentResolver implements HandlerMeth @@ -84,8 +86,12 @@ public final class CurrentSecurityContextArgumentResolver implements HandlerMeth
private ExpressionParser parser = new SpelExpressionParser();
private final Class<CurrentSecurityContext> annotationType = CurrentSecurityContext.class;
private SecurityAnnotationScanner<CurrentSecurityContext> scanner = SecurityAnnotationScanners
.requireUnique(CurrentSecurityContext.class);
.requireUnique(this.annotationType);
private boolean useAnnotationTemplate = false;
private BeanResolver beanResolver;
@ -140,6 +146,7 @@ public final class CurrentSecurityContextArgumentResolver implements HandlerMeth @@ -140,6 +146,7 @@ public final class CurrentSecurityContextArgumentResolver implements HandlerMeth
* @since 6.4
*/
public void setTemplateDefaults(AnnotationTemplateExpressionDefaults templateDefaults) {
this.useAnnotationTemplate = templateDefaults != null;
this.scanner = SecurityAnnotationScanners.requireUnique(CurrentSecurityContext.class, templateDefaults);
}
@ -171,9 +178,22 @@ public final class CurrentSecurityContextArgumentResolver implements HandlerMeth @@ -171,9 +178,22 @@ public final class CurrentSecurityContextArgumentResolver implements HandlerMeth
* @param parameter the {@link MethodParameter} to search for an {@link Annotation}
* @return the {@link Annotation} that was found or null.
*/
@SuppressWarnings("unchecked")
private <T extends Annotation> T findMethodAnnotation(MethodParameter parameter) {
return (T) this.scanner.scan(parameter.getParameter());
private CurrentSecurityContext findMethodAnnotation(MethodParameter parameter) {
if (this.useAnnotationTemplate) {
return this.scanner.scan(parameter.getParameter());
}
CurrentSecurityContext annotation = parameter.getParameterAnnotation(this.annotationType);
if (annotation != null) {
return annotation;
}
Annotation[] annotationsToSearch = parameter.getParameterAnnotations();
for (Annotation toSearch : annotationsToSearch) {
annotation = AnnotationUtils.findAnnotation(toSearch.annotationType(), this.annotationType);
if (annotation != null) {
return MergedAnnotations.from(toSearch).get(this.annotationType).synthesize();
}
}
return null;
}
}

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

@ -25,6 +25,8 @@ import org.springframework.core.MethodParameter; @@ -25,6 +25,8 @@ import org.springframework.core.MethodParameter;
import org.springframework.core.ReactiveAdapter;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.expression.BeanResolver;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
@ -53,8 +55,12 @@ public class AuthenticationPrincipalArgumentResolver extends HandlerMethodArgume @@ -53,8 +55,12 @@ public class AuthenticationPrincipalArgumentResolver extends HandlerMethodArgume
private ExpressionParser parser = new SpelExpressionParser();
private final Class<AuthenticationPrincipal> annotationType = AuthenticationPrincipal.class;
private SecurityAnnotationScanner<AuthenticationPrincipal> scanner = SecurityAnnotationScanners
.requireUnique(AuthenticationPrincipal.class);
.requireUnique(this.annotationType);
private boolean useAnnotationTemplate = false;
private BeanResolver beanResolver;
@ -134,6 +140,7 @@ public class AuthenticationPrincipalArgumentResolver extends HandlerMethodArgume @@ -134,6 +140,7 @@ public class AuthenticationPrincipalArgumentResolver extends HandlerMethodArgume
* @since 6.4
*/
public void setTemplateDefaults(AnnotationTemplateExpressionDefaults templateDefaults) {
this.useAnnotationTemplate = templateDefaults != null;
this.scanner = SecurityAnnotationScanners.requireUnique(AuthenticationPrincipal.class, templateDefaults);
}
@ -143,9 +150,22 @@ public class AuthenticationPrincipalArgumentResolver extends HandlerMethodArgume @@ -143,9 +150,22 @@ public class AuthenticationPrincipalArgumentResolver extends HandlerMethodArgume
* @param parameter the {@link MethodParameter} to search for an {@link Annotation}
* @return the {@link Annotation} that was found or null.
*/
@SuppressWarnings("unchecked")
private <T extends Annotation> T findMethodAnnotation(MethodParameter parameter) {
return (T) this.scanner.scan(parameter.getParameter());
private AuthenticationPrincipal findMethodAnnotation(MethodParameter parameter) {
if (this.useAnnotationTemplate) {
return this.scanner.scan(parameter.getParameter());
}
AuthenticationPrincipal annotation = parameter.getParameterAnnotation(this.annotationType);
if (annotation != null) {
return annotation;
}
Annotation[] annotationsToSearch = parameter.getParameterAnnotations();
for (Annotation toSearch : annotationsToSearch) {
annotation = AnnotationUtils.findAnnotation(toSearch.annotationType(), this.annotationType);
if (annotation != null) {
return MergedAnnotations.from(toSearch).get(this.annotationType).synthesize();
}
}
return null;
}
}

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

@ -25,6 +25,8 @@ import org.springframework.core.MethodParameter; @@ -25,6 +25,8 @@ import org.springframework.core.MethodParameter;
import org.springframework.core.ReactiveAdapter;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.expression.BeanResolver;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
@ -53,8 +55,12 @@ public class CurrentSecurityContextArgumentResolver extends HandlerMethodArgumen @@ -53,8 +55,12 @@ public class CurrentSecurityContextArgumentResolver extends HandlerMethodArgumen
private ExpressionParser parser = new SpelExpressionParser();
private final Class<CurrentSecurityContext> annotationType = CurrentSecurityContext.class;
private SecurityAnnotationScanner<CurrentSecurityContext> scanner = SecurityAnnotationScanners
.requireUnique(CurrentSecurityContext.class);
.requireUnique(this.annotationType);
private boolean useAnnotationTemplate = false;
private BeanResolver beanResolver;
@ -81,6 +87,7 @@ public class CurrentSecurityContextArgumentResolver extends HandlerMethodArgumen @@ -81,6 +87,7 @@ public class CurrentSecurityContextArgumentResolver extends HandlerMethodArgumen
* @since 6.4
*/
public void setTemplateDefaults(AnnotationTemplateExpressionDefaults templateDefaults) {
this.useAnnotationTemplate = templateDefaults != null;
this.scanner = SecurityAnnotationScanners.requireUnique(CurrentSecurityContext.class, templateDefaults);
}
@ -183,9 +190,22 @@ public class CurrentSecurityContextArgumentResolver extends HandlerMethodArgumen @@ -183,9 +190,22 @@ public class CurrentSecurityContextArgumentResolver extends HandlerMethodArgumen
* @param parameter the {@link MethodParameter} to search for an {@link Annotation}
* @return the {@link Annotation} that was found or null.
*/
@SuppressWarnings("unchecked")
private <T extends Annotation> T findMethodAnnotation(MethodParameter parameter) {
return (T) this.scanner.scan(parameter.getParameter());
private CurrentSecurityContext findMethodAnnotation(MethodParameter parameter) {
if (this.useAnnotationTemplate) {
return this.scanner.scan(parameter.getParameter());
}
CurrentSecurityContext annotation = parameter.getParameterAnnotation(this.annotationType);
if (annotation != null) {
return annotation;
}
Annotation[] annotationsToSearch = parameter.getParameterAnnotations();
for (Annotation toSearch : annotationsToSearch) {
annotation = AnnotationUtils.findAnnotation(toSearch.annotationType(), this.annotationType);
if (annotation != null) {
return MergedAnnotations.from(toSearch).get(this.annotationType).synthesize();
}
}
return null;
}
}

Loading…
Cancel
Save