1
0
mirror of synced 2026-08-05 01:36:56 +00:00

Remove Generic Typing From Authentication.Builder

It would be better to introduce parameter types for
principal and credentials into Authentication.Builder
at the same time as doing so for Authentication

Issue gh-17861
This commit is contained in:
Josh Cummings
2025-09-09 09:03:57 -06:00
parent 4744752a1b
commit dd50dc0c40
17 changed files with 47 additions and 38 deletions
@@ -69,7 +69,7 @@ public abstract class AbstractAuthenticationToken implements Authentication, Cre
this.authorities = Collections.unmodifiableList(new ArrayList<>(authorities));
}
protected AbstractAuthenticationToken(AbstractAuthenticationBuilder<?, ?, ?> builder) {
protected AbstractAuthenticationToken(AbstractAuthenticationBuilder<?> builder) {
this(builder.authorities);
this.authenticated = builder.authenticated;
this.details = builder.details;
@@ -197,8 +197,8 @@ public abstract class AbstractAuthenticationToken implements Authentication, Cre
return sb.toString();
}
protected abstract static class AbstractAuthenticationBuilder<P, C, B extends AbstractAuthenticationBuilder<P, C, B>>
implements Authentication.Builder<P, C, B> {
protected abstract static class AbstractAuthenticationBuilder<B extends AbstractAuthenticationBuilder<B>>
implements Authentication.Builder<B> {
protected boolean authenticated;
@@ -126,7 +126,7 @@ public class RememberMeAuthenticationToken extends AbstractAuthenticationToken {
*
* @since 7.0
*/
public static class Builder<B extends Builder<B>> extends AbstractAuthenticationBuilder<Object, Object, B> {
public static class Builder<B extends Builder<B>> extends AbstractAuthenticationBuilder<B> {
private Integer keyHash;
@@ -91,7 +91,7 @@ public class TestingAuthenticationToken extends AbstractAuthenticationToken {
*
* @since 7.0
*/
public static class Builder<B extends Builder<B>> extends AbstractAuthenticationBuilder<Object, Object, B> {
public static class Builder<B extends Builder<B>> extends AbstractAuthenticationBuilder<B> {
private Object principal;
@@ -141,7 +141,7 @@ public class UsernamePasswordAuthenticationToken extends AbstractAuthenticationT
*
* @since 7.0
*/
public static class Builder<B extends Builder<B>> extends AbstractAuthenticationBuilder<Object, Object, B> {
public static class Builder<B extends Builder<B>> extends AbstractAuthenticationBuilder<B> {
protected @Nullable Object principal;
@@ -67,7 +67,7 @@ public class OneTimeTokenAuthentication extends AbstractAuthenticationToken {
/**
* A builder for constructing a {@link OneTimeTokenAuthentication} instance
*/
public static class Builder<B extends Builder<B>> extends AbstractAuthenticationBuilder<Object, Object, B> {
public static class Builder<B extends Builder<B>> extends AbstractAuthenticationBuilder<B> {
private Object principal;
@@ -143,7 +143,7 @@ public interface Authentication extends Principal, Serializable {
* instance
* @since 7.0
*/
default Builder<?, ?, ?> toBuilder() {
default Builder<?> toBuilder() {
return new SimpleAuthentication.Builder(this);
}
@@ -153,18 +153,18 @@ public interface Authentication extends Principal, Serializable {
* @author Josh Cummings
* @since 7.0
*/
interface Builder<P, C, B extends Builder<P, C, B>> {
interface Builder<B extends Builder<B>> {
B authorities(Consumer<Collection<GrantedAuthority>> authorities);
default B credentials(@Nullable C credentials) {
default B credentials(@Nullable Object credentials) {
throw new UnsupportedOperationException(
String.format("%s does not store credentials", this.getClass().getSimpleName()));
}
B details(@Nullable Object details);
B principal(@Nullable P principal);
B principal(@Nullable Object principal);
B authenticated(boolean authenticated);
@@ -21,6 +21,8 @@ import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.function.Consumer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jspecify.annotations.Nullable;
@Transient
@@ -83,7 +85,9 @@ final class SimpleAuthentication implements Authentication {
return (this.principal == null) ? "" : this.principal.toString();
}
static final class Builder implements Authentication.Builder<Object, Object, Builder> {
static final class Builder implements Authentication.Builder<Builder> {
private final Log logger = LogFactory.getLog(getClass());
private final Collection<GrantedAuthority> authorities = new LinkedHashSet<>();
@@ -96,11 +100,15 @@ final class SimpleAuthentication implements Authentication {
private boolean authenticated;
Builder(Authentication authentication) {
this.logger.debug("Creating a builder which will result in exchanging an authentication of type "
+ authentication.getClass() + " for " + SimpleAuthentication.class.getSimpleName() + ";"
+ " consider implementing " + authentication.getClass().getSimpleName() + "#toBuilder");
this.authorities.addAll(authentication.getAuthorities());
this.principal = authentication.getPrincipal();
this.credentials = authentication.getCredentials();
this.details = authentication.getDetails();
this.authenticated = authentication.isAuthenticated();
}
@Override
@@ -40,7 +40,7 @@ class AbstractAuthenticationBuilderTests {
}
private static final class TestAbstractAuthenticationBuilder
extends AbstractAuthenticationBuilder<Object, Object, TestAbstractAuthenticationBuilder> {
extends AbstractAuthenticationBuilder<TestAbstractAuthenticationBuilder> {
private TestAbstractAuthenticationBuilder(TestingAuthenticationToken token) {
super(token);