Class AuthInfoDetailsService
java.lang.Object
hu.rts.mike.example.dev.crm.service.AuthInfoDetailsService
- All Implemented Interfaces:
org.springframework.security.core.userdetails.UserDetailsService
@Service("userDetailsService")
public class AuthInfoDetailsService
extends Object
implements org.springframework.security.core.userdetails.UserDetailsService
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final AuthInfoRepositoryprivate final jakarta.servlet.http.HttpServletRequeststatic final Stringstatic final Stringprivate final TokenCache -
Constructor Summary
ConstructorsConstructorDescriptionAuthInfoDetailsService(AuthInfoRepository authInfoRepository, TokenCache tokenCache, jakarta.servlet.http.HttpServletRequest httpServletRequest) -
Method Summary
Modifier and TypeMethodDescriptionorg.springframework.security.core.userdetails.UserDetailsReturns the current user in a UserDetails object.private Set<? extends org.springframework.security.core.GrantedAuthority>getAuthorities(Collection<Role> roles) Returns user privileges as Set of SimpleGrantedAuthorities.private Set<? extends org.springframework.security.core.GrantedAuthority>getAuthorities(org.springframework.security.oauth2.jwt.Jwt jwt) Returns user privileges as Set of SimpleGrantedAuthorities.private Set<org.springframework.security.core.GrantedAuthority>getGrantedAuthorities(Set<String> privileges) Converts privileges to GrantedAuthorities.getPrivileges(Collection<Role> roles) Gets privileges of given roles.booleanhasAllPrivileges(Privilege... privileges) Return true if the user has all the specified privileges/roles.booleanhasAnyPrivileges(Privilege... privileges) Return true if the user has any of the specified privileges/roles.booleanisCurrentUser(UserListDto userListDto) Is the given user the currently logged-in user?org.springframework.security.core.userdetails.UserDetailsloadUserByUsername(String username) Loads the user with given username from the database.
-
Field Details
-
ROLE_PREFIX
- See Also:
-
PRIVILEGES
- See Also:
-
authInfoRepository
-
tokenCache
-
httpServletRequest
private final jakarta.servlet.http.HttpServletRequest httpServletRequest
-
-
Constructor Details
-
AuthInfoDetailsService
public AuthInfoDetailsService(AuthInfoRepository authInfoRepository, TokenCache tokenCache, jakarta.servlet.http.HttpServletRequest httpServletRequest)
-
-
Method Details
-
getPrivileges
Gets privileges of given roles. The privilege names are prefixed with "ROLE_" because the view-level @RolesAllowed annotation automatically extends the given role names with the same prefix and @PrivilegesAllowed annotation does not exist.- Parameters:
roles- The roles to be analysed.- Returns:
- Set of privilege names.
-
getGrantedAuthorities
private Set<org.springframework.security.core.GrantedAuthority> getGrantedAuthorities(Set<String> privileges) Converts privileges to GrantedAuthorities.- Parameters:
privileges- The privileges to be converted.- Returns:
- Set of SimpleGrantedAuthorities.
-
getAuthorities
private Set<? extends org.springframework.security.core.GrantedAuthority> getAuthorities(Collection<Role> roles) Returns user privileges as Set of SimpleGrantedAuthorities. Used in case of session-based authentication.- Parameters:
roles- The roles to be converted.- Returns:
- Set of privileges as SimpleGrantedAuthority instances.
-
getAuthorities
private Set<? extends org.springframework.security.core.GrantedAuthority> getAuthorities(org.springframework.security.oauth2.jwt.Jwt jwt) Returns user privileges as Set of SimpleGrantedAuthorities. Used in case of JWT-based authentication.- Parameters:
jwt- The JWT containing username, privileges and further information.- Returns:
- Set of privileges as SimpleGrantedAuthority instances.
-
loadUserByUsername
@Transactional(readOnly=true) public org.springframework.security.core.userdetails.UserDetails loadUserByUsername(String username) throws org.springframework.security.core.userdetails.UsernameNotFoundException Loads the user with given username from the database.- Specified by:
loadUserByUsernamein interfaceorg.springframework.security.core.userdetails.UserDetailsService- Parameters:
username- The user's name.- Returns:
- User data represented with a UserDetails object.
- Throws:
org.springframework.security.core.userdetails.UsernameNotFoundException- If user with the given name is not found.
-
getAuthenticatedUser
public org.springframework.security.core.userdetails.UserDetails getAuthenticatedUser()Returns the current user in a UserDetails object.- Returns:
- The current user's details or null if no user is logged in.
- Throws:
UserBlockedException- If the current user is blocked and blacklisted by TokenCache.
-
isCurrentUser
Is the given user the currently logged-in user?- Parameters:
userListDto- One of the users of the system.- Returns:
- True if the passed user is the current user.
-
hasAllPrivileges
Return true if the user has all the specified privileges/roles.- Parameters:
privileges- Array of required privileges.- Returns:
- True if the current user has all required privileges.
-
hasAnyPrivileges
Return true if the user has any of the specified privileges/roles.- Parameters:
privileges- Array of appropriate privileges.- Returns:
- True if the current user has any of the appropriate privileges.
-