Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove duplicate cache in AuthenticationPrincipalArgumentResolver and CurrentSecurityContextArgumentResolver #16202

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package org.springframework.security.messaging.context;

import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.core.MethodParameter;
import org.springframework.expression.Expression;
Expand Down Expand Up @@ -95,8 +93,6 @@ public final class AuthenticationPrincipalArgumentResolver implements HandlerMet
private SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder
.getContextHolderStrategy();

private final Map<MethodParameter, Annotation> cachedAttributes = new ConcurrentHashMap<>();

private ExpressionParser parser = new SpelExpressionParser();

private SecurityAnnotationScanner<AuthenticationPrincipal> scanner = SecurityAnnotationScanners
Expand Down Expand Up @@ -164,8 +160,7 @@ public void setTemplateDefaults(AnnotationTemplateExpressionDefaults templateDef
*/
@SuppressWarnings("unchecked")
private <T extends Annotation> T findMethodAnnotation(MethodParameter parameter) {
return (T) this.cachedAttributes.computeIfAbsent(parameter,
(methodParameter) -> this.scanner.scan(methodParameter.getParameter()));
return (T) this.scanner.scan(parameter.getParameter());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package org.springframework.security.messaging.handler.invocation.reactive;

import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -99,8 +97,6 @@
*/
public class AuthenticationPrincipalArgumentResolver implements HandlerMethodArgumentResolver {

private final Map<MethodParameter, Annotation> cachedAttributes = new ConcurrentHashMap<>();

private ExpressionParser parser = new SpelExpressionParser();

private SecurityAnnotationScanner<AuthenticationPrincipal> scanner = SecurityAnnotationScanners
Expand Down Expand Up @@ -205,8 +201,7 @@ public void setTemplateDefaults(AnnotationTemplateExpressionDefaults templateDef
*/
@SuppressWarnings("unchecked")
private <T extends Annotation> T findMethodAnnotation(MethodParameter parameter) {
return (T) this.cachedAttributes.computeIfAbsent(parameter,
(methodParameter) -> this.scanner.scan(methodParameter.getParameter()));
return (T) this.scanner.scan(parameter.getParameter());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package org.springframework.security.messaging.handler.invocation.reactive;

import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -97,8 +95,6 @@
*/
public class CurrentSecurityContextArgumentResolver implements HandlerMethodArgumentResolver {

private final Map<MethodParameter, Annotation> cachedAttributes = new ConcurrentHashMap<>();

private ExpressionParser parser = new SpelExpressionParser();

private SecurityAnnotationScanner<CurrentSecurityContext> scanner = SecurityAnnotationScanners
Expand Down Expand Up @@ -222,8 +218,7 @@ public void setTemplateDefaults(AnnotationTemplateExpressionDefaults templateDef
*/
@SuppressWarnings("unchecked")
private <T extends Annotation> T findMethodAnnotation(MethodParameter parameter) {
return (T) this.cachedAttributes.computeIfAbsent(parameter,
(methodParameter) -> this.scanner.scan(methodParameter.getParameter()));
return (T) this.scanner.scan(parameter.getParameter());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package org.springframework.security.web.method.annotation;

import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.core.MethodParameter;
import org.springframework.expression.BeanResolver;
Expand Down Expand Up @@ -98,8 +96,6 @@ public final class AuthenticationPrincipalArgumentResolver implements HandlerMet
private SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder
.getContextHolderStrategy();

private final Map<MethodParameter, Annotation> cachedAttributes = new ConcurrentHashMap<>();

private ExpressionParser parser = new SpelExpressionParser();

private SecurityAnnotationScanner<AuthenticationPrincipal> scanner = SecurityAnnotationScanners
Expand Down Expand Up @@ -179,8 +175,7 @@ public void setTemplateDefaults(AnnotationTemplateExpressionDefaults templateDef
*/
@SuppressWarnings("unchecked")
private <T extends Annotation> T findMethodAnnotation(MethodParameter parameter) {
return (T) this.cachedAttributes.computeIfAbsent(parameter,
(methodParameter) -> this.scanner.scan(methodParameter.getParameter()));
return (T) this.scanner.scan(parameter.getParameter());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package org.springframework.security.web.method.annotation;

import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.core.MethodParameter;
import org.springframework.expression.BeanResolver;
Expand Down Expand Up @@ -84,8 +82,6 @@ public final class CurrentSecurityContextArgumentResolver implements HandlerMeth
private SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder
.getContextHolderStrategy();

private final Map<MethodParameter, Annotation> cachedAttributes = new ConcurrentHashMap<>();

private ExpressionParser parser = new SpelExpressionParser();

private SecurityAnnotationScanner<CurrentSecurityContext> scanner = SecurityAnnotationScanners
Expand Down Expand Up @@ -177,8 +173,7 @@ private Object resolveSecurityContextFromAnnotation(MethodParameter parameter, C
*/
@SuppressWarnings("unchecked")
private <T extends Annotation> T findMethodAnnotation(MethodParameter parameter) {
return (T) this.cachedAttributes.computeIfAbsent(parameter,
(methodParameter) -> this.scanner.scan(methodParameter.getParameter()));
return (T) this.scanner.scan(parameter.getParameter());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package org.springframework.security.web.reactive.result.method.annotation;

import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -53,8 +51,6 @@
*/
public class AuthenticationPrincipalArgumentResolver extends HandlerMethodArgumentResolverSupport {

private final Map<MethodParameter, Annotation> cachedAttributes = new ConcurrentHashMap<>();

private ExpressionParser parser = new SpelExpressionParser();

private SecurityAnnotationScanner<AuthenticationPrincipal> scanner = SecurityAnnotationScanners
Expand Down Expand Up @@ -149,8 +145,7 @@ public void setTemplateDefaults(AnnotationTemplateExpressionDefaults templateDef
*/
@SuppressWarnings("unchecked")
private <T extends Annotation> T findMethodAnnotation(MethodParameter parameter) {
return (T) this.cachedAttributes.computeIfAbsent(parameter,
(methodParameter) -> this.scanner.scan(methodParameter.getParameter()));
return (T) this.scanner.scan(parameter.getParameter());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package org.springframework.security.web.reactive.result.method.annotation;

import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -53,8 +51,6 @@
*/
public class CurrentSecurityContextArgumentResolver extends HandlerMethodArgumentResolverSupport {

private final Map<MethodParameter, Annotation> cachedAttributes = new ConcurrentHashMap<>();

private ExpressionParser parser = new SpelExpressionParser();

private SecurityAnnotationScanner<CurrentSecurityContext> scanner = SecurityAnnotationScanners
Expand Down Expand Up @@ -189,8 +185,7 @@ private boolean isInvalidType(MethodParameter parameter, Object reactiveSecurity
*/
@SuppressWarnings("unchecked")
private <T extends Annotation> T findMethodAnnotation(MethodParameter parameter) {
return (T) this.cachedAttributes.computeIfAbsent(parameter,
(methodParameter) -> this.scanner.scan(methodParameter.getParameter()));
return (T) this.scanner.scan(parameter.getParameter());
}

}