mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-05-19 08:22:00 +00:00

# Description of Changes Please provide a summary of the changes, including: - What was changed - Why the change was made - Any challenges encountered Closes #(issue_number) --- ## Checklist ### General - [ ] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [ ] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md) (if applicable) - [ ] I have performed a self-review of my own code - [ ] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [ ] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md#6-testing) for more details. --------- Co-authored-by: a <a>
63 lines
2.4 KiB
Java
63 lines
2.4 KiB
Java
package stirling.software.SPDF.config.security;
|
|
|
|
import java.util.Collection;
|
|
import java.util.Set;
|
|
|
|
import org.springframework.security.authentication.LockedException;
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
|
import org.springframework.security.core.userdetails.UserDetails;
|
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import stirling.software.SPDF.model.Authority;
|
|
import stirling.software.SPDF.model.User;
|
|
import stirling.software.SPDF.repository.UserRepository;
|
|
|
|
@Service
|
|
public class CustomUserDetailsService implements UserDetailsService {
|
|
|
|
private final UserRepository userRepository;
|
|
|
|
private final LoginAttemptService loginAttemptService;
|
|
|
|
public CustomUserDetailsService(
|
|
UserRepository userRepository, LoginAttemptService loginAttemptService) {
|
|
this.userRepository = userRepository;
|
|
this.loginAttemptService = loginAttemptService;
|
|
}
|
|
|
|
@Override
|
|
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
|
User user =
|
|
userRepository
|
|
.findByUsername(username)
|
|
.orElseThrow(
|
|
() ->
|
|
new UsernameNotFoundException(
|
|
"No user found with username: " + username));
|
|
if (loginAttemptService.isBlocked(username)) {
|
|
throw new LockedException(
|
|
"Your account has been locked due to too many failed login attempts.");
|
|
}
|
|
if (!user.hasPassword()) {
|
|
throw new IllegalArgumentException("Password must not be null");
|
|
}
|
|
return new org.springframework.security.core.userdetails.User(
|
|
user.getUsername(),
|
|
user.getPassword(),
|
|
user.isEnabled(),
|
|
true,
|
|
true,
|
|
true,
|
|
getAuthorities(user.getAuthorities()));
|
|
}
|
|
|
|
private Collection<? extends GrantedAuthority> getAuthorities(Set<Authority> authorities) {
|
|
return authorities.stream()
|
|
.map(authority -> new SimpleGrantedAuthority(authority.getAuthority()))
|
|
.toList();
|
|
}
|
|
}
|