diff --git a/core/src/main/java/org/springframework/security/access/expression/SecurityExpressionRoot.java b/core/src/main/java/org/springframework/security/access/expression/SecurityExpressionRoot.java index db26a13b35..a4a2aff309 100644 --- a/core/src/main/java/org/springframework/security/access/expression/SecurityExpressionRoot.java +++ b/core/src/main/java/org/springframework/security/access/expression/SecurityExpressionRoot.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,6 +28,7 @@ import org.springframework.security.core.Authentication; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.AuthorityUtils; import org.springframework.util.Assert; +import org.springframework.util.function.SingletonSupplier; /** * Base root object for use in Spring Security expression evaluations. @@ -86,7 +87,11 @@ public abstract class SecurityExpressionRoot implements SecurityExpressionOperat * @since 5.8 */ public SecurityExpressionRoot(Supplier authentication) { - this.authentication = new AuthenticationSupplier(authentication); + this.authentication = SingletonSupplier.of(() -> { + Authentication value = authentication.get(); + Assert.notNull(value, "Authentication object cannot be null"); + return value; + }); } @Override @@ -236,27 +241,4 @@ public abstract class SecurityExpressionRoot implements SecurityExpressionOperat return defaultRolePrefix + role; } - private static final class AuthenticationSupplier implements Supplier { - - private Authentication value; - - private final Supplier delegate; - - private AuthenticationSupplier(Supplier delegate) { - Assert.notNull(delegate, "delegate cannot be null"); - this.delegate = delegate; - } - - @Override - public Authentication get() { - if (this.value == null) { - Authentication authentication = this.delegate.get(); - Assert.notNull(authentication, "Authentication object cannot be null"); - this.value = authentication; - } - return this.value; - } - - } - }