use of com.canoo.dp.impl.security.SecurityConstants.USER_CONTEXT in project dolphin-platform by canoo.
the class KeycloakSecurityContextExtractFilter method doFilter.
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
final HttpServletRequest req = (HttpServletRequest) request;
Assert.requireNonNull(chain, "chain");
final KeycloakSecurityContext securityContext = keyCloakSecurityExtractor.extractContext(request);
contextHolder.set(securityContext);
realmHolder.set(req.getHeader(REALM_NAME_HEADER));
appNameHolder.set(req.getHeader(APPLICATION_NAME_HEADER));
accessDenied.set(false);
final Subscription userContextSubscription = Optional.ofNullable(securityContext).map(c -> c.getToken()).map(t -> t.getPreferredUsername()).map(u -> ContextManagerImpl.getInstance().addThreadContext(USER_CONTEXT, u)).orElse(null);
try {
chain.doFilter(request, response);
} catch (Exception e) {
if (!accessDenied.get()) {
throw e;
} else {
LOG.error("SecurityContext error in request", e);
}
} finally {
Optional.ofNullable(userContextSubscription).ifPresent(s -> s.unsubscribe());
contextHolder.set(null);
boolean sendAccessDenied = accessDenied.get();
accessDenied.set(false);
if (sendAccessDenied) {
((HttpServletResponse) response).sendError(403, "Access Denied");
}
}
}
Aggregations