Skip to content

Add Anonymous Support to AuthenticatedReactiveAuthorizationManager #6267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.security.authorization;

import org.springframework.security.authentication.AuthenticationTrustResolver;
import org.springframework.security.authentication.AuthenticationTrustResolverImpl;
import org.springframework.security.core.Authentication;
import reactor.core.publisher.Mono;

Expand All @@ -30,13 +32,25 @@
*/
public class AuthenticatedReactiveAuthorizationManager<T> implements ReactiveAuthorizationManager<T> {

private AuthenticationTrustResolver authTrustResolver = new AuthenticationTrustResolverImpl();

@Override
public Mono<AuthorizationDecision> check(Mono<Authentication> authentication, T object) {
return authentication
.filter(this::isNotAnonymous)
.map(a -> new AuthorizationDecision(a.isAuthenticated()))
.defaultIfEmpty(new AuthorizationDecision(false));
}

/**
* Verify (via {@link AuthenticationTrustResolver}) that the given authentication is not anonymous.
* @param authentication to be checked
* @return <code>true</code> if not anonymous, otherwise <code>false</code>.
*/
private boolean isNotAnonymous(Authentication authentication) {
return !authTrustResolver.isAnonymous(authentication);
}

/**
* Gets an instance of {@link AuthenticatedReactiveAuthorizationManager}
* @param <T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
Expand Down Expand Up @@ -62,6 +64,14 @@ public void checkWhenEmptyThenReturnFalse() {
assertThat(granted).isFalse();
}

@Test
public void checkWhenAnonymousAuthenticatedThenReturnFalse() {
AnonymousAuthenticationToken anonymousAuthenticationToken = mock(AnonymousAuthenticationToken.class);

boolean granted = manager.check(Mono.just(anonymousAuthenticationToken), null).block().isGranted();

assertThat(granted).isFalse();
}

@Test
public void checkWhenErrorThenError() {
Expand Down