@@ -46,7 +46,7 @@ public class CachedIdentityAuthorizeCallback implements ExtendedCallback {
4646
4747 private final Function <SecurityDomain , IdentityCache > identityCache ;
4848 private final boolean localCache ;
49- private Principal principal ;
49+ private final Principal principal ;
5050 private boolean authorized ;
5151 private SecurityDomain securityDomain ;
5252
@@ -85,11 +85,14 @@ public CachedIdentityAuthorizeCallback(Function<SecurityDomain, IdentityCache> i
8585 checkNotNullParam ("identityCache" , identityCache );
8686 this .identityCache = identityCache ;
8787 this .localCache = localCache ;
88+ this .principal = null ;
8889 }
8990
9091 /**
9192 * Creates a new instance to authenticate, authorize and cache the identity associated with the given <code>name</code>.
9293 *
94+ * <p>By supplying a name authorizing the supplied name will be prioritised over restoring an identify from the cache</p>
95+ *
9396 * @param name the name associated with the identity
9497 * @param identityCache the identity cache
9598 */
@@ -100,6 +103,8 @@ public CachedIdentityAuthorizeCallback(String name, IdentityCache identityCache)
100103 /**
101104 * Creates a new instance to authenticate, authorize and cache the identity associated with the given <code>principal</code>.
102105 *
106+ * <p>By supplying a {@code Principal} authorizing the supplied {@code Principal} will be prioritised over restoring an identify from the cache</p>
107+ *
103108 * @param principal the principal associated with the identity
104109 * @param identityCache the identity cache
105110 * @param localCache if true, indicates that authorization should be based on the given {@code identityCache} only. In case the mechanism
@@ -113,6 +118,8 @@ public CachedIdentityAuthorizeCallback(Principal principal, IdentityCache identi
113118 /**
114119 * Creates a new instance to authenticate, authorize and cache the identity associated with the given <code>principal</code>.
115120 *
121+ * <p>By supplying a {@code Principal} authorizing the supplied {@code Principal} will be prioritised over restoring an identify from the cache</p>
122+ *
116123 * @param principal the principal associated with the identity
117124 * @param identityCache the identity cache
118125 */
@@ -125,6 +132,8 @@ public CachedIdentityAuthorizeCallback(Principal principal, IdentityCache identi
125132 *
126133 * <p>This constructor can be used to perform caching operations (e.g.: put, get and remove) in the context of a {@link SecurityDomain}.
127134 *
135+ * <p>By supplying a {@code Principal} authorizing the supplied {@code Principal} will be prioritised over restoring an identify from the cache</p>
136+ *
128137 * @param principal the principal associated with the identity
129138 * @param identityCache a function that creates an {@link IdentityCache} given a {@link SecurityDomain}
130139 * @param localCache if true, indicates that authorization should be based on the given {@code identityCache} only. In case the mechanism
@@ -156,7 +165,11 @@ public boolean isAuthorized() {
156165 public void setAuthorized (SecurityIdentity securityIdentity ) {
157166 authorized = securityIdentity != null ;
158167 if (authorized ) {
159- createDomainCache ().put (securityIdentity );
168+ IdentityCache cache = createDomainCache ();
169+ if (this .principal != null ) {
170+ cache .remove ();
171+ }
172+ cache .put (securityIdentity );
160173 } else {
161174 createDomainCache ().remove ();
162175 }
@@ -178,7 +191,7 @@ public Principal getPrincipal() {
178191 /**
179192 * Returns the authorization {@link Principal}.
180193 *
181- * @return the principal (not {@code null})
194+ * @return the principal
182195 */
183196 public Principal getAuthorizationPrincipal () {
184197 return this .principal ;
0 commit comments