Untitled

 avatar
unknown
plain_text
4 days ago
1.1 kB
4
Indexable
/*
    If a header with name user-context is found within the request,
    the filter builds the UserContext object from the header.
    If there is no header found in the request it generates
    new UserContext with unique globalCorrelationId

    For Servlet applications
*/
@Component
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class ServletUserContextInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        UserContext userContext;
        String userContextJson = request.getHeader(UserContext.USER_CONTEXT_KEY);
        if (Objects.nonNull(userContextJson)) {
            userContext = UserContextHolder.fromJson(
            		new String(Base64.getDecoder().decode(userContextJson.getBytes()))
            		);
        } else {
            userContext = new UserContext(SecurityContextHolder.getContext().getAuthentication());
        }
        UserContextHolder.setToServletContext(userContext);
        return true;
    }
}
Leave a Comment