15
15
*/
16
16
package org .springframework .security .oauth2 .client .registration ;
17
17
18
+ import java .util .Arrays ;
19
+ import java .util .Collections ;
18
20
import java .util .Iterator ;
19
21
import java .util .List ;
20
22
import java .util .Map ;
21
23
import java .util .concurrent .ConcurrentHashMap ;
22
- import java .util .concurrent .ConcurrentHashMap ;
23
-
24
- import org .springframework .util .Assert ;
25
24
26
25
import reactor .core .publisher .Mono ;
27
26
27
+ import org .springframework .util .Assert ;
28
+
28
29
/**
29
30
* A Reactive {@link ClientRegistrationRepository} that stores {@link ClientRegistration}(s) in-memory.
30
31
*
@@ -45,12 +46,12 @@ public final class InMemoryReactiveClientRegistrationRepository
45
46
* @param registrations the client registration(s)
46
47
*/
47
48
public InMemoryReactiveClientRegistrationRepository (ClientRegistration ... registrations ) {
48
- Assert . notEmpty ( registrations , "registrations cannot be empty" );
49
- this . clientIdToClientRegistration = new ConcurrentHashMap <>();
50
- for ( ClientRegistration registration : registrations ) {
51
- Assert . notNull ( registration , "registrations cannot contain null values" );
52
- this . clientIdToClientRegistration . put ( registration . getRegistrationId (), registration );
53
- }
49
+ this ( toList ( registrations ) );
50
+ }
51
+
52
+ private static List < ClientRegistration > toList ( ClientRegistration ... registrations ) {
53
+ Assert . notEmpty ( registrations , "registrations cannot be null or empty" );
54
+ return Arrays . asList ( registrations );
54
55
}
55
56
56
57
/**
@@ -59,8 +60,7 @@ public InMemoryReactiveClientRegistrationRepository(ClientRegistration... regist
59
60
* @param registrations the client registration(s)
60
61
*/
61
62
public InMemoryReactiveClientRegistrationRepository (List <ClientRegistration > registrations ) {
62
- Assert .notEmpty (registrations , "registrations cannot be null or empty" );
63
- this .clientIdToClientRegistration = toConcurrentMap (registrations );
63
+ this .clientIdToClientRegistration = toUnmodifiableConcurrentMap (registrations );
64
64
}
65
65
66
66
@ Override
@@ -78,11 +78,17 @@ public Iterator<ClientRegistration> iterator() {
78
78
return this .clientIdToClientRegistration .values ().iterator ();
79
79
}
80
80
81
- private ConcurrentHashMap <String , ClientRegistration > toConcurrentMap (List <ClientRegistration > registrations ) {
81
+ private static Map <String , ClientRegistration > toUnmodifiableConcurrentMap (List <ClientRegistration > registrations ) {
82
+ Assert .notEmpty (registrations , "registrations cannot be null or empty" );
82
83
ConcurrentHashMap <String , ClientRegistration > result = new ConcurrentHashMap <>();
83
84
for (ClientRegistration registration : registrations ) {
85
+ Assert .notNull (registration , "no registration can be null" );
86
+ if (result .containsKey (registration .getRegistrationId ())) {
87
+ throw new IllegalStateException (String .format ("Duplicate key %s" ,
88
+ registration .getRegistrationId ()));
89
+ }
84
90
result .put (registration .getRegistrationId (), registration );
85
91
}
86
- return result ;
92
+ return Collections . unmodifiableMap ( result ) ;
87
93
}
88
94
}
0 commit comments