Skip to content

Commit e535e61

Browse files
committed
Move toBuilder to BuildableAuthentication
Closes gh-18052
1 parent fbf7bb3 commit e535e61

File tree

24 files changed

+279
-333
lines changed

24 files changed

+279
-333
lines changed

cas/src/main/java/org/springframework/security/cas/authentication/CasAuthenticationToken.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.jspecify.annotations.Nullable;
2424

2525
import org.springframework.security.authentication.AbstractAuthenticationToken;
26+
import org.springframework.security.core.BuildableAuthentication;
2627
import org.springframework.security.core.GrantedAuthority;
2728
import org.springframework.security.core.userdetails.UserDetails;
2829
import org.springframework.util.Assert;
@@ -34,7 +35,8 @@
3435
* @author Ben Alex
3536
* @author Scott Battaglia
3637
*/
37-
public class CasAuthenticationToken extends AbstractAuthenticationToken implements Serializable {
38+
public class CasAuthenticationToken extends AbstractAuthenticationToken
39+
implements BuildableAuthentication, Serializable {
3840

3941
private static final long serialVersionUID = 620L;
4042

cas/src/main/java/org/springframework/security/cas/authentication/CasServiceTicketAuthenticationToken.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.jspecify.annotations.Nullable;
2323

2424
import org.springframework.security.authentication.AbstractAuthenticationToken;
25+
import org.springframework.security.core.BuildableAuthentication;
2526
import org.springframework.security.core.GrantedAuthority;
2627
import org.springframework.util.Assert;
2728

@@ -32,7 +33,8 @@
3233
* @author Hal Deadman
3334
* @since 6.1
3435
*/
35-
public class CasServiceTicketAuthenticationToken extends AbstractAuthenticationToken {
36+
public class CasServiceTicketAuthenticationToken extends AbstractAuthenticationToken
37+
implements BuildableAuthentication {
3638

3739
static final String CAS_STATELESS_IDENTIFIER = "_cas_stateless_";
3840

core/src/main/java/org/springframework/security/authentication/AbstractAuthenticationToken.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import org.springframework.security.core.AuthenticatedPrincipal;
3030
import org.springframework.security.core.Authentication;
31+
import org.springframework.security.core.BuildableAuthentication;
3132
import org.springframework.security.core.CredentialsContainer;
3233
import org.springframework.security.core.GrantedAuthority;
3334
import org.springframework.security.core.authority.AuthorityUtils;
@@ -198,15 +199,15 @@ public String toString() {
198199
}
199200

200201
/**
201-
* A common abstract implementation of {@link Authentication.Builder}. It implements
202-
* the builder methods that correspond to the {@link Authentication} methods that
203-
* {@link AbstractAuthenticationToken} implements
202+
* A common abstract implementation of {@link BuildableAuthentication.Builder}. It
203+
* implements the builder methods that correspond to the {@link Authentication}
204+
* methods that {@link AbstractAuthenticationToken} implements
204205
*
205206
* @param <B>
206207
* @since 7.0
207208
*/
208209
protected abstract static class AbstractAuthenticationBuilder<B extends AbstractAuthenticationBuilder<B>>
209-
implements Authentication.Builder<B> {
210+
implements BuildableAuthentication.Builder<B> {
210211

211212
private boolean authenticated;
212213

core/src/main/java/org/springframework/security/authentication/RememberMeAuthenticationToken.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.jspecify.annotations.Nullable;
2222

23+
import org.springframework.security.core.BuildableAuthentication;
2324
import org.springframework.security.core.GrantedAuthority;
2425
import org.springframework.util.Assert;
2526

@@ -32,7 +33,7 @@
3233
* @author Ben Alex
3334
* @author Luke Taylor
3435
*/
35-
public class RememberMeAuthenticationToken extends AbstractAuthenticationToken {
36+
public class RememberMeAuthenticationToken extends AbstractAuthenticationToken implements BuildableAuthentication {
3637

3738
private static final long serialVersionUID = 620L;
3839

core/src/main/java/org/springframework/security/authentication/TestingAuthenticationToken.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.jspecify.annotations.Nullable;
2424

25+
import org.springframework.security.core.BuildableAuthentication;
2526
import org.springframework.security.core.GrantedAuthority;
2627
import org.springframework.security.core.authority.AuthorityUtils;
2728
import org.springframework.util.Assert;
@@ -34,7 +35,7 @@
3435
*
3536
* @author Ben Alex
3637
*/
37-
public class TestingAuthenticationToken extends AbstractAuthenticationToken {
38+
public class TestingAuthenticationToken extends AbstractAuthenticationToken implements BuildableAuthentication {
3839

3940
private static final long serialVersionUID = 1L;
4041

core/src/main/java/org/springframework/security/authentication/UsernamePasswordAuthenticationToken.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.jspecify.annotations.Nullable;
2222

23+
import org.springframework.security.core.BuildableAuthentication;
2324
import org.springframework.security.core.GrantedAuthority;
2425
import org.springframework.util.Assert;
2526

@@ -35,7 +36,8 @@
3536
* @author Ben Alex
3637
* @author Norbert Nowak
3738
*/
38-
public class UsernamePasswordAuthenticationToken extends AbstractAuthenticationToken {
39+
public class UsernamePasswordAuthenticationToken extends AbstractAuthenticationToken
40+
implements BuildableAuthentication {
3941

4042
private static final long serialVersionUID = 620L;
4143

core/src/main/java/org/springframework/security/authentication/ott/OneTimeTokenAuthentication.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.jspecify.annotations.Nullable;
2323

2424
import org.springframework.security.authentication.AbstractAuthenticationToken;
25+
import org.springframework.security.core.BuildableAuthentication;
2526
import org.springframework.security.core.GrantedAuthority;
2627
import org.springframework.util.Assert;
2728

@@ -31,7 +32,7 @@
3132
* @author Josh Cummings
3233
* @since 7.0
3334
*/
34-
public class OneTimeTokenAuthentication extends AbstractAuthenticationToken {
35+
public class OneTimeTokenAuthentication extends AbstractAuthenticationToken implements BuildableAuthentication {
3536

3637
@Serial
3738
private static final long serialVersionUID = 1195893764725073959L;

core/src/main/java/org/springframework/security/core/Authentication.java

Lines changed: 0 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.io.Serializable;
2020
import java.security.Principal;
2121
import java.util.Collection;
22-
import java.util.function.Consumer;
2322

2423
import org.jspecify.annotations.Nullable;
2524

@@ -137,110 +136,4 @@ public interface Authentication extends Principal, Serializable {
137136
*/
138137
void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException;
139138

140-
/**
141-
* Return an {@link Builder} based on this instance. By default, returns a builder
142-
* that builds a {@link SimpleAuthentication}.
143-
* <p>
144-
* Although a {@code default} method, all {@link Authentication} implementations
145-
* should implement this. The reason is to ensure that the {@link Authentication} type
146-
* is preserved when {@link Builder#build} is invoked. This is especially important in
147-
* the event that your authentication implementation contains custom fields.
148-
* </p>
149-
* <p>
150-
* This isn't strictly necessary since it is recommended that applications code to the
151-
* {@link Authentication} interface and that custom information is often contained in
152-
* the {@link Authentication#getPrincipal} value.
153-
* </p>
154-
* @return an {@link Builder} for building a new {@link Authentication} based on this
155-
* instance
156-
* @since 7.0
157-
*/
158-
default Builder<?> toBuilder() {
159-
return new SimpleAuthentication.Builder(this);
160-
}
161-
162-
/**
163-
* A builder based on a given {@link Authentication} instance
164-
*
165-
* @author Josh Cummings
166-
* @since 7.0
167-
*/
168-
interface Builder<B extends Builder<B>> {
169-
170-
/**
171-
* Mutate the authorities with this {@link Consumer}.
172-
* <p>
173-
* Note that since a non-empty set of authorities implies an
174-
* {@link Authentication} is authenticated, this method also marks the
175-
* authentication as {@link #authenticated} by default.
176-
* </p>
177-
* @param authorities a consumer that receives the full set of authorities
178-
* @return the {@link Builder} for additional configuration
179-
* @see Authentication#getAuthorities
180-
*/
181-
B authorities(Consumer<Collection<GrantedAuthority>> authorities);
182-
183-
/**
184-
* Use this credential.
185-
* <p>
186-
* Note that since some credentials are insecure to store, this method is
187-
* implemented as unsupported by default. Only implement or use this method if you
188-
* support secure storage of the credential or if your implementation also
189-
* implements {@link CredentialsContainer} and the credentials are thereby erased.
190-
* </p>
191-
* @param credentials the credentials to use
192-
* @return the {@link Builder} for additional configuration
193-
* @see Authentication#getCredentials
194-
*/
195-
default B credentials(@Nullable Object credentials) {
196-
throw new UnsupportedOperationException(
197-
String.format("%s does not store credentials", this.getClass().getSimpleName()));
198-
}
199-
200-
/**
201-
* Use this details object.
202-
* <p>
203-
* Implementations may choose to use these {@code details} in combination with any
204-
* principal from the pre-existing {@link Authentication} instance.
205-
* </p>
206-
* @param details the details to use
207-
* @return the {@link Builder} for additional configuration
208-
* @see Authentication#getDetails
209-
*/
210-
B details(@Nullable Object details);
211-
212-
/**
213-
* Use this principal.
214-
* <p>
215-
* Note that in many cases, the principal is strongly-typed. Implementations may
216-
* choose to do a type check and are not necessarily expected to allow any object
217-
* as a principal.
218-
* </p>
219-
* <p>
220-
* Implementations may choose to use this {@code principal} in combination with
221-
* any principal from the pre-existing {@link Authentication} instance.
222-
* </p>
223-
* @param principal the principal to use
224-
* @return the {@link Builder} for additional configuration
225-
* @see Authentication#getPrincipal
226-
*/
227-
B principal(@Nullable Object principal);
228-
229-
/**
230-
* Mark this authentication as authenticated or not
231-
* @param authenticated whether this is an authenticated {@link Authentication}
232-
* instance
233-
* @return the {@link Builder} for additional configuration
234-
* @see Authentication#isAuthenticated
235-
*/
236-
B authenticated(boolean authenticated);
237-
238-
/**
239-
* Build an {@link Authentication} instance
240-
* @return the {@link Authentication} instance
241-
*/
242-
Authentication build();
243-
244-
}
245-
246139
}

0 commit comments

Comments
 (0)