

private void handleSpringSecurityException(HttpServletRequest request, HttpServletResponse response,
FilterChain chain, RuntimeException exception) throws IOException, ServletException {
if (exception instanceof AuthenticationException) {
handleAuthenticationException(request, response, chain, (AuthenticationException) exception);
}
else if (exception instanceof AccessDeniedException) {
handleAccessDeniedException(request, response, chain, (AccessDeniedException) exception);
}
}
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)
throws IOException, ServletException {
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
PrintWriter writer = response.getWriter();
writer.write(authException.getMessage());
writer.flush();
writer.close();
}
@Override
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException)
throws IOException, ServletException {
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
PrintWriter writer = response.getWriter();
writer.write(accessDeniedException.getMessage());
writer.flush();
writer.close();
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
try {
filterChain.doFilter(request, response);
} catch (ExpiredJwtException eje) { // JWT 만료 오류
log.error(eje.getMessage(), eje);
sendError(response, HttpServletResponse.SC_UNAUTHORIZED, "Access-Token is expired");
} catch (JwtException je) { // JWT 인증 오류
log.error(je.getMessage(), je);
sendError(response, HttpServletResponse.SC_UNAUTHORIZED, je.getMessage());
}
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
boolean skip = Arrays.stream(skipUrlList)
.anyMatch(url -> antPathMatcher.match(url, request.getRequestURI()));
if (skip) {
filterChain.doFilter(request, response);
return;
}
String token = jwtProvider.resolveToken(
request.getHeader(jwtProvider.getAccessTokenHeader())
);
log.info("login blacklist token = {}", token);
if (redisService.blackListTokenGet(token)) {
throw new JwtException("Token is blacklisted");
}
Payload payload = jwtProvider.verifyToken(token);
Authentication authenticated = LoginAuthentication.authenticated(payload, List.of());
SecurityContextHolder.getContext().setAuthentication(authenticated);
filterChain.doFilter(request, response);
}
try {
AuthorizationDecision decision = this.authorizationManager.check(this::getAuthentication, request);
this.eventPublisher.publishAuthorizationEvent(this::getAuthentication, request, decision);
if (decision != null && !decision.isGranted()) {
throw new AccessDeniedException("Access Denied");
}
chain.doFilter(request, response);
}
private static class AuthenticatedAuthorizationStrategy extends AbstractAuthorizationStrategy {
@Override
boolean isGranted(Authentication authentication) {
return this.trustResolver.isAuthenticated(authentication);
}
}
default boolean isAuthenticated(Authentication authentication) {
return authentication != null && authentication.isAuthenticated() && !isAnonymous(authentication);
}
front-end : https://github.com/onlydev7777/emotion-diary-react
back-end : https://github.com/onlydev7777/emotion-diary-monolithic