All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.security.core.context.ReactiveSecurityContextHolder Maven / Gradle / Ivy

There is a newer version: 6.2.4
Show newest version
/*
 * Copyright 2002-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.security.core.context;

import java.util.function.Function;

import reactor.core.publisher.Mono;
import reactor.util.context.Context;

import org.springframework.security.core.Authentication;

/**
 * Allows getting and setting the Spring {@link SecurityContext} into a {@link Context}.
 *
 * @author Rob Winch
 * @since 5.0
 */
public final class ReactiveSecurityContextHolder {

	private static final Class SECURITY_CONTEXT_KEY = SecurityContext.class;

	private ReactiveSecurityContextHolder() {
	}

	/**
	 * Gets the {@code Mono} from Reactor {@link Context}
	 * @return the {@code Mono}
	 */
	public static Mono getContext() {
		// @formatter:off
		return Mono.subscriberContext()
				.filter(ReactiveSecurityContextHolder::hasSecurityContext)
				.flatMap(ReactiveSecurityContextHolder::getSecurityContext);
		// @formatter:on
	}

	private static boolean hasSecurityContext(Context context) {
		return context.hasKey(SECURITY_CONTEXT_KEY);
	}

	private static Mono getSecurityContext(Context context) {
		return context.>get(SECURITY_CONTEXT_KEY);
	}

	/**
	 * Clears the {@code Mono} from Reactor {@link Context}
	 * @return Return a {@code Mono} which only replays complete and error signals
	 * from clearing the context.
	 */
	public static Function clearContext() {
		return (context) -> context.delete(SECURITY_CONTEXT_KEY);
	}

	/**
	 * Creates a Reactor {@link Context} that contains the {@code Mono}
	 * that can be merged into another {@link Context}
	 * @param securityContext the {@code Mono} to set in the returned
	 * Reactor {@link Context}
	 * @return a Reactor {@link Context} that contains the {@code Mono}
	 */
	public static Context withSecurityContext(Mono securityContext) {
		return Context.of(SECURITY_CONTEXT_KEY, securityContext);
	}

	/**
	 * A shortcut for {@link #withSecurityContext(Mono)}
	 * @param authentication the {@link Authentication} to be used
	 * @return a Reactor {@link Context} that contains the {@code Mono}
	 */
	public static Context withAuthentication(Authentication authentication) {
		return withSecurityContext(Mono.just(new SecurityContextImpl(authentication)));
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy