diff --git a/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/JdbcOAuth2AuthorizedClientService.java b/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/JdbcOAuth2AuthorizedClientService.java index 19e373fe725..291158068e8 100644 --- a/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/JdbcOAuth2AuthorizedClientService.java +++ b/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/JdbcOAuth2AuthorizedClientService.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,7 +30,6 @@ import java.util.function.Function; import org.springframework.dao.DataRetrievalFailureException; -import org.springframework.dao.DuplicateKeyException; import org.springframework.jdbc.core.ArgumentPreparedStatementSetter; import org.springframework.jdbc.core.JdbcOperations; import org.springframework.jdbc.core.PreparedStatementSetter; @@ -166,22 +165,13 @@ public T loadAuthorizedClient(String clientRe public void saveAuthorizedClient(OAuth2AuthorizedClient authorizedClient, Authentication principal) { Assert.notNull(authorizedClient, "authorizedClient cannot be null"); Assert.notNull(principal, "principal cannot be null"); - boolean existsAuthorizedClient = null != this - .loadAuthorizedClient(authorizedClient.getClientRegistration().getRegistrationId(), principal.getName()); - if (existsAuthorizedClient) { - updateAuthorizedClient(authorizedClient, principal); - } - else { - try { - insertAuthorizedClient(authorizedClient, principal); - } - catch (DuplicateKeyException ex) { - updateAuthorizedClient(authorizedClient, principal); - } + int rows = updateAuthorizedClient(authorizedClient, principal); + if (rows == 0) { + insertAuthorizedClient(authorizedClient, principal); } } - private void updateAuthorizedClient(OAuth2AuthorizedClient authorizedClient, Authentication principal) { + private int updateAuthorizedClient(OAuth2AuthorizedClient authorizedClient, Authentication principal) { List parameters = this.authorizedClientParametersMapper .apply(new OAuth2AuthorizedClientHolder(authorizedClient, principal)); SqlParameterValue clientRegistrationIdParameter = parameters.remove(0); @@ -191,7 +181,7 @@ private void updateAuthorizedClient(OAuth2AuthorizedClient authorizedClient, Aut try (LobCreator lobCreator = this.lobHandler.getLobCreator()) { PreparedStatementSetter pss = new LobCreatorArgumentPreparedStatementSetter(lobCreator, parameters.toArray()); - this.jdbcOperations.update(UPDATE_AUTHORIZED_CLIENT_SQL, pss); + return this.jdbcOperations.update(UPDATE_AUTHORIZED_CLIENT_SQL, pss); } } diff --git a/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/JdbcOAuth2AuthorizedClientServiceTests.java b/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/JdbcOAuth2AuthorizedClientServiceTests.java index 20deeb03c84..6320b0c5368 100644 --- a/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/JdbcOAuth2AuthorizedClientServiceTests.java +++ b/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/JdbcOAuth2AuthorizedClientServiceTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -60,6 +60,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; @@ -306,7 +307,7 @@ public void saveLoadAuthorizedClientWhenCustomStrategiesSetThenCalled() throws E this.authorizedClientService.loadAuthorizedClient(this.clientRegistration.getRegistrationId(), principal.getName()); verify(authorizedClientRowMapper).mapRow(any(), anyInt()); - verify(authorizedClientParametersMapper).apply(any()); + verify(authorizedClientParametersMapper, atLeastOnce()).apply(any()); } @Test diff --git a/web/src/main/java/org/springframework/security/web/webauthn/management/JdbcPublicKeyCredentialUserEntityRepository.java b/web/src/main/java/org/springframework/security/web/webauthn/management/JdbcPublicKeyCredentialUserEntityRepository.java index bfeaafb0e87..f4eb985cfd4 100644 --- a/web/src/main/java/org/springframework/security/web/webauthn/management/JdbcPublicKeyCredentialUserEntityRepository.java +++ b/web/src/main/java/org/springframework/security/web/webauthn/management/JdbcPublicKeyCredentialUserEntityRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,6 @@ import java.util.List; import java.util.function.Function; -import org.springframework.dao.DuplicateKeyException; import org.springframework.jdbc.core.ArgumentPreparedStatementSetter; import org.springframework.jdbc.core.JdbcOperations; import org.springframework.jdbc.core.PreparedStatementSetter; @@ -124,17 +123,9 @@ public PublicKeyCredentialUserEntity findByUsername(String username) { @Override public void save(PublicKeyCredentialUserEntity userEntity) { Assert.notNull(userEntity, "userEntity cannot be null"); - boolean existsUserEntity = null != this.findById(userEntity.getId()); - if (existsUserEntity) { - updateUserEntity(userEntity); - } - else { - try { - insertUserEntity(userEntity); - } - catch (DuplicateKeyException ex) { - updateUserEntity(userEntity); - } + int rows = updateUserEntity(userEntity); + if (rows == 0) { + insertUserEntity(userEntity); } } @@ -144,12 +135,12 @@ private void insertUserEntity(PublicKeyCredentialUserEntity userEntity) { this.jdbcOperations.update(SAVE_USER_SQL, pss); } - private void updateUserEntity(PublicKeyCredentialUserEntity userEntity) { + private int updateUserEntity(PublicKeyCredentialUserEntity userEntity) { List parameters = this.userEntityParametersMapper.apply(userEntity); SqlParameterValue userEntityId = parameters.remove(0); parameters.add(userEntityId); PreparedStatementSetter pss = new ArgumentPreparedStatementSetter(parameters.toArray()); - this.jdbcOperations.update(UPDATE_USER_SQL, pss); + return this.jdbcOperations.update(UPDATE_USER_SQL, pss); } @Override