0
0
Spring Bootframework~10 mins

JWT validation filter in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare the filter class that extends the correct Spring Security filter.

Spring Boot
public class JwtValidationFilter extends [1] { }
Drag options to blanks, or click blank then click option'
AHttpServlet
BOncePerRequestFilter
CFilterChain
DWebSecurityConfigurerAdapter
Attempts:
3 left
💡 Hint
Common Mistakes
Extending HttpServlet instead of OncePerRequestFilter
Using FilterChain as a class to extend
Extending WebSecurityConfigurerAdapter which is for configuration
2fill in blank
medium

Complete the method signature to override the filter logic.

Spring Boot
@Override
protected void [1](HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { }
Drag options to blanks, or click blank then click option'
AdoFilter
BprocessFilter
CfilterRequest
DdoFilterInternal
Attempts:
3 left
💡 Hint
Common Mistakes
Using doFilter instead of doFilterInternal
Using incorrect method names like filterRequest or processFilter
3fill in blank
hard

Fix the error in extracting the JWT token from the Authorization header.

Spring Boot
String authHeader = request.getHeader("Authorization");
if (authHeader != null && authHeader.startsWith([1])) {
    String token = authHeader.substring(7);
}
Drag options to blanks, or click blank then click option'
A"JWT "
B"Token "
C"Bearer "
D"Auth "
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Token ' instead of 'Bearer '
Using 'JWT ' or 'Auth ' which are not standard prefixes
4fill in blank
hard

Fill both blanks to validate the token and set authentication in the security context.

Spring Boot
if (jwtUtil.validateToken([1])) {
    UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(
        userDetails, null, userDetails.[2]());
    SecurityContextHolder.getContext().setAuthentication(authToken);
}
Drag options to blanks, or click blank then click option'
Atoken
BgetAuthorities
CgetCredentials
Duser
Attempts:
3 left
💡 Hint
Common Mistakes
Passing user instead of token to validateToken
Using getCredentials instead of getAuthorities
5fill in blank
hard

Fill all three blanks to complete the filter chain call and handle exceptions properly.

Spring Boot
try {
    [1].doFilter([2], [3]);
} catch (JwtException e) {
    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
}
Drag options to blanks, or click blank then click option'
AfilterChain
Brequest
Cresponse
DauthToken
Attempts:
3 left
💡 Hint
Common Mistakes
Passing authToken instead of request or response
Calling doFilter on request or response instead of filterChain