File tree 2 files changed +24
-0
lines changed
main/java/org/springframework/security/authorization
test/java/org/springframework/security/authorization
2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change 16
16
17
17
package org .springframework .security .authorization ;
18
18
19
+ import org .springframework .security .authentication .AuthenticationTrustResolver ;
20
+ import org .springframework .security .authentication .AuthenticationTrustResolverImpl ;
19
21
import org .springframework .security .core .Authentication ;
20
22
import reactor .core .publisher .Mono ;
21
23
30
32
*/
31
33
public class AuthenticatedReactiveAuthorizationManager <T > implements ReactiveAuthorizationManager <T > {
32
34
35
+ private AuthenticationTrustResolver authTrustResolver = new AuthenticationTrustResolverImpl ();
36
+
33
37
@ Override
34
38
public Mono <AuthorizationDecision > check (Mono <Authentication > authentication , T object ) {
35
39
return authentication
40
+ .filter (this ::isNotAnonymous )
36
41
.map (a -> new AuthorizationDecision (a .isAuthenticated ()))
37
42
.defaultIfEmpty (new AuthorizationDecision (false ));
38
43
}
39
44
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
+
40
54
/**
41
55
* Gets an instance of {@link AuthenticatedReactiveAuthorizationManager}
42
56
* @param <T>
Original file line number Diff line number Diff line change 20
20
import org .junit .runner .RunWith ;
21
21
import org .mockito .Mock ;
22
22
import org .mockito .junit .MockitoJUnitRunner ;
23
+ import org .springframework .security .authentication .AnonymousAuthenticationToken ;
23
24
import org .springframework .security .core .Authentication ;
24
25
import reactor .core .publisher .Mono ;
25
26
import reactor .test .StepVerifier ;
26
27
27
28
import static org .assertj .core .api .Assertions .assertThat ;
29
+ import static org .mockito .Mockito .mock ;
28
30
import static org .mockito .Mockito .when ;
29
31
30
32
/**
@@ -62,6 +64,14 @@ public void checkWhenEmptyThenReturnFalse() {
62
64
assertThat (granted ).isFalse ();
63
65
}
64
66
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
+ }
65
75
66
76
@ Test
67
77
public void checkWhenErrorThenError () {
You can’t perform that action at this time.
0 commit comments