Skip to content

Commit

Permalink
fix : 헤더 값 요청 수정 (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
GaBaljaintheroom committed Oct 7, 2024
1 parent 5d94c09 commit 148b927
Show file tree
Hide file tree
Showing 50 changed files with 176 additions and 675 deletions.
37 changes: 28 additions & 9 deletions app/api/common-api/src/main/java/org/example/filter/JWTFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.io.IOException;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.example.security.dto.AuthenticatedUser;
import org.example.security.dto.AuthenticatedInfo;
import org.example.security.dto.UserParam;
import org.example.security.token.JWTHandler;
import org.example.security.token.TokenProcessor;
Expand All @@ -34,27 +34,46 @@ protected void doFilterInternal(
handleAccessToken(request);
}

if (request.getHeader("Refresh") != null) {
handleRefreshToken(request);
}

filterChain.doFilter(request, response);
}

private void handleAccessToken(HttpServletRequest request) {
String accessToken = jwtHandler.extractAccessToken(request);
UserParam userParam = jwtHandler.extractUserFrom(accessToken);
tokenProcessor.verifyAccessTokenBlacklist(userParam, accessToken);
saveOnSecurityContextHolder(userParam);
saveOnSecurityContextHolder(userParam, accessToken);
}

private void saveOnSecurityContextHolder(UserParam userParam, String accessToken) {
var authenticatedInfo = AuthenticatedInfo.getUserWithAccessToken(userParam, accessToken);

SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken(
authenticatedInfo,
null,
List.of(new SimpleGrantedAuthority(authenticatedInfo.role().getAuthority()))
)
);
}

private void handleRefreshToken(HttpServletRequest request) {
String refreshToken = jwtHandler.extractRefreshToken(request);
UserParam userParam = jwtHandler.extractUserFrom(refreshToken);
saveOnSecurityContextHolderWithRefreshToken(userParam, refreshToken);
}

private void saveOnSecurityContextHolder(UserParam userParam) {
AuthenticatedUser authenticatedUser = AuthenticatedUser.builder()
.userId(userParam.userId())
.role(userParam.role())
.build();
private void saveOnSecurityContextHolderWithRefreshToken(UserParam userParam, String refreshToken) {
var authenticatedInfo = AuthenticatedInfo.getUserWithRefreshToken(userParam, refreshToken);

SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken(
authenticatedUser,
authenticatedInfo,
null,
List.of(new SimpleGrantedAuthority(authenticatedUser.role().getAuthority()))
List.of(new SimpleGrantedAuthority(authenticatedInfo.role().getAuthority()))
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ public interface TokenRepository {

boolean existAccessTokenInBlacklist(UUID userId, String accessToken);

void deleteRefreshToken(UUID userId);
boolean deleteRefreshToken(UUID userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.example.security.dto;

import java.util.UUID;
import lombok.Builder;
import org.example.vo.UserRoleApiType;

@Builder
public record AuthenticatedInfo(
String accessToken,
String refreshToken,
UUID userId,
UserRoleApiType role
) {

public static AuthenticatedInfo getUserWithAccessToken(
UserParam userParam,
String accessToken
) {
return AuthenticatedInfo.builder()
.userId(userParam.userId())
.role(userParam.role())
.accessToken(accessToken)
.build();
}

public static AuthenticatedInfo getUserWithRefreshToken(
UserParam userParam,
String refreshToken
) {
return AuthenticatedInfo.builder()
.refreshToken(refreshToken)
.userId(userParam.userId())
.role(userParam.role())
.build();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ public void makeAccessTokenBlacklistAndDeleteRefreshToken(
UUID userId
) {
tokenRepository.saveBlacklistAccessToken(userId, accessToken);
tokenRepository.deleteRefreshToken(userId);

if (!tokenRepository.deleteRefreshToken(userId)) {
throw new BusinessException(TokenError.INVALID_TOKEN);
}
}

private String getExistRefreshToken(UserParam userParam) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.example.util;

import java.util.UUID;
import org.example.security.dto.AuthenticatedUser;
import org.example.security.dto.AuthenticatedInfo;

public final class ValidatorUser {

public static UUID getUserId(AuthenticatedUser user) {
public static UUID getUserId(AuthenticatedInfo user) {
if (user == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.example.dto.response.PaginationApiResponse;
import org.example.security.dto.AuthenticatedUser;
import org.example.security.dto.AuthenticatedInfo;
import org.example.util.ValidatorUser;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
Expand All @@ -40,18 +42,11 @@ public class ArtistController {
@GetMapping("/unsubscriptions")
@Operation(summary = "구독하지 않은 아티스트 목록 조회")
public ResponseEntity<PaginationApiResponse<ArtistUnsubscriptionPaginationApiParam>> getUnsubscribedArtists(
@AuthenticationPrincipal AuthenticatedUser user,
@AuthenticationPrincipal AuthenticatedInfo info,
@ParameterObject ArtistUnsubscriptionPaginationApiRequest request
) {
var response =
(user == null)
? artistService.findArtistUnsubscriptionsForNonUser(
request.toNonUserServiceRequest()
)
: artistService.findArtistUnsubscriptions(
request.toServiceRequest(user.userId())
);

UUID userId = ValidatorUser.getUserId(info);
var response = artistService.findArtistUnsubscriptions(request.toServiceRequest(userId));
var data = response.data().stream()
.map(ArtistUnsubscriptionPaginationApiParam::from)
.toList();
Expand All @@ -67,11 +62,11 @@ public ResponseEntity<PaginationApiResponse<ArtistUnsubscriptionPaginationApiPar
@GetMapping("/subscriptions")
@Operation(summary = "구독한 아티스트 목록 조회")
public ResponseEntity<PaginationApiResponse<ArtistSubscriptionPaginationApiParam>> getSubscribedArtists(
@AuthenticationPrincipal AuthenticatedUser user,
@AuthenticationPrincipal AuthenticatedInfo info,
@ParameterObject ArtistSubscriptionPaginationApiRequest request
) {
var response = artistService.findArtistSubscriptions(
request.toServiceRequest(user.userId()));
request.toServiceRequest(info.userId()));
var data = response.data().stream()
.map(ArtistSubscriptionPaginationApiParam::from)
.toList();
Expand All @@ -87,48 +82,49 @@ public ResponseEntity<PaginationApiResponse<ArtistSubscriptionPaginationApiParam
@GetMapping("/subscriptions/count")
@Operation(summary = "구독한 아티스트 수")
public ResponseEntity<NumberOfSubscribedArtistApiResponse> getNumberOfSubscribedArtist(
@AuthenticationPrincipal AuthenticatedUser user
@AuthenticationPrincipal AuthenticatedInfo info
) {
return ResponseEntity.ok(
NumberOfSubscribedArtistApiResponse.from(
artistService.countSubscribedArtists(user.userId())
artistService.countSubscribedArtists(info.userId())
)
);
}

@PostMapping("/subscribe")
@Operation(summary = "구독하기")
public ResponseEntity<ArtistSubscriptionApiResponse> subscribe(
@AuthenticationPrincipal AuthenticatedUser user,
@AuthenticationPrincipal AuthenticatedInfo info,
@Valid @RequestBody ArtistSubscriptionApiRequest request
) {
return ResponseEntity.ok(
ArtistSubscriptionApiResponse.from(
artistService.subscribe(request.toServiceRequest(user.userId()))
artistService.subscribe(request.toServiceRequest(info.userId()))
)
);
}

@PostMapping("/unsubscribe")
@Operation(summary = "구독 취소하기")
public ResponseEntity<ArtistUnsubscriptionApiResponse> unsubscribe(
@AuthenticationPrincipal AuthenticatedUser user,
@AuthenticationPrincipal AuthenticatedInfo info,
@Valid @RequestBody ArtistUnsubscriptionApiRequest request
) {
return ResponseEntity.ok(
ArtistUnsubscriptionApiResponse.from(
artistService.unsubscribe(request.toServiceRequest(user.userId()))
artistService.unsubscribe(request.toServiceRequest(info.userId()))
)
);
}

@GetMapping("/search")
@Operation(summary = "검색하기")
public ResponseEntity<PaginationApiResponse<ArtistSearchPaginationApiParam>> search(
@AuthenticationPrincipal AuthenticatedUser user,
@AuthenticationPrincipal AuthenticatedInfo info,
@ParameterObject ArtistSearchPaginationApiRequest request
) {
var response = artistService.searchArtist(request.toServiceRequest(user));
UUID userId = ValidatorUser.getUserId(info);
var response = artistService.searchArtist(request.toServiceRequest(userId));
var data = response.data().stream()
.map(ArtistSearchPaginationApiParam::from)
.toList();
Expand All @@ -144,11 +140,11 @@ public ResponseEntity<PaginationApiResponse<ArtistSearchPaginationApiParam>> sea
@GetMapping("/filter-total-count")
@Operation(summary = "필터링한 데이터의 총 개수 가져오기")
public ResponseEntity<ArtistFilterTotalCountApiResponse> filterTotalCount(
@AuthenticationPrincipal AuthenticatedUser user,
@AuthenticationPrincipal AuthenticatedInfo info,
@Valid @RequestBody ArtistFilterTotalCountApiRequest request
) {
var response = artistService.filterArtistTotalCount(
request.toServiceRequest(user.userId()));
request.toServiceRequest(info.userId()));

return ResponseEntity.ok(
ArtistFilterTotalCountApiResponse.from(response.totalCount())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.UUID;
import org.example.security.dto.AuthenticatedUser;

public record ArtistSearchPaginationApiRequest(

Expand All @@ -31,12 +30,7 @@ public record ArtistSearchPaginationApiRequest(
}
}

public ArtistSearchPaginationServiceRequest toServiceRequest(AuthenticatedUser user) {
UUID userId = null;
if (user != null) {
userId = user.userId();
}

public ArtistSearchPaginationServiceRequest toServiceRequest(UUID userId) {
return ArtistSearchPaginationServiceRequest.builder()
.userId(userId)
.sortStandard(sortStandard)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,4 @@ public ArtistUnsubscriptionPaginationServiceRequest toServiceRequest(UUID userId
.size(size)
.build();
}

public ArtistUnsubscriptionPaginationServiceRequest toNonUserServiceRequest() {
return ArtistUnsubscriptionPaginationServiceRequest.builder()
.subscriptionStatusApiType(SubscriptionStatusApiType.UNSUBSCRIBED)
.sortStandard(sortStandard)
.artistGenderApiTypes(artistGenderApiTypes)
.artistApiTypes(artistApiTypes)
.genreIds(genreIds)
.userId(null)
.cursor(cursor)
.size(size)
.build();
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ public PaginationServiceResponse<ArtistSubscriptionPaginationServiceParam> findA
public PaginationServiceResponse<ArtistUnsubscriptionPaginationServiceParam> findArtistUnsubscriptions(
ArtistUnsubscriptionPaginationServiceRequest request
) {
List<UUID> subscriptionArtistIds = getSubscriptionArtistIds(request.userId());
List<UUID> subscriptionArtistIds = request.userId() == null
? List.of()
: getSubscriptionArtistIds(request.userId());

var response = artistUseCase.findAllArtistInCursorPagination(
request.toDomainRequest(subscriptionArtistIds));
Expand All @@ -177,27 +179,14 @@ public PaginationServiceResponse<ArtistUnsubscriptionPaginationServiceParam> fin
return PaginationServiceResponse.of(data, response.hasNext());
}

public PaginationServiceResponse<ArtistUnsubscriptionPaginationServiceParam> findArtistUnsubscriptionsForNonUser(
ArtistUnsubscriptionPaginationServiceRequest request
) {
var response = artistUseCase.findAllArtistInCursorPagination(
request.toNonUserDomainRequest());
List<ArtistUnsubscriptionPaginationServiceParam> data = response.data().stream()
.map(ArtistUnsubscriptionPaginationServiceParam::new)
.toList();

return PaginationServiceResponse.of(data, response.hasNext());
}

public NumberOfSubscribedArtistServiceResponse countSubscribedArtists(UUID userId) {
return NumberOfSubscribedArtistServiceResponse.from(
artistSubscriptionUseCase.countSubscribedArtists(userId)
);
}

private List<UUID> getSubscriptionArtistIds(UUID userId) {
List<ArtistSubscription> subscriptions = artistSubscriptionUseCase.findSubscriptionList(
userId);
List<ArtistSubscription> subscriptions = artistSubscriptionUseCase.findSubscriptionList(userId);

return subscriptions.stream()
.map(ArtistSubscription::getArtistId)
Expand Down
Loading

0 comments on commit 148b927

Please sign in to comment.