Skip to content

Commit

Permalink
Add/#82 fix auditing (#83)
Browse files Browse the repository at this point in the history
* add: dto에 valid message 추가

* add: controller에 valid 옵션 추가

* refactor: 폴더 구조 변경

* add: 엔티티에 생성 및 수정 User 기록 Auditing 추가

* refactor: 폴더 구조 변경

* fix: unchecked call to ~ warning 제거

* fix: 코드에 타입 캐스팅 중복 제거

* fix: 코드에 타입 캐스팅 중복 제거

* refactor: 코드 계층 구조 변경
  • Loading branch information
dong2ast authored Aug 14, 2023
1 parent 55315d2 commit bea4040
Show file tree
Hide file tree
Showing 35 changed files with 139 additions and 89 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.sophy.sophy.common.component;

import java.util.Optional;
import org.springframework.data.domain.AuditorAware;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

@Component
public class UserAuditorAware implements AuditorAware<String> {

@Override
public Optional<String> getCurrentAuditor() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

if (authentication == null || !authentication.isAuthenticated()) {
return Optional.empty();
}

return Optional.of(((UserDetails) authentication.getPrincipal()).getUsername());
}
}
10 changes: 3 additions & 7 deletions src/main/java/org/sophy/sophy/controller/AuthController.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import org.sophy.sophy.controller.dto.response.TokenDto;
import org.sophy.sophy.exception.SuccessStatus;
import org.sophy.sophy.jwt.TokenProvider;
import org.sophy.sophy.service.AuthService;
import org.sophy.sophy.service.EmailService;
import org.sophy.sophy.service.common.AuthService;
import org.sophy.sophy.service.common.EmailService;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
Expand Down Expand Up @@ -59,14 +59,10 @@ public ApiResponseDto<TokenDto> login(
@Operation(summary = "로그아웃")
@SecurityRequirement(name = "JWT Auth")
public ApiResponseDto<String> logout(@Parameter(hidden = true) HttpServletRequest request) {
/**
* HttpServletRequest나 HttpServletResponse 객체가 Service 계층으로 넘어가는 것은 좋지 않다.
* request, response는 컨트롤러 계층에서 사용되는 객체이며, Service 계층이 request와 response를 알 필요가 없다.
*/
String accessToken = tokenProvider.resolveAccessToken(request);
return ApiResponseDto.success(SuccessStatus.LOGOUT_SUCCESS,
authService.logout(accessToken));
}
} // HttpServletRequest 나 HttpServletResponse 객체가 Service 계층으로 넘어가는 것은 좋지 않다.

