Spring Cloud - 18. Users Microservice 로그인 성공 처리

목차

Users Microservice 로그인 성공 처리

@Slf4j
public class AuthenticationFilter extends UsernamePasswordAuthenticationFilter {
private UserService userService;
private Environment env;

public AuthenticationFilter(AuthenticationManager authenticationManager, UserService userService, Environment env){
super(authenticationManager);
this.userService = userService;
this.env = env;
}

@Override
public Authentication attemptAuthentication(HttpServletRequest request,
HttpServletResponse response) throws AuthenticationException {

try {
RequestLogin creds = new ObjectMapper().readValue(request.getInputStream(), RequestLogin.class);

Authentication token =
new UsernamePasswordAuthenticationToken(creds.getEmail(), creds.getPassword(), new ArrayList<>());

return getAuthenticationManager().authenticate(token);
} catch (IOException e){
throw new RuntimeException(e);
}
}

@Override
protected void successfulAuthentication(HttpServletRequest request,
HttpServletResponse response,
FilterChain chain,
Authentication authResult) throws IOException, ServletException {
String username = ((User)authResult.getPrincipal()).getUsername();
UserDto userDetails = userService.getUserDetailsByEmail(username);
}
}
@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(authenticationManager(), userService, env);

return authenticationFilter;
}
}

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

UserDto getUserByUserId(String userId);

Iterable<UserEntity> getUserByAll();

UserDto getUserDetailsByEmail(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();
}

@Override
public UserDto getUserDetailsByEmail(String email) {
UserEntity userEntity = userRepository.findByEmail(email);

if(userEntity == null){
throw new UsernameNotFoundException(email);
}

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