레스토랑 예약 사이트 만들기 14 - 테이블 예약
패스트 캠퍼스에서 @RestController@RequiredArgsConstructorpublic class ReservationController { private final ReservationService reservationService; @GetMapping("/reservations") public List<Reservation> list(Authentication authentication){ Claims claims = (Claims) authentication.getPrincipal(); Long restaurantId = claims.get("restaurantId", Long.class);// Long restaurantId = 1004L; List<Reservation> reservations = reservationService.getReservations(restaurantId); return reservations; }} @WebMvcTest(ReservationController.class)class ReservationControllerTest { @Autowired private MockMvc mockMvc; @MockBean private ReservationService reservationService; @Test @DisplayName("예약목록을 가져온다.") public void list() throws Exception { String token = "eyJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOjEwMDQsIm5hbWUiOiJPd25lciIsInJlc3RhdXJhbnRJZCI6MTAwNH0.cQTXhzTW48F5Nj3eXa80Y9J4OJryzFvoHtT8ELl4kTw"; ResultActions resultActions = mockMvc.perform(get("/reservations") .header("Authorization", "Bearer " + token)); resultActions .andExpect(status().isOk()); verify(reservationService).getReservations(1004L); }} @Service@RequiredArgsConstructorpublic class ReservationService { private final ReservationRepository reservationRepository; public List<Reservation> getReservations(Long restaurantId) { return reservationRepository.findAllByRestaurantId(restaurantId); }} class ReservationServiceTest { @Mock private ReservationRepository reservationRepository; private ReservationService reservationService; @BeforeEach public void setUp(){ MockitoAnnotations.openMocks(this); this.reservationService = new ReservationService(reservationRepository); } @Test @DisplayName("예약목록들을 가져온다.") public void getReservation(){ Long restaurantId = 1004L; List<Reservation> reservations = reservationService.getReservations(restaurantId); verify(reservationRepository).findAllByRestaurantId(restaurantId); }} public class JwtAuthenticationFilter extends BasicAuthenticationFilter { private final JwtUtil jwtUtil; public JwtAuthenticationFilter(AuthenticationManager authenticationManager, JwtUtil jwtUtil) { super(authenticationManager); this.jwtUtil = jwtUtil; } @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { Authentication authentication = getAuthentication(request); if(authentication != null){ SecurityContext context = SecurityContextHolder.getContext(); context.setAuthentication(authentication); } chain.doFilter(request, response); } private Authentication getAuthentication(HttpServletRequest request){ // Header에서 Data를 얻어야 한다. String token = request.getHeader("Authorization"); if(token == null){ return null; } Claims claims = jwtUtil.getClaims(token.substring("Bearer ".length())); Authentication authentication = new UsernamePasswordAuthenticationToken(claims, null); return authentication; }} @Configuration@EnableWebSecuritypublic class SecurityConfig extends WebSecurityConfigurerAdapter { @Value("${jwt.secret}") private String secret; @Override protected void configure(HttpSecurity http) throws Exception { Filter filter = new JwtAuthenticationFilter(authenticationManager(), jwtUtil()); http .cors().disable() .csrf().disable() .formLogin().disable() .headers().frameOptions().disable(); http .addFilter(filter) .sessionManagement() // Session을 사용하지 않음 .sessionCreationPolicy(SessionCreationPolicy.STATELESS); } @Bean public PasswordEncoder passwordEncoder(){ return new BCryptPasswordEncoder(); } @Bean public JwtUtil jwtUtil(){ return new JwtUtil(secret); }}