@PostMapping("/reissue")
@Operation(summary = "액세스 토큰 재발행")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import org.sophy.sophy.domain.dto.booktalk.response.BooktalkDeleteResponseDto;
import org.sophy.sophy.domain.dto.mypage.MyPageBooktalkDto;
import org.sophy.sophy.exception.SuccessStatus;
import org.sophy.sophy.service.api.BooktalkService;
import org.sophy.sophy.service.api.MemberService;
import org.sophy.sophy.service.BooktalkService;
import org.sophy.sophy.service.MemberService;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.sophy.sophy.domain.dto.booktalk.response.BooktalkResponseDto;
import org.sophy.sophy.domain.enumerate.City;
import org.sophy.sophy.exception.SuccessStatus;
import org.sophy.sophy.service.api.BooktalkService;
import org.sophy.sophy.service.BooktalkService;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.sophy.sophy.common.dto.ApiResponseDto;
import org.sophy.sophy.domain.dto.HomeResponseDto;
import org.sophy.sophy.exception.SuccessStatus;
import org.sophy.sophy.service.api.HomeService;
import org.sophy.sophy.service.HomeService;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.sophy.sophy.domain.dto.mypage.MyPageDto;
import org.sophy.sophy.domain.dto.mypage.MyInfoDto;
import org.sophy.sophy.exception.SuccessStatus;
import org.sophy.sophy.service.api.MemberService;
import org.sophy.sophy.service.MemberService;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.sophy.sophy.domain.Booktalk;
import org.sophy.sophy.service.api.OperatorService;
import org.sophy.sophy.service.OperatorService;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.sophy.sophy.domain.dto.place.request.PlaceRequestDto;
import org.sophy.sophy.domain.enumerate.City;
import org.sophy.sophy.exception.SuccessStatus;
import org.sophy.sophy.service.api.PlaceService;
import org.sophy.sophy.service.PlaceService;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.sophy.sophy.common.dto.ApiResponseDto;
import org.sophy.sophy.domain.dto.SophyStoryDto;
import org.sophy.sophy.domain.dto.SophyStoryRequestDto;
import org.sophy.sophy.exception.SuccessStatus;
import org.sophy.sophy.service.api.SophyStoryService;
import org.sophy.sophy.service.SophyStoryService;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;
Expand All @@ -27,7 +28,7 @@ public class SophyStoryController {
@GetMapping //소피스토리 연, 월로 조회
public ApiResponseDto<List<SophyStoryDto>> geyMySophyStory(
@Parameter(hidden = true) @AuthenticationPrincipal User user,
@RequestBody SophyStoryRequestDto sophyStoryRequestDto) {
@RequestBody @Valid SophyStoryRequestDto sophyStoryRequestDto) {
return ApiResponseDto.success(SuccessStatus.GET_SOPHY_STORY_SUCCESS,
sophyStoryService.getMySophyStory(user.getUsername(), sophyStoryRequestDto));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sophy.sophy.controller;
package org.sophy.sophy.controller.other;

import io.swagger.v3.oas.annotations.Hidden;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sophy.sophy.controller;
package org.sophy.sophy.controller.other;

import io.swagger.v3.oas.annotations.Hidden;
import org.sophy.sophy.common.dto.ApiResponseDto;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/sophy/sophy/domain/Booktalk.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import org.sophy.sophy.domain.common.AuditingEntity;
import org.sophy.sophy.domain.common.ScheduledBooktalk;

@Entity
@Getter
@NoArgsConstructor
@SQLDelete(sql = "UPDATE booktalk SET deleted = true WHERE booktalk_id=?")
@Where(clause = "deleted=false")
public class Booktalk extends AuditingTimeEntity {
public class Booktalk extends AuditingEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.sophy.sophy.domain.enumerate.BookCategory;
import org.sophy.sophy.domain.common.AuditingTimeEntity;

@Entity
@Getter
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/sophy/sophy/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import javax.validation.constraints.Pattern;
import java.util.ArrayList;
import java.util.List;
import org.sophy.sophy.domain.common.AuditingTimeEntity;

@Entity
@Getter
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/sophy/sophy/domain/MemberBooktalk.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import lombok.NoArgsConstructor;

import javax.persistence.*;
import org.sophy.sophy.domain.common.AuditingEntity;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class MemberBooktalk extends AuditingTimeEntity {
public class MemberBooktalk extends AuditingEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/sophy/sophy/domain/Place.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import org.sophy.sophy.domain.common.AuditingEntity;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Place extends AuditingTimeEntity {
public class Place extends AuditingEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/org/sophy/sophy/domain/common/AuditingEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.sophy.sophy.domain.common;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import lombok.Getter;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

@Getter
@EntityListeners(AuditingEntityListener.class)
@MappedSuperclass
public class AuditingEntity extends AuditingTimeEntity {

@CreatedBy
@Column(updatable = false)
private String createdBy;

@LastModifiedBy
private String lastModifiedBy;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.sophy.sophy.domain;
package org.sophy.sophy.domain.common;

import javax.persistence.Column;
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
Expand All @@ -15,6 +16,7 @@
public class AuditingTimeEntity {

@CreatedDate
@Column(updatable = false)
private LocalDateTime createAt;

@LastModifiedDate
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package org.sophy.sophy.domain;
package org.sophy.sophy.domain.common;

import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import org.sophy.sophy.domain.Booktalk;

@Entity
@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package org.sophy.sophy.domain;
package org.sophy.sophy.domain.common;

import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import org.sophy.sophy.domain.Booktalk;

@Entity
@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
@Getter
public class CityRequestDto {

@NotNull
@NotNull(message = "유효하지 않은 지역입니다.")
private City city;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,35 @@
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class BooktalkUpdateDto {

@NotNull
@NotNull(message = "유효하지 않은 공간 ID입니다.")
@Schema(description = "장소 Id", example = "1")
private Long placeId;
private String booktalkImageUrl;
@NotBlank
@NotBlank(message = "유효하지 않은 북토크 제목입니다.")
@Schema(description = "북토크 이름", example = "소나기")
private String title;
@NotNull
@NotNull(message = "유효하지 않은 책 분야입니다.")
@Schema(description = "책 분야", example = "LITERATURE")
private BookCategory bookCategory;
@NotNull
@NotNull(message = "유효하지 않은 책 ID입니다.")
@Schema(description = "책 Id", example = "1")
private Long bookId;
@NotNull
@NotNull(message = "유효하지 않은 시작 날짜입니다.")
@Schema(description = "북토크 시작시간", example = "2023-08-12 15:00:00")
private LocalDateTime startDate; //TODO 시작 시간은 오늘날짜 이전은 안되도록?
@NotNull
@NotNull(message = "유효하지 않은 종료 날짜입니다.")
@Schema(description = "북토크 종료시간", example = "2023-08-12 17:00:00")
private LocalDateTime endDate;
@NotNull
@NotNull(message = "유효하지 않은 북토크 참가 인원입니다.")
@Schema(description = "북토크 참가 인원", example = "8")
private Integer participant;
@NotNull
@NotNull(message = "유효하지 않은 북토크 참가비입니다.")
@Schema(description = "북토크 참가 비", example = "1000")
private Integer participationFee;
@NotNull
@NotNull(message = "유효하지 않은 북토크 사전 준비 사항입니다.")
@Schema(description = "북토크 사전 준비 사항", example = "PRE_READING")
private PreliminaryInfo preliminaryInfo;
@NotBlank
@NotBlank(message = "유효하지 않은 북토크 상세 설명입니다.")
@Schema(description = "북토크 상세 설명", example = "밖에 비온다 주륵주륵")
private String description;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
@Getter
public class BooktalkParticipationRequestDto {

@NotNull
@Schema(description = "북토크 Id", example = "1")
@NotNull(message = "유효하지 않은 북토크 ID입니다.")
@Schema(description = "북토크 Id", example = "5")
private Long booktalkId;

public MemberBooktalk toMemberBooktalk(Booktalk booktalk, Member member) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,36 @@
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class BooktalkRequestDto {

@NotNull
@NotNull(message = "유효하지 않은 공간 ID입니다.")
@Schema(description = "장소 Id", example = "1")
private Long placeId;

private MultipartFile booktalkImage;
@NotBlank
@NotBlank(message = "유효하지 않은 북토크 제목입니다.")
@Schema(description = "북토크 이름", example = "소나기")
private String title;
@NotNull
@NotNull(message = "유효하지 않은 책 분야입니다.")
@Schema(description = "책 분야", example = "LITERATURE")
private BookCategory bookCategory;
@NotNull
@NotNull(message = "유효하지 않은 책 ID입니다.")
@Schema(description = "책 Id", example = "1")
private Long bookId;
@NotNull
@NotNull(message = "유효하지 않은 시작 날짜입니다.")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
private LocalDateTime startDate;
@NotNull
@NotNull(message = "유효하지 않은 종료 날짜입니다.")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
private LocalDateTime endDate;
@NotNull
@NotNull(message = "유효하지 않은 북토크 참가 인원입니다.")
@Schema(description = "북토크 참가 인원", example = "8")
private Integer participant;
@NotNull
@Schema(description = "북토크 참가 비", example = "1000")
@NotNull(message = "유효하지 않은 북토크 참가비입니다.")
@Schema(description = "북토크 참가비", example = "1000")
private Integer participationFee;
@NotNull
@NotNull(message = "유효하지 않은 북토크 사전 준비 사항입니다.")
@Schema(description = "북토크 사전 준비 사항", example = "PRE_READING")
private PreliminaryInfo preliminaryInfo;
@NotBlank
@NotBlank(message = "유효하지 않은 북토크 상세 설명입니다.")
@Schema(description = "북토크 상세 설명", example = "밖에 비온다 주륵주륵")
private String description;

Expand Down
Loading

0 comments on commit bea4040

Please sign in to comment.