Skip to content

Commit 0d31e31

Browse files
Improve JdbcPublicKeyCredentialUserEntityRepository save
Closes gh-16726 Signed-off-by: Max Batischev <[email protected]>
1 parent 3f4ff1c commit 0d31e31

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

web/src/main/java/org/springframework/security/web/webauthn/management/JdbcPublicKeyCredentialUserEntityRepository.java

+6-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 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.
@@ -23,7 +23,6 @@
2323
import java.util.List;
2424
import java.util.function.Function;
2525

26-
import org.springframework.dao.DuplicateKeyException;
2726
import org.springframework.jdbc.core.ArgumentPreparedStatementSetter;
2827
import org.springframework.jdbc.core.JdbcOperations;
2928
import org.springframework.jdbc.core.PreparedStatementSetter;
@@ -124,17 +123,9 @@ public PublicKeyCredentialUserEntity findByUsername(String username) {
124123
@Override
125124
public void save(PublicKeyCredentialUserEntity userEntity) {
126125
Assert.notNull(userEntity, "userEntity cannot be null");
127-
boolean existsUserEntity = null != this.findById(userEntity.getId());
128-
if (existsUserEntity) {
129-
updateUserEntity(userEntity);
130-
}
131-
else {
132-
try {
133-
insertUserEntity(userEntity);
134-
}
135-
catch (DuplicateKeyException ex) {
136-
updateUserEntity(userEntity);
137-
}
126+
int rows = updateUserEntity(userEntity);
127+
if (rows == 0) {
128+
insertUserEntity(userEntity);
138129
}
139130
}
140131

@@ -144,12 +135,12 @@ private void insertUserEntity(PublicKeyCredentialUserEntity userEntity) {
144135
this.jdbcOperations.update(SAVE_USER_SQL, pss);
145136
}
146137

147-
private void updateUserEntity(PublicKeyCredentialUserEntity userEntity) {
138+
private int updateUserEntity(PublicKeyCredentialUserEntity userEntity) {
148139
List<SqlParameterValue> parameters = this.userEntityParametersMapper.apply(userEntity);
149140
SqlParameterValue userEntityId = parameters.remove(0);
150141
parameters.add(userEntityId);
151142
PreparedStatementSetter pss = new ArgumentPreparedStatementSetter(parameters.toArray());
152-
this.jdbcOperations.update(UPDATE_USER_SQL, pss);
143+
return this.jdbcOperations.update(UPDATE_USER_SQL, pss);
153144
}
154145

155146
@Override

0 commit comments

Comments
 (0)