Skip to content

Commit 216680b

Browse files
committed
Update Deprecated Spring Jdbc Usage
1 parent 2ad859a commit 216680b

File tree

6 files changed

+55
-43
lines changed

6 files changed

+55
-43
lines changed

acl/src/main/java/org/springframework/security/acls/jdbc/JdbcAclService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ public JdbcAclService(JdbcOperations jdbcOperations, LookupStrategy lookupStrate
100100
@Override
101101
public List<ObjectIdentity> findChildren(ObjectIdentity parentIdentity) {
102102
Object[] args = { parentIdentity.getIdentifier().toString(), parentIdentity.getType() };
103-
List<ObjectIdentity> objects = this.jdbcOperations.query(this.findChildrenSql, args,
104-
(rs, rowNum) -> mapObjectIdentityRow(rs));
103+
List<ObjectIdentity> objects = this.jdbcOperations.query(this.findChildrenSql,
104+
(rs, rowNum) -> mapObjectIdentityRow(rs), args);
105105
return (!objects.isEmpty()) ? objects : null;
106106
}
107107

acl/src/main/java/org/springframework/security/acls/jdbc/JdbcMutableAclService.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,7 @@ protected void createObjectIdentity(ObjectIdentity object, Sid owner) {
190190
* @return the primary key or null if not found
191191
*/
192192
protected Long createOrRetrieveClassPrimaryKey(String type, boolean allowCreate, Class idType) {
193-
List<Long> classIds = this.jdbcOperations.queryForList(this.selectClassPrimaryKey, new Object[] { type },
194-
Long.class);
193+
List<Long> classIds = this.jdbcOperations.queryForList(this.selectClassPrimaryKey, Long.class, type);
195194

196195
if (!classIds.isEmpty()) {
197196
return classIds.get(0);
@@ -242,8 +241,8 @@ protected Long createOrRetrieveSidPrimaryKey(Sid sid, boolean allowCreate) {
242241
* @return the primary key or null if not found
243242
*/
244243
protected Long createOrRetrieveSidPrimaryKey(String sidName, boolean sidIsPrincipal, boolean allowCreate) {
245-
List<Long> sidIds = this.jdbcOperations.queryForList(this.selectSidPrimaryKey,
246-
new Object[] { sidIsPrincipal, sidName }, Long.class);
244+
List<Long> sidIds = this.jdbcOperations.queryForList(this.selectSidPrimaryKey, Long.class, sidIsPrincipal,
245+
sidName);
247246
if (!sidIds.isEmpty()) {
248247
return sidIds.get(0);
249248
}

acl/src/test/java/org/springframework/security/acls/jdbc/JdbcAclServiceTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public void findOneChildren() {
109109
List<ObjectIdentity> result = new ArrayList<>();
110110
result.add(new ObjectIdentityImpl(Object.class, "5577"));
111111
Object[] args = { "1", "org.springframework.security.acls.jdbc.JdbcAclServiceTests$MockLongIdDomainObject" };
112-
given(this.jdbcOperations.query(anyString(), eq(args), any(RowMapper.class))).willReturn(result);
112+
given(this.jdbcOperations.query(anyString(), any(RowMapper.class), eq(args))).willReturn(result);
113113
ObjectIdentity objectIdentity = new ObjectIdentityImpl(MockLongIdDomainObject.class, 1L);
114114
List<ObjectIdentity> objectIdentities = this.aclService.findChildren(objectIdentity);
115115
assertThat(objectIdentities).hasSize(1);

core/src/main/java/org/springframework/security/core/userdetails/jdbc/JdbcDaoImpl.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,10 @@ protected List<UserDetails> loadUsersByUsername(String username) {
226226
* @return a list of GrantedAuthority objects for the user
227227
*/
228228
protected List<GrantedAuthority> loadUserAuthorities(String username) {
229-
return getJdbcTemplate().query(this.authoritiesByUsernameQuery, new String[] { username }, (rs, rowNum) -> {
229+
return getJdbcTemplate().query(this.authoritiesByUsernameQuery, (rs, rowNum) -> {
230230
String roleName = JdbcDaoImpl.this.rolePrefix + rs.getString(2);
231231
return new SimpleGrantedAuthority(roleName);
232-
});
232+
}, username);
233233
}
234234

235235
/**
@@ -238,11 +238,10 @@ protected List<GrantedAuthority> loadUserAuthorities(String username) {
238238
* @return a list of GrantedAuthority objects for the user
239239
*/
240240
protected List<GrantedAuthority> loadGroupAuthorities(String username) {
241-
return getJdbcTemplate().query(this.groupAuthoritiesByUsernameQuery, new String[] { username },
242-
(rs, rowNum) -> {
243-
String roleName = getRolePrefix() + rs.getString(3);
244-
return new SimpleGrantedAuthority(roleName);
245-
});
241+
return getJdbcTemplate().query(this.groupAuthoritiesByUsernameQuery, (rs, rowNum) -> {
242+
String roleName = getRolePrefix() + rs.getString(3);
243+
return new SimpleGrantedAuthority(roleName);
244+
}, username);
246245
}
247246

248247
/**

core/src/main/java/org/springframework/security/provisioning/JdbcUserDetailsManager.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,7 @@ protected Authentication createNewAuthentication(Authentication currentAuth, Str
336336

337337
@Override
338338
public boolean userExists(String username) {
339-
List<String> users = getJdbcTemplate().queryForList(this.userExistsSql, new String[] { username },
340-
String.class);
339+
List<String> users = getJdbcTemplate().queryForList(this.userExistsSql, String.class, username);
341340
if (users.size() > 1) {
342341
throw new IncorrectResultSizeDataAccessException("More than one user found with name '" + username + "'",
343342
1);
@@ -353,7 +352,7 @@ public List<String> findAllGroups() {
353352
@Override
354353
public List<String> findUsersInGroup(String groupName) {
355354
Assert.hasText(groupName, "groupName should have text");
356-
return getJdbcTemplate().queryForList(this.findUsersInGroupSql, new String[] { groupName }, String.class);
355+
return getJdbcTemplate().queryForList(this.findUsersInGroupSql, String.class, groupName);
357356
}
358357

359358
@Override
@@ -422,8 +421,7 @@ public void removeUserFromGroup(final String username, final String groupName) {
422421
public List<GrantedAuthority> findGroupAuthorities(String groupName) {
423422
this.logger.debug("Loading authorities for group '" + groupName + "'");
424423
Assert.hasText(groupName, "groupName should have text");
425-
return getJdbcTemplate().query(this.groupAuthoritiesSql, new String[] { groupName },
426-
this.grantedAuthorityMapper);
424+
return getJdbcTemplate().query(this.groupAuthoritiesSql, this.grantedAuthorityMapper, groupName);
427425
}
428426

429427
private GrantedAuthority mapToGrantedAuthority(ResultSet rs, int rowNum) throws SQLException {

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

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.springframework.jdbc.core.PreparedStatementSetter;
3434
import org.springframework.jdbc.core.RowMapper;
3535
import org.springframework.jdbc.core.SqlParameterValue;
36-
import org.springframework.jdbc.support.lob.DefaultLobHandler;
3736
import org.springframework.jdbc.support.lob.LobCreator;
3837
import org.springframework.jdbc.support.lob.LobHandler;
3938
import org.springframework.security.web.webauthn.api.AuthenticatorTransport;
@@ -62,11 +61,11 @@
6261
*/
6362
public final class JdbcUserCredentialRepository implements UserCredentialRepository {
6463

65-
private RowMapper<CredentialRecord> credentialRecordRowMapper = new CredentialRecordRowMapper();
64+
private RowMapper<CredentialRecord> credentialRecordRowMapper = new CredentialRecordRowMapper(ResultSet::getBytes);
6665

6766
private Function<CredentialRecord, List<SqlParameterValue>> credentialRecordParametersMapper = new CredentialRecordParametersMapper();
6867

69-
private LobHandler lobHandler = new DefaultLobHandler();
68+
private SetBytes setBytes = PreparedStatement::setBytes;
7069

7170
private final JdbcOperations jdbcOperations;
7271

@@ -159,22 +158,16 @@ public void save(CredentialRecord record) {
159158

160159
private void insertCredentialRecord(CredentialRecord record) {
161160
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);
167163
}
168164

169165
private int updateCredentialRecord(CredentialRecord record) {
170166
List<SqlParameterValue> parameters = this.credentialRecordParametersMapper.apply(record);
171167
SqlParameterValue credentialId = parameters.remove(0);
172168
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);
178171
}
179172

180173
@Override
@@ -195,10 +188,18 @@ public List<CredentialRecord> findByUserId(Bytes userId) {
195188
/**
196189
* Sets a {@link LobHandler} for large binary fields and large text field parameters.
197190
* @param lobHandler the lob handler
191+
* @deprecated {@link LobHandler} is deprecated without replacement, as such this
192+
* method will also be removed without replacement
198193
*/
194+
@Deprecated(since = "6.5", forRemoval = true)
199195
public void setLobHandler(LobHandler lobHandler) {
200196
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);
202203
}
203204

204205
private static class CredentialRecordParametersMapper
@@ -246,13 +247,25 @@ private Timestamp fromInstant(Instant instant) {
246247

247248
}
248249

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 {
250257

251-
private final LobCreator lobCreator;
258+
byte[] getBytes(ResultSet rs, String columnName) throws SQLException;
252259

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) {
254267
super(args);
255-
this.lobCreator = lobCreator;
268+
this.setBytes = setBytes;
256269
}
257270

258271
@Override
@@ -264,7 +277,7 @@ protected void doSetValue(PreparedStatement ps, int parameterPosition, Object ar
264277
"Value of blob parameter must be byte[]");
265278
}
266279
byte[] valueBytes = (byte[]) paramValue.getValue();
267-
this.lobCreator.setBlobAsBytes(ps, parameterPosition, valueBytes);
280+
this.setBytes.setBytes(ps, parameterPosition, valueBytes);
268281
return;
269282
}
270283
}
@@ -275,14 +288,17 @@ protected void doSetValue(PreparedStatement ps, int parameterPosition, Object ar
275288

276289
private static class CredentialRecordRowMapper implements RowMapper<CredentialRecord> {
277290

278-
private LobHandler lobHandler = new DefaultLobHandler();
291+
private final GetBytes getBytes;
292+
293+
CredentialRecordRowMapper(GetBytes getBytes) {
294+
this.getBytes = getBytes;
295+
}
279296

280297
@Override
281298
public CredentialRecord mapRow(ResultSet rs, int rowNum) throws SQLException {
282299
Bytes credentialId = Bytes.fromBase64(new String(rs.getString("credential_id").getBytes()));
283300
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"));
286302
long signatureCount = rs.getLong("signature_count");
287303
boolean uvInitialized = rs.getBoolean("uv_initialized");
288304
boolean backupEligible = rs.getBoolean("backup_eligible");
@@ -291,13 +307,13 @@ public CredentialRecord mapRow(ResultSet rs, int rowNum) throws SQLException {
291307
boolean backupState = rs.getBoolean("backup_state");
292308

293309
Bytes attestationObject = null;
294-
byte[] rawAttestationObject = this.lobHandler.getBlobAsBytes(rs, "attestation_object");
310+
byte[] rawAttestationObject = this.getBytes.getBytes(rs, "attestation_object");
295311
if (rawAttestationObject != null) {
296312
attestationObject = new Bytes(rawAttestationObject);
297313
}
298314

299315
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");
301317
if (rawAttestationClientDataJson != null) {
302318
attestationClientDataJson = new Bytes(rawAttestationClientDataJson);
303319
}

0 commit comments

Comments
 (0)