Spring Cloud - 16. Users Microservice loadUserByUsername() 구현

목차

Users Microservice loadUserByUsername() 구현

public interface UserService extends UserDetailsService {
UserDto createUser(UserDto userDto);

UserDto getUserByUserId(String userId);

Iterable<UserEntity> getUserByAll();
}
public interface UserRepository extends CrudRepository<UserEntity, Long> {

UserEntity findByUserId(String userId);
UserEntity findByEmail(String username);
}
@Service
public class UserServiceImpl implements UserService {
UserRepository userRepository;
BCryptPasswordEncoder passwordEncoder;

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UserEntity userEntity = userRepository.findByEmail(username);

if (UserEntity == null) {
throw new UsernameNotFoundException(username);
}

return new User(userEntity.getEmail()
, userEntity.getEncryptedPwd()
, true
, true
, true
, true
, new ArrayList<>());
}

public UserServiceImpl(UserRepository userRepository, BCryptPasswordEncoder passwordEncoder) {
this.userRepository = userRepository;
this.passwordEncoder = passwordEncoder;
}

@Override
public UserDto createUser(UserDto userDto) {
userDto.setUserId(UUID.randomUUID().toString());

ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
UserEntity userEntity = modelMapper.map(userDto, UserEntity.class);
userEntity.setEncryptedPwd(passwordEncoder.encode(userDto.getPwd()));

userRepository.save(userEntity);

UserDto returnUserDto = modelMapper.map(userEntity, UserDto.class);

return returnUserDto;
}

@Override
public UserDto getUserByUserId(String userId) {
UserEntity userEntity = userRepository.findByUserId(userId);

if (userEntity == null)
throw new UsernameNotFoundException("User not found");

UserDto userDto = new ModelMapper().map(userEntity, UserDto.class);

List<ResponseOrder> orders = new ArrayList<>();
userDto.setOrders(orders);

return userDto;
}

@Override
public Iterable<UserEntity> getUserByAll() {
return userRepository.findAll();
}
}
@Configuration
@EnableWebSecurity
public class WebSecurity extends WebSecurityConfigurerAdapter {

private Environment env;
private UserService userService;
private BCryptPasswordEncoder bCryptPasswordEncoder;

public WebSecurity(Environment evn, UserService userService, BCryptPasswordEncoder bCryptPasswordEncoder){
this.env = env;
this.userService = userService;
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userService).passwordEncoder(bCryptPasswordEncoder);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();

http.authorizeRequests().antMatchers("/users/**")
.hasIpAddress("192.168.0.2")
.and()
.addFilter(getAuthenticationFilter());

http.headers().frameOptions().disable();
}

private AuthenticationFilter getAuthenticationFilter() throws Exception{
AuthenticationFilter authenticationFilter = new AuthenticationFilter();
authenticationFilter.setAuthenticationManager(authenticationManager());

return authenticationFilter;
}

@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
Share