<commit_before>package com.nextgearcapital.feign.oauth2.autoconfigure.hystrix;

import com.netflix.hystrix.HystrixInvokable;
import com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * A HystrixCommandExecutionHook that makes the Spring SecurityContext available during the execution of HystrixCommands.
 *
 * It extracts the SecurityContext from the SecurityContextHystrixRequestVariable and sets it on the SecurityContextHolder.
 */
public class SecurityContextRegistratorCommandHook extends HystrixCommandExecutionHook {

    @Override
    public <T> void onExecutionStart(HystrixInvokable<T> commandInstance) {
        SecurityContext securityContext = SecurityContextHystrixRequestVariable.getInstance().get();
        if (securityContext != null) {
            SecurityContextHolder.setContext(securityContext);
        }
    }

    /**
     * Clear the SecurityContext
     */
    @Override
    public <T> void onExecutionSuccess(HystrixInvokable<T> commandInstance) {
        SecurityContextHolder.clearContext();
    }

    /**
     * Clear the SecurityContext
     */
    @Override
    public <T> Exception onExecutionError(HystrixInvokable<T> commandInstance, Exception e) {
        SecurityContextHolder.clearContext();
        return e;
    }

}<commit_msg>securitycontext should not be cleared when it was not populated by web hook.<commit_after>package com.nextgearcapital.feign.oauth2.autoconfigure.hystrix;

import com.netflix.hystrix.HystrixInvokable;
import com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

/**
 * A HystrixCommandExecutionHook that makes the Spring SecurityContext available during the execution of HystrixCommands.
 * <p>
 * It extracts the SecurityContext from the SecurityContextHystrixRequestVariable and sets it on the SecurityContextHolder.
 */
public class SecurityContextRegistratorCommandHook extends HystrixCommandExecutionHook {

	private static final String SECURITY_CONTEXT_SET_BY_SECURITY_CONTEXT_REGISTRATOR_COMMAND_HOOK = "securityContextSetBySecurityContextRegistratorCommandHook";

	@Override
	public <T> void onExecutionStart(HystrixInvokable<T> commandInstance) {
		if (SecurityContextHolder.getContext() == null) {
			SecurityContext securityContext = SecurityContextHystrixRequestVariable.getInstance().get();
			if (securityContext != null) {
				SecurityContextHolder.setContext(securityContext);
				RequestContextHolder.getRequestAttributes()
						.setAttribute(SECURITY_CONTEXT_SET_BY_SECURITY_CONTEXT_REGISTRATOR_COMMAND_HOOK, true, RequestAttributes.SCOPE_REQUEST);
			}
		}
	}

	/**
	 * Clear the SecurityContext
	 */
	@Override
	public <T> void onExecutionSuccess(HystrixInvokable<T> commandInstance) {
		if (RequestContextHolder.getRequestAttributes()
				.getAttribute(SECURITY_CONTEXT_SET_BY_SECURITY_CONTEXT_REGISTRATOR_COMMAND_HOOK, RequestAttributes.SCOPE_REQUEST) != null) {
			SecurityContextHolder.clearContext();
		}
	}

	/**
	 * Clear the SecurityContext
	 */
	@Override
	public <T> Exception onExecutionError(HystrixInvokable<T> commandInstance, Exception e) {
		if (RequestContextHolder.getRequestAttributes()
				.getAttribute(SECURITY_CONTEXT_SET_BY_SECURITY_CONTEXT_REGISTRATOR_COMMAND_HOOK, RequestAttributes.SCOPE_REQUEST) != null) {
			SecurityContextHolder.clearContext();
		}
		return e;
	}

}
{'fraction_non_alphanumeric': 0.0763888888888889, 'fraction_numerical': 0.0005341880341880342, 'mean_word_length': 5.6875, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5569154', 'n_tokens_mistral': 1072, 'n_tokens_neox': 1035, 'n_words': 196}