AuthorityAuthorizationManager->AuthorityReactiveAuthorizationManager

Issue gh-4615
This commit is contained in:
Rob Winch 2017-10-10 09:36:35 -05:00
parent d840090cb0
commit 866ce5eaec
4 changed files with 18 additions and 23 deletions

View File

@ -21,7 +21,7 @@ import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.ReactiveAuthenticationManager;
import org.springframework.security.authorization.AuthenticatedAuthorizationManager;
import org.springframework.security.authorization.AuthorityAuthorizationManager;
import org.springframework.security.authorization.AuthorityReactiveAuthorizationManager;
import org.springframework.security.authorization.AuthorizationDecision;
import org.springframework.security.authorization.ReactiveAuthorizationManager;
import org.springframework.security.web.server.AuthenticationEntryPoint;
@ -293,11 +293,11 @@ public class HttpSecurity {
}
public AuthorizeExchangeBuilder hasRole(String role) {
return access(AuthorityAuthorizationManager.hasRole(role));
return access(AuthorityReactiveAuthorizationManager.hasRole(role));
}
public AuthorizeExchangeBuilder hasAuthority(String authority) {
return access(AuthorityAuthorizationManager.hasAuthority(authority));
return access(AuthorityReactiveAuthorizationManager.hasAuthority(authority));
}
public AuthorizeExchangeBuilder authenticated() {

View File

@ -24,10 +24,10 @@ import reactor.core.publisher.Mono;
* @author Rob Winch
* @since 5.0
*/
public class AuthorityAuthorizationManager<T> implements ReactiveAuthorizationManager<T> {
public class AuthorityReactiveAuthorizationManager<T> implements ReactiveAuthorizationManager<T> {
private final String authority;
private AuthorityAuthorizationManager(String authority) {
private AuthorityReactiveAuthorizationManager(String authority) {
this.authority = authority;
}
@ -42,12 +42,12 @@ public class AuthorityAuthorizationManager<T> implements ReactiveAuthorizationMa
.defaultIfEmpty(new AuthorizationDecision(false));
}
public static <T> AuthorityAuthorizationManager<T> hasAuthority(String authority) {
public static <T> AuthorityReactiveAuthorizationManager<T> hasAuthority(String authority) {
Assert.notNull(authority, "authority cannot be null");
return new AuthorityAuthorizationManager<>(authority);
return new AuthorityReactiveAuthorizationManager<>(authority);
}
public static <T> AuthorityAuthorizationManager<T> hasRole(String role) {
public static <T> AuthorityReactiveAuthorizationManager<T> hasRole(String role) {
Assert.notNull(role, "role cannot be null");
return hasAuthority("ROLE_" + role);
}

View File

@ -22,28 +22,25 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import java.util.Collection;
import java.util.Collections;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
import static org.springframework.security.core.authority.AuthorityUtils.createAuthorityList;
/**
* @author Rob Winch
* @since 5.0
*/
@RunWith(MockitoJUnitRunner.class)
public class AuthorityAuthorizationManagerTests {
public class AuthorityReactiveAuthorizationManagerTests {
@Mock
Authentication authentication;
AuthorityAuthorizationManager<Object> manager = AuthorityAuthorizationManager.hasAuthority("ADMIN");
AuthorityReactiveAuthorizationManager<Object> manager = AuthorityReactiveAuthorizationManager
.hasAuthority("ADMIN");
@Test
public void checkWhenHasAuthorityAndNotAuthenticatedThenReturnFalse() {
@ -99,7 +96,7 @@ public class AuthorityAuthorizationManagerTests {
@Test
public void checkWhenHasRoleAndAuthorizedThenReturnTrue() {
manager = AuthorityAuthorizationManager.hasRole("ADMIN");
manager = AuthorityReactiveAuthorizationManager.hasRole("ADMIN");
authentication = new TestingAuthenticationToken("rob", "secret", "ROLE_ADMIN");
boolean granted = manager.check(Mono.just(authentication), null).block().isGranted();
@ -109,7 +106,7 @@ public class AuthorityAuthorizationManagerTests {
@Test
public void checkWhenHasRoleAndNotAuthorizedThenReturnTrue() {
manager = AuthorityAuthorizationManager.hasRole("ADMIN");
manager = AuthorityReactiveAuthorizationManager.hasRole("ADMIN");
authentication = new TestingAuthenticationToken("rob", "secret", "ADMIN");
boolean granted = manager.check(Mono.just(authentication), null).block().isGranted();
@ -120,12 +117,12 @@ public class AuthorityAuthorizationManagerTests {
@Test(expected = IllegalArgumentException.class)
public void hasRoleWhenNullThenException() {
String role = null;
AuthorityAuthorizationManager.hasRole(role);
AuthorityReactiveAuthorizationManager.hasRole(role);
}
@Test(expected = IllegalArgumentException.class)
public void hasAuthorityWhenNullThenException() {
String authority = null;
AuthorityAuthorizationManager.hasAuthority(authority);
AuthorityReactiveAuthorizationManager.hasAuthority(authority);
}
}

View File

@ -21,7 +21,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.security.authorization.AuthorityAuthorizationManager;
import org.springframework.security.authorization.AuthorityReactiveAuthorizationManager;
import org.springframework.security.authorization.AuthorizationDecision;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher;
@ -45,10 +45,8 @@ public class DelegatingReactiveAuthorizationManagerTests {
ServerWebExchangeMatcher match1;
@Mock
ServerWebExchangeMatcher match2;
@Mock
AuthorityAuthorizationManager<AuthorizationContext> delegate1;
@Mock
AuthorityAuthorizationManager<AuthorizationContext> delegate2;
@Mock AuthorityReactiveAuthorizationManager<AuthorizationContext> delegate1;
@Mock AuthorityReactiveAuthorizationManager<AuthorizationContext> delegate2;
@Mock
ServerWebExchange exchange;
@Mock