Proper error message when bearer token is empty (#23903)

* Proper error message when bearer token is empty

* Refactored the method

---------

Co-authored-by: sonika-shah <58761340+sonika-shah@users.noreply.github.com>
This commit is contained in:
Ajith Prasad 2025-10-29 15:09:28 +05:30 committed by GitHub
parent 3760a2d612
commit a481b33264
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -278,24 +278,23 @@ public class JwtFilter implements ContainerRequestFilter {
protected static String extractToken(MultivaluedMap<String, String> headers) {
LOG.debug("Request Headers:{}", headers);
String source = headers.getFirst(AUTHORIZATION_HEADER);
if (nullOrEmpty(source)) {
throw AuthenticationException.getTokenNotPresentException();
}
// Extract the bearer token
if (source.startsWith(TOKEN_PREFIX)) {
return source.substring(TOKEN_PREFIX.length() + 1);
}
throw AuthenticationException.getTokenNotPresentException();
return extractTokenFromString(source);
}
public static String extractToken(String tokenFromHeader) {
LOG.debug("Request Token:{}", tokenFromHeader);
if (nullOrEmpty(tokenFromHeader)) {
return extractTokenFromString(tokenFromHeader);
}
private static String extractTokenFromString(String tokenString) {
if (nullOrEmpty(tokenString)) {
throw AuthenticationException.getTokenNotPresentException();
}
// Extract the bearer token
if (tokenFromHeader.startsWith(TOKEN_PREFIX)) {
return tokenFromHeader.substring(TOKEN_PREFIX.length() + 1);
if (tokenString.startsWith(TOKEN_PREFIX)) {
if (tokenString.length() <= TOKEN_PREFIX.length() + 1) {
throw AuthenticationException.getTokenNotPresentException();
}
return tokenString.substring(TOKEN_PREFIX.length() + 1);
}
throw AuthenticationException.getTokenNotPresentException();
}