forked from spring-projects/spring-security
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
187 additions
and
18 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
core/src/main/java/org/springframework/security/core/userdetails/UserAuthorities.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2002-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.security.core.userdetails; | ||
|
||
import java.io.Serializable; | ||
import java.util.Collection; | ||
|
||
import org.springframework.security.core.GrantedAuthority; | ||
|
||
/** | ||
* Represents user authorities. This interface is mostly intended for scenarios where a | ||
* password is not need, like X509, CAS, Passkeys, One Time Tokens and others. | ||
* | ||
* @author Marcus da Coregio | ||
* @since 6.4 | ||
* @see UserAuthoritiesRepository | ||
* @see UserDetails | ||
*/ | ||
public interface UserAuthorities extends Serializable { | ||
|
||
/** | ||
* Returns the authorities granted to the user. Cannot return <code>null</code>. | ||
* @return the authorities, sorted by natural key (never <code>null</code>) | ||
*/ | ||
Collection<? extends GrantedAuthority> getAuthorities(); | ||
|
||
/** | ||
* Returns the username used to authenticate the user. Cannot return | ||
* <code>null</code>. | ||
* @return the username (never <code>null</code>) | ||
*/ | ||
String getUsername(); | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
...rc/main/java/org/springframework/security/core/userdetails/UserAuthoritiesRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2002-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.security.core.userdetails; | ||
|
||
/** | ||
* Repository interface for accessing user authorities. | ||
* | ||
* @author Marcus da Coregio | ||
* @since 6.4 | ||
* @see UserAuthorities | ||
*/ | ||
public interface UserAuthoritiesRepository { | ||
|
||
/** | ||
* Finds the authorities associated with the given username. | ||
* @param username the username for which to find authorities | ||
* @return the {@link UserAuthorities} object containing authorities associated with | ||
* the specified username | ||
* @throws UsernameNotFoundException if the user could not be found or the user has no | ||
* GrantedAuthority | ||
*/ | ||
UserAuthorities findAuthoritiesByUsername(String username) throws UsernameNotFoundException; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...rg/springframework/security/core/userdetails/UserDetailsServiceAuthoritiesRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2002-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.security.core.userdetails; | ||
|
||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* An implementation of {@link UserAuthoritiesRepository} that uses a | ||
* {@link UserDetailsService} to load the user authorities. | ||
* | ||
* @author Marcus da Coregio | ||
* @since 6.4 | ||
*/ | ||
public class UserDetailsServiceAuthoritiesRepository implements UserAuthoritiesRepository { | ||
|
||
private final UserDetailsService userDetailsService; | ||
|
||
public UserDetailsServiceAuthoritiesRepository(UserDetailsService userDetailsService) { | ||
Assert.notNull(userDetailsService, "userDetailsService cannot be null"); | ||
this.userDetailsService = userDetailsService; | ||
} | ||
|
||
@Override | ||
public UserAuthorities findAuthoritiesByUsername(String username) { | ||
return this.userDetailsService.loadUserByUsername(username); | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
...ringframework/security/core/userdetails/UserDetailsServiceAuthoritiesRepositoryTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2002-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.security.core.userdetails; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.provisioning.InMemoryUserDetailsManager; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
|
||
/** | ||
* Tests for {@link UserDetailsServiceAuthoritiesRepository} | ||
* | ||
* @author Marcus da Coregio | ||
*/ | ||
class UserDetailsServiceAuthoritiesRepositoryTests { | ||
|
||
UserDetailsService userDetailsService = new InMemoryUserDetailsManager(PasswordEncodedUser.user(), | ||
PasswordEncodedUser.admin()); | ||
|
||
UserDetailsServiceAuthoritiesRepository userAuthoritiesRepository; | ||
|
||
@BeforeEach | ||
void setup() { | ||
this.userAuthoritiesRepository = new UserDetailsServiceAuthoritiesRepository(this.userDetailsService); | ||
} | ||
|
||
@Test | ||
void findUserAuthoritiesWhenUserExistsThenReturn() { | ||
UserAuthorities admin = this.userAuthoritiesRepository.findAuthoritiesByUsername("admin"); | ||
assertThat(admin.getAuthorities()).extracting(GrantedAuthority::getAuthority) | ||
.containsExactly("ROLE_ADMIN", "ROLE_USER"); | ||
} | ||
|
||
@Test | ||
void findUserAuthoritiesWhenUserDoesNotExistsThenUsernameNotFoundException() { | ||
assertThatExceptionOfType(UsernameNotFoundException.class) | ||
.isThrownBy(() -> this.userAuthoritiesRepository.findAuthoritiesByUsername("unknown")); | ||
} | ||
|
||
} |