Skip to content

Commit f8f1e9a

Browse files
committed
Use ConcurrentHashMap in InMemoryReactiveClientRegistrationRepository
Fixes gh-7299
1 parent bf78e43 commit f8f1e9a

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/registration/InMemoryReactiveClientRegistrationRepository.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,31 +15,43 @@
1515
*/
1616
package org.springframework.security.oauth2.client.registration;
1717

18+
import java.util.Arrays;
19+
import java.util.Collections;
1820
import java.util.Iterator;
1921
import java.util.List;
22+
import java.util.Map;
23+
import java.util.concurrent.ConcurrentHashMap;
2024

2125
import reactor.core.publisher.Mono;
2226

27+
import org.springframework.util.Assert;
28+
2329
/**
2430
* A Reactive {@link ClientRegistrationRepository} that stores {@link ClientRegistration}(s) in-memory.
2531
*
2632
* @author Rob Winch
33+
* @author Ebert Toribio
2734
* @since 5.1
2835
* @see ClientRegistrationRepository
2936
* @see ClientRegistration
3037
*/
3138
public final class InMemoryReactiveClientRegistrationRepository
3239
implements ReactiveClientRegistrationRepository, Iterable<ClientRegistration> {
3340

34-
private final InMemoryClientRegistrationRepository delegate;
41+
private final Map<String, ClientRegistration> clientIdToClientRegistration;
3542

3643
/**
3744
* Constructs an {@code InMemoryReactiveClientRegistrationRepository} using the provided parameters.
3845
*
3946
* @param registrations the client registration(s)
4047
*/
4148
public InMemoryReactiveClientRegistrationRepository(ClientRegistration... registrations) {
42-
this.delegate = new InMemoryClientRegistrationRepository(registrations);
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);
4355
}
4456

4557
/**
@@ -48,12 +60,12 @@ public InMemoryReactiveClientRegistrationRepository(ClientRegistration... regist
4860
* @param registrations the client registration(s)
4961
*/
5062
public InMemoryReactiveClientRegistrationRepository(List<ClientRegistration> registrations) {
51-
this.delegate = new InMemoryClientRegistrationRepository(registrations);
63+
this.clientIdToClientRegistration = toUnmodifiableConcurrentMap(registrations);
5264
}
5365

5466
@Override
5567
public Mono<ClientRegistration> findByRegistrationId(String registrationId) {
56-
return Mono.justOrEmpty(this.delegate.findByRegistrationId(registrationId));
68+
return Mono.justOrEmpty(this.clientIdToClientRegistration.get(registrationId));
5769
}
5870

5971
/**
@@ -63,6 +75,20 @@ public Mono<ClientRegistration> findByRegistrationId(String registrationId) {
6375
*/
6476
@Override
6577
public Iterator<ClientRegistration> iterator() {
66-
return delegate.iterator();
78+
return this.clientIdToClientRegistration.values().iterator();
79+
}
80+
81+
private static Map<String, ClientRegistration> toUnmodifiableConcurrentMap(List<ClientRegistration> registrations) {
82+
Assert.notEmpty(registrations, "registrations cannot be null or empty");
83+
ConcurrentHashMap<String, ClientRegistration> result = new ConcurrentHashMap<>();
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+
}
90+
result.put(registration.getRegistrationId(), registration);
91+
}
92+
return Collections.unmodifiableMap(result);
6793
}
6894
}

0 commit comments

Comments
 (0)