Skip to content

Commit ba8eda3

Browse files
franticticktickjzheaux
authored andcommitted
Improve JdbcOAuth2AuthorizedClientService saveAuthorizedClient
Closes gh-16726 Signed-off-by: Max Batischev <[email protected]>
1 parent 1120733 commit ba8eda3

File tree

2 files changed

+7
-18
lines changed

2 files changed

+7
-18
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/JdbcOAuth2AuthorizedClientService.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -30,7 +30,6 @@
3030
import java.util.function.Function;
3131

3232
import org.springframework.dao.DataRetrievalFailureException;
33-
import org.springframework.dao.DuplicateKeyException;
3433
import org.springframework.jdbc.core.ArgumentPreparedStatementSetter;
3534
import org.springframework.jdbc.core.JdbcOperations;
3635
import org.springframework.jdbc.core.PreparedStatementSetter;
@@ -166,22 +165,13 @@ public <T extends OAuth2AuthorizedClient> T loadAuthorizedClient(String clientRe
166165
public void saveAuthorizedClient(OAuth2AuthorizedClient authorizedClient, Authentication principal) {
167166
Assert.notNull(authorizedClient, "authorizedClient cannot be null");
168167
Assert.notNull(principal, "principal cannot be null");
169-
boolean existsAuthorizedClient = null != this
170-
.loadAuthorizedClient(authorizedClient.getClientRegistration().getRegistrationId(), principal.getName());
171-
if (existsAuthorizedClient) {
172-
updateAuthorizedClient(authorizedClient, principal);
173-
}
174-
else {
175-
try {
176-
insertAuthorizedClient(authorizedClient, principal);
177-
}
178-
catch (DuplicateKeyException ex) {
179-
updateAuthorizedClient(authorizedClient, principal);
180-
}
168+
int rows = updateAuthorizedClient(authorizedClient, principal);
169+
if (rows == 0) {
170+
insertAuthorizedClient(authorizedClient, principal);
181171
}
182172
}
183173

184-
private void updateAuthorizedClient(OAuth2AuthorizedClient authorizedClient, Authentication principal) {
174+
private int updateAuthorizedClient(OAuth2AuthorizedClient authorizedClient, Authentication principal) {
185175
List<SqlParameterValue> parameters = this.authorizedClientParametersMapper
186176
.apply(new OAuth2AuthorizedClientHolder(authorizedClient, principal));
187177
SqlParameterValue clientRegistrationIdParameter = parameters.remove(0);
@@ -191,7 +181,7 @@ private void updateAuthorizedClient(OAuth2AuthorizedClient authorizedClient, Aut
191181
try (LobCreator lobCreator = this.lobHandler.getLobCreator()) {
192182
PreparedStatementSetter pss = new LobCreatorArgumentPreparedStatementSetter(lobCreator,
193183
parameters.toArray());
194-
this.jdbcOperations.update(UPDATE_AUTHORIZED_CLIENT_SQL, pss);
184+
return this.jdbcOperations.update(UPDATE_AUTHORIZED_CLIENT_SQL, pss);
195185
}
196186
}
197187

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/JdbcOAuth2AuthorizedClientServiceTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -306,7 +306,6 @@ public void saveLoadAuthorizedClientWhenCustomStrategiesSetThenCalled() throws E
306306
this.authorizedClientService.loadAuthorizedClient(this.clientRegistration.getRegistrationId(),
307307
principal.getName());
308308
verify(authorizedClientRowMapper).mapRow(any(), anyInt());
309-
verify(authorizedClientParametersMapper).apply(any());
310309
}
311310

312311
@Test

0 commit comments

Comments
 (0)