Skip to content

Commit f814dc0

Browse files
committed
Add private general-purpose Authentication impl
1 parent e8d7861 commit f814dc0

File tree

3 files changed

+144
-71
lines changed

3 files changed

+144
-71
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public interface Authentication extends Principal, Serializable {
6565
* instance.
6666
* </p>
6767
* @return the authorities granted to the principal, or an empty collection if the
68-
* token has not been authenticated. Never null.Saml2AssertAu
68+
* token has not been authenticated. Never null.
6969
*/
7070
Collection<? extends GrantedAuthority> getAuthorities();
7171

@@ -144,7 +144,7 @@ public interface Authentication extends Principal, Serializable {
144144
* @since 7.0
145145
*/
146146
default Builder<?, ?, ?> toBuilder() {
147-
return new NoopAuthenticationBuilder(this);
147+
return new SimpleAuthentication.Builder(this);
148148
}
149149

150150
/**

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

Lines changed: 0 additions & 69 deletions
This file was deleted.
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* Copyright 2004-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.core;
18+
19+
import java.io.Serial;
20+
import java.util.Collection;
21+
import java.util.LinkedHashSet;
22+
import java.util.function.Consumer;
23+
24+
import org.jspecify.annotations.Nullable;
25+
26+
final class SimpleAuthentication implements Authentication {
27+
28+
@Serial
29+
private static final long serialVersionUID = 3194696462184782814L;
30+
31+
private final @Nullable Object principal;
32+
33+
private final @Nullable Object credentials;
34+
35+
private final Collection<GrantedAuthority> authorities;
36+
37+
private final @Nullable Object details;
38+
39+
private final boolean authenticated;
40+
41+
private SimpleAuthentication(Builder builder) {
42+
this.principal = builder.principal;
43+
this.credentials = builder.credentials;
44+
this.authorities = builder.authorities;
45+
this.details = builder.details;
46+
this.authenticated = builder.authenticated;
47+
}
48+
49+
@Override
50+
public Collection<? extends GrantedAuthority> getAuthorities() {
51+
return this.authorities;
52+
}
53+
54+
@Override
55+
public @Nullable Object getCredentials() {
56+
return this.credentials;
57+
}
58+
59+
@Override
60+
public @Nullable Object getDetails() {
61+
return this.details;
62+
}
63+
64+
@Override
65+
public @Nullable Object getPrincipal() {
66+
return this.principal;
67+
}
68+
69+
@Override
70+
public boolean isAuthenticated() {
71+
return this.authenticated;
72+
}
73+
74+
@Override
75+
public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
76+
throw new IllegalArgumentException(
77+
"Instead of calling this setter, please call toBuilder to create a new instance");
78+
}
79+
80+
@Override
81+
public String getName() {
82+
return (this.principal == null) ? "" : this.principal.toString();
83+
}
84+
85+
static final class Builder implements Authentication.Builder<Object, Object, Builder> {
86+
87+
private final Collection<GrantedAuthority> authorities = new LinkedHashSet<>();
88+
89+
private @Nullable Object principal;
90+
91+
private @Nullable Object credentials;
92+
93+
private @Nullable Object details;
94+
95+
private boolean authenticated;
96+
97+
Builder(Authentication authentication) {
98+
this.authorities.addAll(authentication.getAuthorities());
99+
this.principal = authentication.getPrincipal();
100+
this.credentials = authentication.getCredentials();
101+
this.details = authentication.getDetails();
102+
this.authenticated = authentication.isAuthenticated();
103+
}
104+
105+
@Override
106+
public Builder authorities(Consumer<Collection<GrantedAuthority>> authorities) {
107+
authorities.accept(this.authorities);
108+
return this;
109+
}
110+
111+
@Override
112+
public Builder details(@Nullable Object details) {
113+
this.details = details;
114+
return this;
115+
}
116+
117+
@Override
118+
public Builder principal(@Nullable Object principal) {
119+
this.principal = principal;
120+
return this;
121+
}
122+
123+
@Override
124+
public Builder credentials(@Nullable Object credentials) {
125+
this.credentials = credentials;
126+
return this;
127+
}
128+
129+
@Override
130+
public Builder authenticated(boolean authenticated) {
131+
this.authenticated = authenticated;
132+
return this;
133+
}
134+
135+
@Override
136+
public Authentication build() {
137+
return new SimpleAuthentication(this);
138+
}
139+
140+
}
141+
142+
}

0 commit comments

Comments
 (0)