Spring Projcect/[팀플] In & Out 가계부

회원 로그아웃 API

계란💕 2022. 10. 26. 18:11

 

 

  • 오류: : "JpaAuditingHandler"빈 생성 오류
    • Back-end 브랜치에서 User-logout 브랜치를 새로 판다.
    • 새로 만들었으니까 test 를 모두 실행한다.
    • 그런데 컨트롤러 테스트(수입 지출 회원)에서만 모두 오류가 났다.
<hide/>
BeanCreationException: Error creating bean with name 'jpaAuditingHandler': 
Cannot resolve reference to bean 'jpaMappingContext' while setting constructor argument; 

nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'jpaMappingContext': Invocation of init method failed;

nested exception is java.lang.IllegalArgumentException: JPA metamodel must not be empty!
  • 원인: "JPA metamodel"은 empty 상태이면 안 된다.
    • @WevMvcTestsms JPA 생성과 관련된 기능이 전혀 없다. 
    • JPA auditing 기능을 사용할 때, @SpringBootApplication에 @EnableJpaAuditing을 추가하여 사용하기 때문에 에러가 발생한다.
    • @EnableJpaAuditing는 각 엔티티의 생성일, 수정일, 등을 자동으로 등록하기 위해 이용한다.
    • 현재, 각 Member, Expense, Income 클래스마다  @AuditOverride(forClass = BaseEntity.class) 가 추가되어 있는 상태이다.
    • 그리고 BaseEntity에는 @EntityListeners(value = {AuditingEntityListener.class}) 가 붙어 있다.
  • 해결: 각 컨트롤러 테스트 클래스에 @MockBean(JpaMetamodelMappingContext.class)를 추가한다.
    • 그럼 이제 JPA metanodel이 생성된다.
    • 위와 다르게 @EnableJpaAuditing을 따로 분리해서 JpaAuditConfig 클래스를 만들 수도 있다.

 

 

  • 오류: CommunicationException
  • 원인: application.properties에 개발 모드, 배포 버전 두 가지 세팅이 있는데 배포 버전으로 설정했다가 오류가 났다...
  • 해결: 설정 변경

 

 

  • ctrl
<hide/>
@PostMapping("/signout")
@ApiOperation(value = "회원 로그아웃 API")
public ResponseEntity<?> signout() {
    memberService.logout();
    String message = "정상적으로 로그아웃을 완료했습니다.";
    return new ResponseEntity<>(message, HttpStatus.OK);
}

 

 

  • service
    • SecurityContextHolder는 인증 정보를 저장하는 저장소같은 개념이다.
    • getAuthentication() 해주면 현재 로그인한 회원의 정보를 조회할 수 있다.
    • 따라서, 로그인할 때는 setAuthentication()을 이용해서 로그인 정보를 저장할 수 있다.
<hide/>
@Override
public void logout() {

    if (SecurityContextHolder.getContext().getAuthentication() == null) {
        throw new MemberException(CANNOT_LOGOUT);
    }

    SecurityContextHolder.clearContext();

}

 

  • 테스트

 

 

 

 

 

Spring Security 관련 참고 블로그) https://catsbi.oopy.io/f9b0d83c-4775-47da-9c81-2261851fe0d0