33
33
import org .springframework .jdbc .core .PreparedStatementSetter ;
34
34
import org .springframework .jdbc .core .RowMapper ;
35
35
import org .springframework .jdbc .core .SqlParameterValue ;
36
- import org .springframework .jdbc .support .lob .DefaultLobHandler ;
37
36
import org .springframework .jdbc .support .lob .LobCreator ;
38
37
import org .springframework .jdbc .support .lob .LobHandler ;
39
38
import org .springframework .security .web .webauthn .api .AuthenticatorTransport ;
62
61
*/
63
62
public final class JdbcUserCredentialRepository implements UserCredentialRepository {
64
63
65
- private RowMapper <CredentialRecord > credentialRecordRowMapper = new CredentialRecordRowMapper ();
64
+ private RowMapper <CredentialRecord > credentialRecordRowMapper = new CredentialRecordRowMapper (ResultSet :: getBytes );
66
65
67
66
private Function <CredentialRecord , List <SqlParameterValue >> credentialRecordParametersMapper = new CredentialRecordParametersMapper ();
68
67
69
- private LobHandler lobHandler = new DefaultLobHandler () ;
68
+ private SetBytes setBytes = PreparedStatement :: setBytes ;
70
69
71
70
private final JdbcOperations jdbcOperations ;
72
71
@@ -159,22 +158,16 @@ public void save(CredentialRecord record) {
159
158
160
159
private void insertCredentialRecord (CredentialRecord record ) {
161
160
List <SqlParameterValue > parameters = this .credentialRecordParametersMapper .apply (record );
162
- try (LobCreator lobCreator = this .lobHandler .getLobCreator ()) {
163
- PreparedStatementSetter pss = new LobCreatorArgumentPreparedStatementSetter (lobCreator ,
164
- parameters .toArray ());
165
- this .jdbcOperations .update (SAVE_CREDENTIAL_RECORD_SQL , pss );
166
- }
161
+ PreparedStatementSetter pss = new BlobArgumentPreparedStatementSetter (this .setBytes , parameters .toArray ());
162
+ this .jdbcOperations .update (SAVE_CREDENTIAL_RECORD_SQL , pss );
167
163
}
168
164
169
165
private int updateCredentialRecord (CredentialRecord record ) {
170
166
List <SqlParameterValue > parameters = this .credentialRecordParametersMapper .apply (record );
171
167
SqlParameterValue credentialId = parameters .remove (0 );
172
168
parameters .add (credentialId );
173
- try (LobCreator lobCreator = this .lobHandler .getLobCreator ()) {
174
- PreparedStatementSetter pss = new LobCreatorArgumentPreparedStatementSetter (lobCreator ,
175
- parameters .toArray ());
176
- return this .jdbcOperations .update (UPDATE_CREDENTIAL_RECORD_SQL , pss );
177
- }
169
+ PreparedStatementSetter pss = new BlobArgumentPreparedStatementSetter (this .setBytes , parameters .toArray ());
170
+ return this .jdbcOperations .update (UPDATE_CREDENTIAL_RECORD_SQL , pss );
178
171
}
179
172
180
173
@ Override
@@ -195,10 +188,18 @@ public List<CredentialRecord> findByUserId(Bytes userId) {
195
188
/**
196
189
* Sets a {@link LobHandler} for large binary fields and large text field parameters.
197
190
* @param lobHandler the lob handler
191
+ * @deprecated {@link LobHandler} is deprecated without replacement, as such this
192
+ * method will also be removed without replacement
198
193
*/
194
+ @ Deprecated (since = "6.5" , forRemoval = true )
199
195
public void setLobHandler (LobHandler lobHandler ) {
200
196
Assert .notNull (lobHandler , "lobHandler cannot be null" );
201
- this .lobHandler = lobHandler ;
197
+ this .setBytes = (ps , index , bytes ) -> {
198
+ try (LobCreator creator = lobHandler .getLobCreator ()) {
199
+ creator .setBlobAsBytes (ps , index , bytes );
200
+ }
201
+ };
202
+ this .credentialRecordRowMapper = new CredentialRecordRowMapper (lobHandler ::getBlobAsBytes );
202
203
}
203
204
204
205
private static class CredentialRecordParametersMapper
@@ -246,13 +247,25 @@ private Timestamp fromInstant(Instant instant) {
246
247
247
248
}
248
249
249
- private static final class LobCreatorArgumentPreparedStatementSetter extends ArgumentPreparedStatementSetter {
250
+ private interface SetBytes {
251
+
252
+ void setBytes (PreparedStatement ps , int index , byte [] bytes ) throws SQLException ;
253
+
254
+ }
255
+
256
+ private interface GetBytes {
250
257
251
- private final LobCreator lobCreator ;
258
+ byte [] getBytes ( ResultSet rs , String columnName ) throws SQLException ;
252
259
253
- private LobCreatorArgumentPreparedStatementSetter (LobCreator lobCreator , Object [] args ) {
260
+ }
261
+
262
+ private static final class BlobArgumentPreparedStatementSetter extends ArgumentPreparedStatementSetter {
263
+
264
+ private final SetBytes setBytes ;
265
+
266
+ private BlobArgumentPreparedStatementSetter (SetBytes setBytes , Object [] args ) {
254
267
super (args );
255
- this .lobCreator = lobCreator ;
268
+ this .setBytes = setBytes ;
256
269
}
257
270
258
271
@ Override
@@ -264,7 +277,7 @@ protected void doSetValue(PreparedStatement ps, int parameterPosition, Object ar
264
277
"Value of blob parameter must be byte[]" );
265
278
}
266
279
byte [] valueBytes = (byte []) paramValue .getValue ();
267
- this .lobCreator . setBlobAsBytes (ps , parameterPosition , valueBytes );
280
+ this .setBytes . setBytes (ps , parameterPosition , valueBytes );
268
281
return ;
269
282
}
270
283
}
@@ -275,14 +288,17 @@ protected void doSetValue(PreparedStatement ps, int parameterPosition, Object ar
275
288
276
289
private static class CredentialRecordRowMapper implements RowMapper <CredentialRecord > {
277
290
278
- private LobHandler lobHandler = new DefaultLobHandler ();
291
+ private final GetBytes getBytes ;
292
+
293
+ CredentialRecordRowMapper (GetBytes getBytes ) {
294
+ this .getBytes = getBytes ;
295
+ }
279
296
280
297
@ Override
281
298
public CredentialRecord mapRow (ResultSet rs , int rowNum ) throws SQLException {
282
299
Bytes credentialId = Bytes .fromBase64 (new String (rs .getString ("credential_id" ).getBytes ()));
283
300
Bytes userEntityUserId = Bytes .fromBase64 (new String (rs .getString ("user_entity_user_id" ).getBytes ()));
284
- ImmutablePublicKeyCose publicKey = new ImmutablePublicKeyCose (
285
- this .lobHandler .getBlobAsBytes (rs , "public_key" ));
301
+ ImmutablePublicKeyCose publicKey = new ImmutablePublicKeyCose (this .getBytes .getBytes (rs , "public_key" ));
286
302
long signatureCount = rs .getLong ("signature_count" );
287
303
boolean uvInitialized = rs .getBoolean ("uv_initialized" );
288
304
boolean backupEligible = rs .getBoolean ("backup_eligible" );
@@ -291,13 +307,13 @@ public CredentialRecord mapRow(ResultSet rs, int rowNum) throws SQLException {
291
307
boolean backupState = rs .getBoolean ("backup_state" );
292
308
293
309
Bytes attestationObject = null ;
294
- byte [] rawAttestationObject = this .lobHandler . getBlobAsBytes (rs , "attestation_object" );
310
+ byte [] rawAttestationObject = this .getBytes . getBytes (rs , "attestation_object" );
295
311
if (rawAttestationObject != null ) {
296
312
attestationObject = new Bytes (rawAttestationObject );
297
313
}
298
314
299
315
Bytes attestationClientDataJson = null ;
300
- byte [] rawAttestationClientDataJson = this .lobHandler . getBlobAsBytes (rs , "attestation_client_data_json" );
316
+ byte [] rawAttestationClientDataJson = this .getBytes . getBytes (rs , "attestation_client_data_json" );
301
317
if (rawAttestationClientDataJson != null ) {
302
318
attestationClientDataJson = new Bytes (rawAttestationClientDataJson );
303
319
}
0 commit comments