Skip to content

Commit 3e5f31b

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

File tree

5 files changed

+187
-18
lines changed

5 files changed

+187
-18
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,38 @@
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
33+
* @throws UsernameNotFoundException if the user could not be found or the user has no
34+
* GrantedAuthority
35+
*/
36+
UserAuthorities findAuthoritiesByUsername(String username) throws UsernameNotFoundException;
37+
38+
}

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

+1-18
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@
1616

1717
package org.springframework.security.core.userdetails;
1818

19-
import java.io.Serializable;
20-
import java.util.Collection;
21-
2219
import org.springframework.security.core.Authentication;
23-
import org.springframework.security.core.GrantedAuthority;
2420

2521
/**
2622
* Provides core user information.
@@ -40,27 +36,14 @@
4036
* @see UserDetailsService
4137
* @see UserCache
4238
*/
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();
39+
public interface UserDetails extends UserAuthorities {
5040

5141
/**
5242
* Returns the password used to authenticate the user.
5343
* @return the password
5444
*/
5545
String getPassword();
5646

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-
6447
/**
6548
* Indicates whether the user's account has expired. An expired account cannot be
6649
* authenticated.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 org.springframework.util.Assert;
20+
21+
/**
22+
* An implementation of {@link UserAuthoritiesRepository} that uses a
23+
* {@link UserDetailsService} to load the user authorities.
24+
*
25+
* @author Marcus da Coregio
26+
* @since 6.4
27+
*/
28+
public class UserDetailsServiceAuthoritiesRepository implements UserAuthoritiesRepository {
29+
30+
private final UserDetailsService userDetailsService;
31+
32+
public UserDetailsServiceAuthoritiesRepository(UserDetailsService userDetailsService) {
33+
Assert.notNull(userDetailsService, "userDetailsService cannot be null");
34+
this.userDetailsService = userDetailsService;
35+
}
36+
37+
@Override
38+
public UserAuthorities findAuthoritiesByUsername(String username) {
39+
return this.userDetailsService.loadUserByUsername(username);
40+
}
41+
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.security.core.GrantedAuthority;
23+
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
27+
28+
/**
29+
* Tests for {@link UserDetailsServiceAuthoritiesRepository}
30+
*
31+
* @author Marcus da Coregio
32+
*/
33+
class UserDetailsServiceAuthoritiesRepositoryTests {
34+
35+
UserDetailsService userDetailsService = new InMemoryUserDetailsManager(PasswordEncodedUser.user(),
36+
PasswordEncodedUser.admin());
37+
38+
UserDetailsServiceAuthoritiesRepository userAuthoritiesRepository;
39+
40+
@BeforeEach
41+
void setup() {
42+
this.userAuthoritiesRepository = new UserDetailsServiceAuthoritiesRepository(this.userDetailsService);
43+
}
44+
45+
@Test
46+
void findUserAuthoritiesWhenUserExistsThenReturn() {
47+
UserAuthorities admin = this.userAuthoritiesRepository.findAuthoritiesByUsername("admin");
48+
assertThat(admin.getAuthorities()).extracting(GrantedAuthority::getAuthority)
49+
.containsExactly("ROLE_ADMIN", "ROLE_USER");
50+
}
51+
52+
@Test
53+
void findUserAuthoritiesWhenUserDoesNotExistsThenUsernameNotFoundException() {
54+
assertThatExceptionOfType(UsernameNotFoundException.class)
55+
.isThrownBy(() -> this.userAuthoritiesRepository.findAuthoritiesByUsername("unknown"));
56+
}
57+
58+
}

0 commit comments

Comments
 (0)