Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] WebRequestAuditorAware를 구현한다 #159

Closed
1 task done
seungki1011 opened this issue Dec 25, 2024 · 0 comments
Closed
1 task done

[Feature] WebRequestAuditorAware를 구현한다 #159

seungki1011 opened this issue Dec 25, 2024 · 0 comments
Assignees
Labels
feature New feature or request

Comments

@seungki1011
Copy link
Owner

seungki1011 commented Dec 25, 2024

🚀 설명

  • (AsIs) 현재 모든 API에 대해서 createdBy, lastModifiedBy는 세션의 로그인 사용자의 memberId를 기록하는 방식으로 사용하고 있다
  • (ToBe) 이슈 생성을 포함해서 워크스페이스 내에서 작업하는 모든 행동은 workspaceMemberId를 기록하는 것이 더 좋음
    • workspaceMember에서 각 워크스페이스에서 멤버의 권한, 별칭, 등을 관리하고 있기 때문!
    • memberId+workspaceCode를 통해 workspaceMember를 조회할 수 있겠지만, 성능적으로 비효율적이라고 생각함

💡아이디어

  • [Feature] API 설계를 개선한다  #157 추가 사항 참고
  • 그럼 차라리 AuthorizationInterceptor에서 workspaceMemberId를 반환하거나, 세션에 저장해서 사용하는 방식을 사용할 수는 없나?
    • -> HttpServletRequestrequest 속성에 저장해서 사용하는 방법
    • request의 생명주기는 API 요청이 들어가고 응답을 받을때 까지 살아있음
    • 명시적으로 관리해줄 필요 없음, API 요청이 끝나면 알아서 정리 됨
    • request의 속성에서 workspaceMemberId를 저장하고 꺼내서 사용하는 방법은, AuditorAware를 통해 memberId 대신 workspaceMemberId를 저장해서 사용하는 것에 적용 가능 할 듯

🤔 구현 방법

  • 바로 코드를 통해 알아보자
@Component
@RequiredArgsConstructor
public class WebRequestAuditorAware implements AuditorAware<Long> {

	private static final String WORKSPACE_API_PREFIX = "/api/v1/workspaces";

	private final HttpSession session;
	private final HttpServletRequest request;

	@Override
	public Optional<Long> getCurrentAuditor() {

		String requestUri = request.getRequestURI();

		// workspace API인 경우 workspaceMemberId 반환, workspaceMemberId가 null이면 넘어가기
		if (requestUri.startsWith(WORKSPACE_API_PREFIX)) {
			Long workspaceMemberId = (Long)request.getAttribute(
				AuthorizationInterceptor.CURRENT_WORKSPACE_MEMBER_ID
			);

			if (workspaceMemberId != null) {
				return Optional.of(workspaceMemberId);
			}
		}

		// 그 외의 경우 memberId 반환
		return Optional.ofNullable(
			(Long)session.getAttribute(SessionAttributes.LOGIN_MEMBER_ID)
		);
	}
}
  • /api/v1/workspaces으로 시작하면 CURRENT_WORKSPACE_MEMBER_IDrequest에서 꺼내서 확인
  • 만약 null이 아니면 바로 Optional.of(workspaceMemberId)를 반환하고, 해당 값을 생성자/수정자에 기록
    • -> 워크스페이스와 관련이 있는 작업
  • 만약 null이라면 기존 SessionAuditorAware 처럼 세션의 LOGIN_MEMBER_ID(Memberid)를 반환
    • /api/v1/workspaces으로 시작하는 요청이 아니거나, CURRENT_WORKSPACE_MEMBER_ID가 없는 경우
    • -> 워크스페이스와 관련 없는 작업이거나 워크스페이스를 생성하는 작업

🤔 추가로

  • 원래는 2개의 AuditorAware를 구현해서 사용하는 방법이 없나 찾아봤는데, 가능하지 않은 것 같다(참고: Allow having multiple AuditorAware(s)

  • URI에 대한 상수는 추후에 정리할 예정


✅ 작업 항목

  • WebRequestAuditorAware를 구현

🚩 관련 이슈, PR


📖 참고

@seungki1011 seungki1011 added the feature New feature or request label Dec 25, 2024
@seungki1011 seungki1011 self-assigned this Dec 25, 2024
@seungki1011 seungki1011 changed the title [Feature] WorkspaceRequestAuditorAware를 구현한다 [Feature] WebRequestAuditorAware를 구현한다 Dec 26, 2024
seungki1011 added a commit that referenced this issue Dec 26, 2024
…ditor-aware

Feat/#159 `WorkspaceMember`의 `id`를 기록하는 `WebRequestAuditorAware` 구현
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant