Skip to content

Commit

Permalink
refactor: BearerAuthorizationParser 불필요한 Component 등록 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
MoonJeWoong committed Nov 22, 2023
1 parent cc237d5 commit 23dcf55
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
public class AuthInterceptor implements HandlerInterceptor {

private final AuthService authService;
private final BearerAuthorizationParser bearerAuthorizationParser;

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Expand All @@ -24,7 +23,7 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
return true;
}
String authorizationHeader = request.getHeader(HttpHeaders.AUTHORIZATION);
String accessToken = bearerAuthorizationParser.parse(authorizationHeader);
String accessToken = BearerAuthorizationParser.parse(authorizationHeader);
Long memberId = authService.parseMemberId(accessToken);
request.setAttribute("memberId", memberId);
return HandlerInterceptor.super.preHandle(request, response, handler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
import java.util.Objects;
import org.springframework.stereotype.Component;

@Component
public class BearerAuthorizationParser {

private static final String TOKEN_TYPE = "Bearer";
private static final int TOKEN_TYPE_LOCATION = 0;
private static final int ACCESS_TOKEN_LOCATION = 1;
private static final int HEADER_SIZE = 2;

public String parse(String authorizationHeader) {
public static String parse(String authorizationHeader) {
validateIsNonNull(authorizationHeader);
String[] split = authorizationHeader.split(" ");
if (split.length != HEADER_SIZE || !split[TOKEN_TYPE_LOCATION].equals(TOKEN_TYPE)) {
Expand All @@ -21,7 +20,7 @@ public String parse(String authorizationHeader) {
return split[ACCESS_TOKEN_LOCATION];
}

private void validateIsNonNull(String authorizationHeader) {
private static void validateIsNonNull(String authorizationHeader) {
if (Objects.isNull(authorizationHeader)) {
throw new InvalidAuthorizationHeaderException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@

@SuppressWarnings("NonAsciiCharacters")
@DisplayNameGeneration(ReplaceUnderscores.class)
@SpringBootTest
class BearerAuthorizationParserTest {

@Autowired
private BearerAuthorizationParser bearerAuthorizationParser;

@Test
void 인증_헤더에서_액세스_토큰을_파싱한다() {
// given
Expand All @@ -26,7 +22,7 @@ class BearerAuthorizationParserTest {
String authorizationHeader = tokenType + " " + accessToken;

// when
String parsed = bearerAuthorizationParser.parse(authorizationHeader);
String parsed = BearerAuthorizationParser.parse(authorizationHeader);

// then
assertThat(parsed).isEqualTo(accessToken);
Expand All @@ -35,7 +31,7 @@ class BearerAuthorizationParserTest {
@Test
void 인증_헤더가_없으면_예외를_던진다() {
// given, when, then
assertThatThrownBy(() -> bearerAuthorizationParser.parse(null))
assertThatThrownBy(() -> BearerAuthorizationParser.parse(null))
.isInstanceOf(InvalidAuthorizationHeaderException.class);
}

Expand All @@ -47,7 +43,7 @@ class BearerAuthorizationParserTest {
String authorizationHeader = tokenType + " " + email;

// when, then
assertThatThrownBy(() -> bearerAuthorizationParser.parse(authorizationHeader))
assertThatThrownBy(() -> BearerAuthorizationParser.parse(authorizationHeader))
.isInstanceOf(InvalidAuthorizationHeaderException.class);
}
}

0 comments on commit 23dcf55

Please sign in to comment.