Web 17

isDuplicate 속성을 가진 DTO가 isDuplicate, duplicate 응답하는 에러

✅ 상황 요약다음 DTO 선언:@Getter@AllArgsConstructorpublic class EmailDuplicateCheckResponse { @JsonProperty("isDuplicate") private boolean isDuplicate;}그런데 JSON 응답은 이렇게 두 개의 필드를 포함:{ "isDuplicate": false, "duplicate": false}❗ 문제의 원인@Getter를 사용하면 Lombok이 isDuplicate()라는 getter 메서드를 생성:public boolean isDuplicate() { return this.isDuplicate;}그런데 Jackson은 boolean 타입일 때 isX() 메서드로부터 자동으로 속성 이름을 추론..

테스트에서 Entity vs DTO 이용

✅ 테스트에서 Request DTO를 사용하는 게 더 나은 경우🔹 테스트가 Controller → Service 흐름을 따를 때@WebMvcTest, MockMvc, 통합 테스트 등에서→ 실제 HTTP 요청처럼 흐름을 시뮬레이션하는 경우엔 CreatePostRequest DTO를 사용하는 게 자연스럽고, 검증이 더 현실적입니다.mockMvc.perform(post("/api/posts") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(new CreatePostRequest("제목", "내용")))) .andExpect(status().isOk());🟡 반면, 단위 테스트 ..

Spring @ModelAttribute 매핑 오류 해결 방법 (Setter, Enum 변환)

Spring Boot에서 @ModelAttribute를 사용하여 Query String을 DTO(PostSearchOptions)에 바인딩할 때, 값이 매핑되지 않는 문제가 발생했습니다.문제 상황: Query String이 PostSearchOptions에 매핑되지 않음예를 들어, 다음과 같은 컨트롤러가 있다고 가정해봅시다.@GetMapping("/list")public ResponseEntity> getAllPostsWithSearchOptionsByPage( Pageable pageable, @ModelAttribute PostSearchOptions postSearchOptions // 🔥 Query String → 객체 매핑) { return ResponseEntity.ok( ..

Spring Data JPA에서 커스텀 Repository 구현체 인식 및 작동 방식

Spring Data JPA의 Repository 시스템 이해하기Spring Data JPA를 사용하면 기본적인 CRUD 기능을 손쉽게 제공받을 수 있습니다.예를 들어, JpaRepository를 상속받으면 findById(), save(), delete() 등의 메서드를 자동으로 사용할 수 있죠.public interface PostRepository extends JpaRepository {}위 코드만으로도 Post 엔티티에 대한 기본적인 데이터 접근 기능을 사용할 수 있습니다.하지만, 복잡한 쿼리나 커스텀 로직이 필요할 때는 어떻게 해야 할까요? 이럴 때 필요한 것이 바로 커스텀 Repository 구현체입니다.Spring Data JPA의 커스텀 Repository 적용 방법Spring Data ..

Mockito를 이용한 테스트 vs. SpringBootTest의 차이점

테스트를 진행할 때 Mockito 기반의 단위 테스트(Unit Test) 와 SpringBootTest 기반의 통합 테스트(Integration Test) 는 각기 다른 목적과 장점이 있습니다.1️⃣ Mockito를 이용한 단위 테스트 (Unit Test)💡 목표:특정 클래스의 동작이 개별적으로 올바르게 동작하는지 검증.Spring 컨텍스트를 로드하지 않고 빠르게 테스트 실행.Mock 객체를 사용하여 실제 데이터베이스와의 상호작용을 차단.💡 특징:✅ Spring의 컨텍스트를 사용하지 않으므로 테스트 속도가 빠름✅ Mock 객체를 이용하여 DB 등의 외부 의존성을 제거 (테스트가 독립적)✅ 특정 메서드가 의도한 대로 동작하는지 세밀하게 검증 가능 💡 Mock 객체 (@Mock)postRepositor..

Spring Security 6.x.x OAuth2.0 JWT 기반 인증 인가

application.properties#registrationspring.security.oauth2.client.registration.서비스명.client-name=서비스명spring.security.oauth2.client.registration.서비스명.client-id=서비스에서 발급 받은 아이디spring.security.oauth2.client.registration.서비스명.client-secret=서비스에서 발급 받은 비밀번호spring.security.oauth2.client.registration.서비스명.redirect-uri=서비스에 등록한 우리쪽 로그인 성공 URIspring.security.oauth2.client.registration.서비스명.authorization-gr..

Web/Spring Security 2025.02.26

JWT 방식 OAuth2.0 클라이언트 동작 원리

무지개 색 순서로 화살표 잘 따라가보기1. 클라이언트가 소셜 로그인 시도(/auth/authentication/서비스) 2. OAuth2AuthorizationReqeustRedirectFilter에서 소셜 로그인 서비스 인증서버로 인증 요청 3. 인증서버에서 클라이언트에게 해당 서비스 로그인 페이지 응답 4. 로그인 성공 시, 인증 서버에서 우리 서버 주소로 리다이렉트 후OAuth2LoginAuthenticationFilter에서 Code 획득(/login/oauth2/code/서비스) 5. OAuth2LoginAuthenticationProvider로 Code를 넘기고, 여기서 인증서버로 코드를 넘겨서 Access 토큰을 발급 받음 6. 발급받은 Access 토큰으로  소셜 로그인 서비스 리소스 서버..

Web/Spring Security 2025.02.26

Spring Security 6.x.x JWT 보안 요소 추가

https://developer-yong.tistory.com/43 JWT 다중 토큰 발급JWT를 이용한 인증/인가는, 토큰이 XSS, HTTP통신 가로채기 등으로 탈취되었을 때 해커가 서비스를 악용할 수 있다. 따라서, 토큰 탈취 방지 및 탈취되었을 때 대비하는 기술이 존재한다. 다중 토큰developer-yong.tistory.comJWT를 단일 토큰 방식으로 사용할 경우, 위와같은 문제점이 존재한다. 따라서, 단일 토큰 방식에서 다중 토큰 발급 방식으로 변경이 필요하다. LoginFilter.java@Overrideprotected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, Filter..

Web/Spring Security 2025.02.20