File tree Expand file tree Collapse file tree 2 files changed +24
-0
lines changed
main/java/org/springframework/security/authorization
test/java/org/springframework/security/authorization Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change 1616
1717package org .springframework .security .authorization ;
1818
19+ import org .springframework .security .authentication .AuthenticationTrustResolver ;
20+ import org .springframework .security .authentication .AuthenticationTrustResolverImpl ;
1921import org .springframework .security .core .Authentication ;
2022import reactor .core .publisher .Mono ;
2123
3032 */
3133public class AuthenticatedReactiveAuthorizationManager <T > implements ReactiveAuthorizationManager <T > {
3234
35+ private AuthenticationTrustResolver authTrustResolver = new AuthenticationTrustResolverImpl ();
36+
3337 @ Override
3438 public Mono <AuthorizationDecision > check (Mono <Authentication > authentication , T object ) {
3539 return authentication
40+ .filter (this ::isNotAnonymous )
3641 .map (a -> new AuthorizationDecision (a .isAuthenticated ()))
3742 .defaultIfEmpty (new AuthorizationDecision (false ));
3843 }
3944
45+ /**
46+ * Verify (via {@link AuthenticationTrustResolver}) that the given authentication is not anonymous.
47+ * @param authentication to be checked
48+ * @return <code>true</code> if not anonymous, otherwise <code>false</code>.
49+ */
50+ private boolean isNotAnonymous (Authentication authentication ) {
51+ return !authTrustResolver .isAnonymous (authentication );
52+ }
53+
4054 /**
4155 * Gets an instance of {@link AuthenticatedReactiveAuthorizationManager}
4256 * @param <T>
Original file line number Diff line number Diff line change 2020import org .junit .runner .RunWith ;
2121import org .mockito .Mock ;
2222import org .mockito .junit .MockitoJUnitRunner ;
23+ import org .springframework .security .authentication .AnonymousAuthenticationToken ;
2324import org .springframework .security .core .Authentication ;
2425import reactor .core .publisher .Mono ;
2526import reactor .test .StepVerifier ;
2627
2728import static org .assertj .core .api .Assertions .assertThat ;
29+ import static org .mockito .Mockito .mock ;
2830import static org .mockito .Mockito .when ;
2931
3032/**
@@ -62,6 +64,14 @@ public void checkWhenEmptyThenReturnFalse() {
6264 assertThat (granted ).isFalse ();
6365 }
6466
67+ @ Test
68+ public void checkWhenAnonymousAuthenticatedThenReturnFalse () {
69+ AnonymousAuthenticationToken anonymousAuthenticationToken = mock (AnonymousAuthenticationToken .class );
70+
71+ boolean granted = manager .check (Mono .just (anonymousAuthenticationToken ), null ).block ().isGranted ();
72+
73+ assertThat (granted ).isFalse ();
74+ }
6575
6676 @ Test
6777 public void checkWhenErrorThenError () {
You can’t perform that action at this time.
0 commit comments