1
1
/*
2
- * Copyright 2002-2017 the original author or authors.
2
+ * Copyright 2002-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
20
20
import org .springframework .util .Assert ;
21
21
import reactor .core .publisher .Mono ;
22
22
23
+ import java .util .Arrays ;
24
+ import java .util .List ;
25
+ import java .util .stream .Stream ;
26
+
23
27
/**
24
28
* A {@link ReactiveAuthorizationManager} that determines if the current user is
25
29
* authorized by evaluating if the {@link Authentication} contains a specified authority.
29
33
* @param <T> the type of object being authorized
30
34
*/
31
35
public class AuthorityReactiveAuthorizationManager <T > implements ReactiveAuthorizationManager <T > {
32
- private final String authority ;
36
+ private final List < String > authorities ;
33
37
34
- private AuthorityReactiveAuthorizationManager (String authority ) {
35
- this .authority = authority ;
38
+ private AuthorityReactiveAuthorizationManager (String ... authorities ) {
39
+ this .authorities = Arrays . asList ( authorities ) ;
36
40
}
37
41
38
42
@ Override
39
43
public Mono <AuthorizationDecision > check (Mono <Authentication > authentication , T object ) {
40
44
return authentication
41
45
.filter (a -> a .isAuthenticated ())
42
46
.flatMapIterable ( a -> a .getAuthorities ())
43
- .map ( g -> g .getAuthority ())
44
- .hasElement ( this .authority )
47
+ .map (g -> g .getAuthority ())
48
+ .any ( a -> this .authorities . contains ( a ) )
45
49
.map ( hasAuthority -> new AuthorizationDecision (hasAuthority ))
46
50
.defaultIfEmpty (new AuthorizationDecision (false ));
47
51
}
@@ -59,6 +63,24 @@ public static <T> AuthorityReactiveAuthorizationManager<T> hasAuthority(String a
59
63
return new AuthorityReactiveAuthorizationManager <>(authority );
60
64
}
61
65
66
+ /**
67
+ * Creates an instance of {@link AuthorityReactiveAuthorizationManager} with the
68
+ * provided authorities.
69
+ *
70
+ * @author Robbie Martinus
71
+ * @param authorities the authorities to check for
72
+ * @param <T> the type of object being authorized
73
+ * @return the new instance
74
+ */
75
+ public static <T > AuthorityReactiveAuthorizationManager <T > hasAnyAuthority (String ... authorities ) {
76
+ Assert .notNull (authorities , "authorities cannot be null" );
77
+ for (String authority : authorities ) {
78
+ Assert .notNull (authority , "authority cannot be null" );
79
+ }
80
+
81
+ return new AuthorityReactiveAuthorizationManager <>(authorities );
82
+ }
83
+
62
84
/**
63
85
* Creates an instance of {@link AuthorityReactiveAuthorizationManager} with the
64
86
* provided authority.
@@ -71,4 +93,25 @@ public static <T> AuthorityReactiveAuthorizationManager<T> hasRole(String role)
71
93
Assert .notNull (role , "role cannot be null" );
72
94
return hasAuthority ("ROLE_" + role );
73
95
}
96
+
97
+ /**
98
+ * Creates an instance of {@link AuthorityReactiveAuthorizationManager} with the
99
+ * provided authorities.
100
+ *
101
+ * @author Robbie Martinus
102
+ * @param roles the authorities to check for prefixed with "ROLE_"
103
+ * @param <T> the type of object being authorized
104
+ * @return the new instance
105
+ */
106
+ public static <T > AuthorityReactiveAuthorizationManager <T > hasAnyRole (String ... roles ) {
107
+ Assert .notNull (roles , "roles cannot be null" );
108
+ for (String role : roles ) {
109
+ Assert .notNull (role , "role cannot be null" );
110
+ }
111
+
112
+ return hasAnyAuthority (Stream .of (roles )
113
+ .map (r -> "ROLE_" + r )
114
+ .toArray (String []::new )
115
+ );
116
+ }
74
117
}
0 commit comments