Skip to content

Commit

Permalink
Fix CORS error with expired Access Token
Browse files Browse the repository at this point in the history
Signed-off-by: Claudio Mezzasalma <claudio.mezzasalma@eurotech.com>
  • Loading branch information
Claudio Mezzasalma authored and Coduz committed Jun 14, 2021
1 parent 02610d6 commit ceaf360
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
Expand Up @@ -75,7 +75,8 @@ protected AuthenticationToken createToken(ServletRequest request, ServletRespons
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
HttpServletResponse httpResponse = WebUtils.toHttp(response);
httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
return false;
// Continue with the filter chain, because CORS headers are still needed
return true;
}

}
Expand Up @@ -113,21 +113,24 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
// For the actual request it will be available and we will check the CORS according to the scope.
KapuaId scopeId = KapuaSecurityUtils.getSession() != null ? KapuaSecurityUtils.getSession().getScopeId() : null;

String msg = null;
if (checkOrigin(origin, scopeId)) {
// Origin matches at least one defined Endpoint
httpResponse.addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
httpResponse.addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, origin);
httpResponse.addHeader("Vary", HttpHeaders.ORIGIN);
} else {
String msg = scopeId != null ?
msg = scopeId != null ?
String.format("HTTP Origin not allowed: %s for scope: %s", origin, scopeId.toCompactId()) :
String.format("HTTP Origin not allowed: %s", origin);

logger.error(msg);
httpResponse.sendError(HttpServletResponse.SC_FORBIDDEN, msg);
}
int errorCode = httpResponse.getStatus();
if (errorCode >= 400) {
// if there's an error code at this point, return it and stop the chain
httpResponse.sendError(errorCode, msg);
return;
}

chain.doFilter(request, response);
}

Expand Down

0 comments on commit ceaf360

Please sign in to comment.