|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2022 the original author or authors. |
| 2 | + * Copyright 2002-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
28 | 28 | import org.springframework.security.core.GrantedAuthority;
|
29 | 29 | import org.springframework.security.core.authority.AuthorityUtils;
|
30 | 30 | import org.springframework.util.Assert;
|
| 31 | +import org.springframework.util.function.SingletonSupplier; |
31 | 32 |
|
32 | 33 | /**
|
33 | 34 | * Base root object for use in Spring Security expression evaluations.
|
@@ -86,7 +87,11 @@ public SecurityExpressionRoot(Authentication authentication) {
|
86 | 87 | * @since 5.8
|
87 | 88 | */
|
88 | 89 | 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 | + }); |
90 | 95 | }
|
91 | 96 |
|
92 | 97 | @Override
|
@@ -236,27 +241,4 @@ private static String getRoleWithDefaultPrefix(String defaultRolePrefix, String
|
236 | 241 | return defaultRolePrefix + role;
|
237 | 242 | }
|
238 | 243 |
|
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 |
| - |
262 | 244 | }
|
0 commit comments