Skip to content

Commit

Permalink
Merge pull request #95 from nastiausenko/refactor/security-configuration
Browse files Browse the repository at this point in the history
Updated spring security's `CustomUserDetailsService`
  • Loading branch information
IvanShalaev1990 authored Apr 24, 2024
2 parents f416ea8 + 6416702 commit dc29e86
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.linkurlshorter.urlshortener.security;

import com.linkurlshorter.urlshortener.user.model.User;
import com.linkurlshorter.urlshortener.user.UserService;

import com.linkurlshorter.urlshortener.user.UserRepository;
import lombok.RequiredArgsConstructor;
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;

/**
Expand All @@ -16,17 +17,17 @@
*
* @author Egor Sivenko
* @see org.springframework.security.core.userdetails.UserDetailsService
* @see UserService
* @see UserRepository
* @see SecurityUserDetails
*/
@Service
@RequiredArgsConstructor
public class CustomUserDetailsService implements UserDetailsService {

/**
* Service for managing user-related operations.
* Repository for managing user-related operations.
*/
private final UserService userService;
private final UserRepository userRepository;

/**
* Loads user details by their email address.
Expand All @@ -36,7 +37,9 @@ public class CustomUserDetailsService implements UserDetailsService {
*/
@Override
public UserDetails loadUserByUsername(String email) {
User user = userService.findByEmail(email);
return new SecurityUserDetails(user);
return userRepository
.findByEmail(email)
.map(SecurityUserDetails::new)
.orElseThrow(() -> new UsernameNotFoundException(email));
}
}
10 changes: 5 additions & 5 deletions src/test/java/com/linkurlshorter/urlshortener/TestConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ public JwtUtil jwtUtil() {
}

/**
* Creates a bean for CustomUserDetailsService with a mocked UserService dependency.
* Creates a bean for CustomUserDetailsService with a mocked UserRepository dependency.
*
* @param userService UserService mock bean
* @return CustomUserDetailsService bean with mocked UserService dependency
* @param userRepository UserRepository mock bean
* @return CustomUserDetailsService bean with mocked UserRepository dependency
*/
@Bean
public CustomUserDetailsService customUserDetailsService(UserService userService) {
return new CustomUserDetailsService(userService);
public CustomUserDetailsService customUserDetailsService(UserRepository userRepository) {
return new CustomUserDetailsService(userRepository);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void loginFailedWhenUserDoesNotExistTest() throws Exception {
.content(objectMapper.writeValueAsString(authRequest)))
.andExpect(MockMvcResultMatchers.status().is4xxClientError())
.andExpect(MockMvcResultMatchers.jsonPath("$.statusCode").value(401))
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("No user by provided email found"));
.andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Bad credentials"));
}

/**
Expand Down

0 comments on commit dc29e86

Please sign in to comment.