Created
January 30, 2022 19:49
-
-
Save GaetanoPiazzolla/9a52a53359b61b09f85939bfa7a398fe to your computer and use it in GitHub Desktop.
JWT Interceptor used to authorize requests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JWTInterceptor implements HandlerInterceptor { | |
@Value("${jwt.key}") | |
private String jwtKey; | |
@Autowired | |
private BlackListingService blackListingService; | |
@Autowired | |
private UserRequestScopedBean userRequestScopedBean; | |
@Override | |
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException { | |
try { | |
String token = request.getHeader("Authorization"); | |
token = token.substring(7); | |
Jwts.parser() | |
.setSigningKey(Base64.encodeBase64String(jwtKey.getBytes())) | |
.parseClaimsJws(token); | |
String blackListedToken = blackListingService.getJwtBlackList(token); | |
if (blackListedToken != null) { | |
log.error("JwtInterceptor: Token is blacklisted"); | |
response.sendError(401); | |
return false; | |
} | |
userRequestScopedBean.setJwt(token); | |
return true; | |
} catch (Exception e) { | |
log.error("JwtInterceptor - Exception : {} ",e.getMessage()); | |
response.sendError(401); | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Related to medium article: https://blog.devgenius.io/fixing-jwt-insecure-session-termination-by-blacklisting-tokens-36d783adfd67