Package com.alpaca.service.impl
Class AuthServiceImpl
java.lang.Object
org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService
com.alpaca.service.impl.AuthServiceImpl
- All Implemented Interfaces:
IAuthService
,org.springframework.security.core.userdetails.UserDetailsService
,org.springframework.security.oauth2.client.userinfo.OAuth2UserService<org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest,
org.springframework.security.oauth2.core.user.OAuth2User>
@Service
public class AuthServiceImpl
extends org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService
implements IAuthService
Implementation of
IAuthService
, handling authentication, user registration, and OAuth2
login flows within a Spring Security context.
Extends DefaultOAuth2UserService
to support custom OAuth2 user processing, JWT token
generation, and security context management.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final JJwtManager
private final PasswordManager
private final IProfileService
private final IRoleService
private final IUserService
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionorg.springframework.security.core.Authentication
authenticate
(String username, String password) Performs authentication using Spring's authentication token model.checkExistingUser
(User user, boolean emailVerified) Updates an existing user's OAuth2 connection status and email verification flag.org.springframework.security.oauth2.core.user.OAuth2User
loadUser
(org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest userRequest) Handles OAuth2 login by processing the provider's user data and delegating logic.org.springframework.security.core.userdetails.UserDetails
loadUserByUsername
(String username) Loads a user by username (email), required by Spring Security.Authenticates a user using email and password and returns a JWT token wrapped in DTO.org.springframework.security.oauth2.core.user.OAuth2User
processOAuth2User
(org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest request, org.springframework.security.oauth2.core.user.OAuth2User user) Processes OAuth2 user data: registers or connects existing user based on attributes.Registers a new user, assigns default role, and logs them in returning a JWT token.registerOrLoginOAuth2
(String email, String firstName, String lastName, String imageURL, boolean emailVerified, Map<String, Object> attributes) Registers or logs in a user based on OAuth2 information.registerProfile
(User user, String firstName, String lastName, String imageURL) Creates or updates a user's profile.setSecurityContextBefore
(org.springframework.security.core.Authentication authentication) Sets the Spring Security context with the provided authentication object.org.springframework.security.core.userdetails.UserDetails
validateUserDetails
(String rawPassword, org.springframework.security.core.userdetails.UserDetails userDetails) Validates raw password against stored user details and enforces account status checks.Methods inherited from class org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService
setAttributesConverter, setRequestEntityConverter, setRestOperations
-
Field Details
-
roleService
-
userService
-
profileService
-
manager
-
passwordManager
-
-
Constructor Details
-
AuthServiceImpl
public AuthServiceImpl()
-
-
Method Details
-
setSecurityContextBefore
public Object setSecurityContextBefore(org.springframework.security.core.Authentication authentication) Sets the Spring Security context with the provided authentication object.- Parameters:
authentication
- the authentication object; must not benull
- Returns:
- the authenticated user's principal
- Throws:
UnauthorizedException
- if the authentication object isnull
-
login
Authenticates a user using email and password and returns a JWT token wrapped in DTO.- Specified by:
login
in interfaceIAuthService
- Parameters:
email
- user's emailpassword
- raw password- Returns:
AuthResponseDTO
containing the JWT token- Throws:
NotFoundException
- if the email is not registered
-
register
Registers a new user, assigns default role, and logs them in returning a JWT token.- Specified by:
register
in interfaceIAuthService
- Parameters:
email
- new user's emailpassword
- raw password- Returns:
AuthResponseDTO
for the newly registered user- Throws:
BadRequestException
- if the email is already registered
-
loadUserByUsername
public org.springframework.security.core.userdetails.UserDetails loadUserByUsername(String username) Loads a user by username (email), required by Spring Security.- Specified by:
loadUserByUsername
in interfaceorg.springframework.security.core.userdetails.UserDetailsService
- Parameters:
username
- user's email- Returns:
UserDetails
for authentication- Throws:
org.springframework.security.core.userdetails.UsernameNotFoundException
- if user is not found
-
validateUserDetails
public org.springframework.security.core.userdetails.UserDetails validateUserDetails(String rawPassword, org.springframework.security.core.userdetails.UserDetails userDetails) Validates raw password against stored user details and enforces account status checks.- Parameters:
rawPassword
- the entered passworduserDetails
- stored user details- Returns:
- authenticated
UserDetails
- Throws:
BadRequestException
- if validation failsUnauthorizedException
- if account is disabled or locked
-
authenticate
public org.springframework.security.core.Authentication authenticate(String username, String password) Performs authentication using Spring's authentication token model.- Parameters:
username
- user's emailpassword
- raw password- Returns:
- an
Authentication
object
-
loadUser
public org.springframework.security.oauth2.core.user.OAuth2User loadUser(org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest userRequest) Handles OAuth2 login by processing the provider's user data and delegating logic.- Specified by:
loadUser
in interfaceorg.springframework.security.oauth2.client.userinfo.OAuth2UserService<org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest,
org.springframework.security.oauth2.core.user.OAuth2User> - Overrides:
loadUser
in classorg.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService
- Parameters:
userRequest
- the OAuth2 user request- Returns:
- an authenticated
OAuth2User
-
processOAuth2User
public org.springframework.security.oauth2.core.user.OAuth2User processOAuth2User(org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest request, org.springframework.security.oauth2.core.user.OAuth2User user) Processes OAuth2 user data: registers or connects existing user based on attributes.- Parameters:
request
- the OAuth2 user requestuser
- the authenticated OAuth2 user- Returns:
- an
OAuth2User
- Throws:
OAuth2AuthenticationProcessingException
- if required attributes are missing
-
registerOrLoginOAuth2
public UserPrincipal registerOrLoginOAuth2(String email, String firstName, String lastName, String imageURL, boolean emailVerified, Map<String, Object> attributes) Registers or logs in a user based on OAuth2 information. Sets up profile as needed.- Parameters:
email
- user's emailfirstName
- user's first namelastName
- user's last nameimageURL
- profile image URLemailVerified
- email verification statusattributes
- other OAuth2 attributes- Returns:
- a new or existing
UserPrincipal
- Throws:
BadRequestException
- if required fields are missing
-
registerProfile
Creates or updates a user's profile.- Parameters:
user
- the user entityfirstName
- user’s first namelastName
- user’s last nameimageURL
- user's avatar URL- Returns:
- updated
User
with profile - Throws:
BadRequestException
- if the user or mandatory fields are invalid
-
checkExistingUser
Updates an existing user's OAuth2 connection status and email verification flag.- Parameters:
user
- the existing useremailVerified
- OAuth2-provided email verification status- Returns:
- updated
User
- Throws:
UnauthorizedException
- if the user account is disabled or locked
-