Skip to content

Commit

Permalink
refactor: BearerAuthorizationExtractor 메서드 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
yoondgu committed Aug 13, 2023
1 parent 0bca035 commit bad465f
Showing 1 changed file with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,36 @@

import com.mapbefine.mapbefine.auth.dto.AuthInfo;
import jakarta.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.Collections;
import org.springframework.stereotype.Component;

@Component
public class BearerAuthorizationExtractor implements AuthorizationExtractor<AuthInfo> {

private static final String BEARER_TYPE = "Bearer";

@Override
public AuthInfo extract(HttpServletRequest request) {
Enumeration<String> headers = request.getHeaders(AUTHORIZATION);
while (headers.hasMoreElements()) {
String value = headers.nextElement();
if ((value.toLowerCase().startsWith(BEARER_TYPE.toLowerCase()))) {
String authHeaderValue = value.substring(BEARER_TYPE.length()).trim();
int commaIndex = authHeaderValue.indexOf(',');
if (commaIndex > 0) {
authHeaderValue = authHeaderValue.substring(0, commaIndex);
}
return new AuthInfo(authHeaderValue);
}
}
return Collections.list(request.getHeaders(AUTHORIZATION))
.stream()
.filter(this::isBearerType)
.map(this::extractAuthHeaderValue)
.findFirst()
.map(AuthInfo::new)
.orElse(null);
}

return null;
private boolean isBearerType(String value) {
return value.toLowerCase().startsWith(BEARER_TYPE.toLowerCase());
}

private String extractAuthHeaderValue(String value) {
String authHeaderValue = value.substring(BEARER_TYPE.length()).trim();
int commaIndex = authHeaderValue.indexOf(',');
if (commaIndex > 0) {
return authHeaderValue.substring(0, commaIndex);
}
return authHeaderValue;
}

}

0 comments on commit bad465f

Please sign in to comment.