목차
Spring Cloud - 20. Users Microservice AuthorizationHeaderFilter 추가
Spring Cloud - 19. Users Microservice JWT 생성
Spring Cloud - 18. Users Microservice 로그인 성공 처리
Spring Cloud - 17. Users Microservice Routes 정보 변경
Spring Cloud - 16. Users Microservice loadUserByUsername() 구현
Spring Cloud - 15. Users Microservice AuthenticationFilter
Spring Cloud - 14. Users Microservice Order Service
Spring Cloud - 13. Users Microservice Catalog
Spring Cloud - 12. Users Microservice 사용자 조회
Spring Cloud - 11. Users Microservice Gateway 연동
Spring Cloud - 10. Users Microservice 사용자 추가
Spring Cloud로 개발하는 마이크로서비스 애플리케이션 9
Spring Cloud - Users Microservice
Users Microservice 로그인 성공 처리@Slf4jpublic 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@EnableWebSecuritypublic 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);}
@Servicepublic 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; }}