Skip to content

Commit 3369cf5

Browse files
evgeniychebanjzheaux
authored andcommitted
Consider replacing SecurityExpressionRoot.AuthenticationSupplier with SingletonSupplier
Closes gh-12487
1 parent f3ce04e commit 3369cf5

File tree

1 file changed

+7
-25
lines changed

1 file changed

+7
-25
lines changed

core/src/main/java/org/springframework/security/access/expression/SecurityExpressionRoot.java

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -28,6 +28,7 @@
2828
import org.springframework.security.core.GrantedAuthority;
2929
import org.springframework.security.core.authority.AuthorityUtils;
3030
import org.springframework.util.Assert;
31+
import org.springframework.util.function.SingletonSupplier;
3132

3233
/**
3334
* Base root object for use in Spring Security expression evaluations.
@@ -86,7 +87,11 @@ public SecurityExpressionRoot(Authentication authentication) {
8687
* @since 5.8
8788
*/
8889
public SecurityExpressionRoot(Supplier<Authentication> authentication) {
89-
this.authentication = new AuthenticationSupplier(authentication);
90+
this.authentication = SingletonSupplier.of(() -> {
91+
Authentication value = authentication.get();
92+
Assert.notNull(value, "Authentication object cannot be null");
93+
return value;
94+
});
9095
}
9196

9297
@Override
@@ -236,27 +241,4 @@ private static String getRoleWithDefaultPrefix(String defaultRolePrefix, String
236241
return defaultRolePrefix + role;
237242
}
238243

239-
private static final class AuthenticationSupplier implements Supplier<Authentication> {
240-
241-
private Authentication value;
242-
243-
private final Supplier<Authentication> delegate;
244-
245-
private AuthenticationSupplier(Supplier<Authentication> delegate) {
246-
Assert.notNull(delegate, "delegate cannot be null");
247-
this.delegate = delegate;
248-
}
249-
250-
@Override
251-
public Authentication get() {
252-
if (this.value == null) {
253-
Authentication authentication = this.delegate.get();
254-
Assert.notNull(authentication, "Authentication object cannot be null");
255-
this.value = authentication;
256-
}
257-
return this.value;
258-
}
259-
260-
}
261-
262244
}

0 commit comments

Comments
 (0)