Bird
0
0

In a microservice written in Java, which code snippet correctly extracts the JWT token from the Authorization header "Bearer eyJ..."?

medium📝 Analysis Q5 of 15
Microservices - Authentication and Authorization
In a microservice written in Java, which code snippet correctly extracts the JWT token from the Authorization header "Bearer eyJ..."?
AString token = authHeader.split(":")[1];
BString token = authHeader.substring(7);
CString token = authHeader.replace("Bearer", "");
DString token = authHeader.substring(0, 7);
Step-by-Step Solution
Solution:
  1. Step 1: Understand the Authorization header format

    The header starts with "Bearer " which is 7 characters long.
  2. Step 2: Extract token by removing prefix

    Using substring(7) removes "Bearer " and returns the token string.
  3. Final Answer:

    String token = authHeader.substring(7); -> Option B
  4. Quick Check:

    Extract token by skipping first 7 chars [OK]
Quick Trick: Skip first 7 chars to get token [OK]
Common Mistakes:
MISTAKES
  • Splitting by colon instead of space
  • Removing 'Bearer' but leaving space
  • Taking substring of first 7 chars

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Microservices Quizzes