Skip to content

Commit a5edb20

Browse files
Introduce UserAuthorities
Closes gh-15406
1 parent ffd4a0f commit a5edb20

File tree

4 files changed

+91
-15
lines changed

4 files changed

+91
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2002-2024 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.userdetails;
18+
19+
import java.io.Serializable;
20+
import java.util.Collection;
21+
22+
import org.springframework.security.core.GrantedAuthority;
23+
24+
/**
25+
* Represents user authorities. This interface is mostly intended for scenarios where a
26+
* password is not need, like X509, CAS, Passkeys, One Time Tokens and others.
27+
*
28+
* @author Marcus da Coregio
29+
* @since 6.4
30+
* @see UserAuthoritiesRepository
31+
* @see UserDetails
32+
*/
33+
public interface UserAuthorities extends Serializable {
34+
35+
/**
36+
* Returns the authorities granted to the user. Cannot return <code>null</code>.
37+
* @return the authorities, sorted by natural key (never <code>null</code>)
38+
*/
39+
Collection<? extends GrantedAuthority> getAuthorities();
40+
41+
/**
42+
* Returns the username used to authenticate the user. Cannot return
43+
* <code>null</code>.
44+
* @return the username (never <code>null</code>)
45+
*/
46+
String getUsername();
47+
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2002-2024 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.userdetails;
18+
19+
/**
20+
* Repository interface for accessing user authorities.
21+
*
22+
* @author Marcus da Coregio
23+
* @since 6.4
24+
* @see UserAuthorities
25+
*/
26+
public interface UserAuthoritiesRepository {
27+
28+
/**
29+
* Finds the authorities associated with the given username.
30+
* @param username the username for which to find authorities
31+
* @return the {@link UserAuthorities} object containing authorities associated with
32+
* the specified username, or {@code null} if no authorities are found
33+
*/
34+
UserAuthorities findAuthoritiesByUsername(String username);
35+
36+
}

Diff for: core/src/main/java/org/springframework/security/core/userdetails/UserDetails.java

+1-14
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,14 @@
4040
* @see UserDetailsService
4141
* @see UserCache
4242
*/
43-
public interface UserDetails extends Serializable {
44-
45-
/**
46-
* Returns the authorities granted to the user. Cannot return <code>null</code>.
47-
* @return the authorities, sorted by natural key (never <code>null</code>)
48-
*/
49-
Collection<? extends GrantedAuthority> getAuthorities();
43+
public interface UserDetails extends Serializable, UserAuthorities {
5044

5145
/**
5246
* Returns the password used to authenticate the user.
5347
* @return the password
5448
*/
5549
String getPassword();
5650

57-
/**
58-
* Returns the username used to authenticate the user. Cannot return
59-
* <code>null</code>.
60-
* @return the username (never <code>null</code>)
61-
*/
62-
String getUsername();
63-
6451
/**
6552
* Indicates whether the user's account has expired. An expired account cannot be
6653
* authenticated.

Diff for: core/src/main/java/org/springframework/security/core/userdetails/UserDetailsService.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* @see org.springframework.security.authentication.dao.DaoAuthenticationProvider
3232
* @see UserDetails
3333
*/
34-
public interface UserDetailsService {
34+
public interface UserDetailsService extends UserAuthoritiesRepository {
3535

3636
/**
3737
* Locates the user based on the username. In the actual implementation, the search
@@ -46,4 +46,9 @@ public interface UserDetailsService {
4646
*/
4747
UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;
4848

49+
@Override
50+
default UserAuthorities findAuthoritiesByUsername(String username) {
51+
return loadUserByUsername(username);
52+
}
53+
4954
}

0 commit comments

Comments
 (0)