29
29
import org .springframework .context .ApplicationContextException ;
30
30
import org .springframework .core .log .LogMessage ;
31
31
import org .springframework .dao .IncorrectResultSizeDataAccessException ;
32
+ import org .springframework .jdbc .core .JdbcTemplate ;
32
33
import org .springframework .jdbc .core .PreparedStatementSetter ;
33
34
import org .springframework .jdbc .core .RowMapper ;
34
35
import org .springframework .security .access .AccessDeniedException ;
@@ -214,7 +215,7 @@ protected void initDao() throws ApplicationContextException {
214
215
*/
215
216
@ Override
216
217
protected List <UserDetails > loadUsersByUsername (String username ) {
217
- return getJdbcTemplate ().query (getUsersByUsernameQuery (), this .userDetailsMapper , username );
218
+ return requireJdbcTemplate ().query (getUsersByUsernameQuery (), this .userDetailsMapper , username );
218
219
}
219
220
220
221
private UserDetails mapToUser (ResultSet rs , int rowNum ) throws SQLException {
@@ -237,7 +238,7 @@ private UserDetails mapToUser(ResultSet rs, int rowNum) throws SQLException {
237
238
@ Override
238
239
public void createUser (final UserDetails user ) {
239
240
validateUserDetails (user );
240
- getJdbcTemplate ().update (this .createUserSql , (ps ) -> {
241
+ requireJdbcTemplate ().update (this .createUserSql , (ps ) -> {
241
242
ps .setString (1 , user .getUsername ());
242
243
ps .setString (2 , user .getPassword ());
243
244
ps .setBoolean (3 , user .isEnabled ());
@@ -257,7 +258,7 @@ public void createUser(final UserDetails user) {
257
258
@ Override
258
259
public void updateUser (final UserDetails user ) {
259
260
validateUserDetails (user );
260
- getJdbcTemplate ().update (this .updateUserSql , (ps ) -> {
261
+ requireJdbcTemplate ().update (this .updateUserSql , (ps ) -> {
261
262
ps .setString (1 , user .getPassword ());
262
263
ps .setBoolean (2 , user .isEnabled ());
263
264
int paramCount = ps .getParameterMetaData ().getParameterCount ();
@@ -281,7 +282,7 @@ public void updateUser(final UserDetails user) {
281
282
282
283
private void insertUserAuthorities (UserDetails user ) {
283
284
for (GrantedAuthority auth : user .getAuthorities ()) {
284
- getJdbcTemplate ().update (this .createAuthoritySql , user .getUsername (), auth .getAuthority ());
285
+ requireJdbcTemplate ().update (this .createAuthoritySql , user .getUsername (), auth .getAuthority ());
285
286
}
286
287
}
287
288
@@ -290,12 +291,12 @@ public void deleteUser(String username) {
290
291
if (getEnableAuthorities ()) {
291
292
deleteUserAuthorities (username );
292
293
}
293
- getJdbcTemplate ().update (this .deleteUserSql , username );
294
+ requireJdbcTemplate ().update (this .deleteUserSql , username );
294
295
this .userCache .removeUserFromCache (username );
295
296
}
296
297
297
298
private void deleteUserAuthorities (String username ) {
298
- getJdbcTemplate ().update (this .deleteUserAuthoritiesSql , username );
299
+ requireJdbcTemplate ().update (this .deleteUserAuthoritiesSql , username );
299
300
}
300
301
301
302
@ Override
@@ -318,7 +319,7 @@ public void changePassword(String oldPassword, String newPassword) throws Authen
318
319
this .logger .debug ("No authentication manager set. Password won't be re-checked." );
319
320
}
320
321
this .logger .debug ("Changing password for user '" + username + "'" );
321
- getJdbcTemplate ().update (this .changePasswordSql , newPassword , username );
322
+ requireJdbcTemplate ().update (this .changePasswordSql , newPassword , username );
322
323
Authentication authentication = createNewAuthentication (currentUser , newPassword );
323
324
SecurityContext context = this .securityContextHolderStrategy .createEmptyContext ();
324
325
context .setAuthentication (authentication );
@@ -336,7 +337,7 @@ protected Authentication createNewAuthentication(Authentication currentAuth, Str
336
337
337
338
@ Override
338
339
public boolean userExists (String username ) {
339
- List <String > users = getJdbcTemplate ().queryForList (this .userExistsSql , String .class , username );
340
+ List <String > users = requireJdbcTemplate ().queryForList (this .userExistsSql , String .class , username );
340
341
if (users .size () > 1 ) {
341
342
throw new IncorrectResultSizeDataAccessException ("More than one user found with name '" + username + "'" ,
342
343
1 );
@@ -346,13 +347,13 @@ public boolean userExists(String username) {
346
347
347
348
@ Override
348
349
public List <String > findAllGroups () {
349
- return getJdbcTemplate ().queryForList (this .findAllGroupsSql , String .class );
350
+ return requireJdbcTemplate ().queryForList (this .findAllGroupsSql , String .class );
350
351
}
351
352
352
353
@ Override
353
354
public List <String > findUsersInGroup (String groupName ) {
354
355
Assert .hasText (groupName , "groupName should have text" );
355
- return getJdbcTemplate ().queryForList (this .findUsersInGroupSql , String .class , groupName );
356
+ return requireJdbcTemplate ().queryForList (this .findUsersInGroupSql , String .class , groupName );
356
357
}
357
358
358
359
@ Override
@@ -361,11 +362,11 @@ public void createGroup(final String groupName, final List<GrantedAuthority> aut
361
362
Assert .notNull (authorities , "authorities cannot be null" );
362
363
this .logger .debug ("Creating new group '" + groupName + "' with authorities "
363
364
+ AuthorityUtils .authorityListToSet (authorities ));
364
- getJdbcTemplate ().update (this .insertGroupSql , groupName );
365
+ requireJdbcTemplate ().update (this .insertGroupSql , groupName );
365
366
int groupId = findGroupId (groupName );
366
367
for (GrantedAuthority a : authorities ) {
367
368
String authority = a .getAuthority ();
368
- getJdbcTemplate ().update (this .insertGroupAuthoritySql , (ps ) -> {
369
+ requireJdbcTemplate ().update (this .insertGroupAuthoritySql , (ps ) -> {
369
370
ps .setInt (1 , groupId );
370
371
ps .setString (2 , authority );
371
372
});
@@ -378,17 +379,17 @@ public void deleteGroup(String groupName) {
378
379
Assert .hasText (groupName , "groupName should have text" );
379
380
int id = findGroupId (groupName );
380
381
PreparedStatementSetter groupIdPSS = (ps ) -> ps .setInt (1 , id );
381
- getJdbcTemplate ().update (this .deleteGroupMembersSql , groupIdPSS );
382
- getJdbcTemplate ().update (this .deleteGroupAuthoritiesSql , groupIdPSS );
383
- getJdbcTemplate ().update (this .deleteGroupSql , groupIdPSS );
382
+ requireJdbcTemplate ().update (this .deleteGroupMembersSql , groupIdPSS );
383
+ requireJdbcTemplate ().update (this .deleteGroupAuthoritiesSql , groupIdPSS );
384
+ requireJdbcTemplate ().update (this .deleteGroupSql , groupIdPSS );
384
385
}
385
386
386
387
@ Override
387
388
public void renameGroup (String oldName , String newName ) {
388
389
this .logger .debug ("Changing group name from '" + oldName + "' to '" + newName + "'" );
389
390
Assert .hasText (oldName , "oldName should have text" );
390
391
Assert .hasText (newName , "newName should have text" );
391
- getJdbcTemplate ().update (this .renameGroupSql , newName , oldName );
392
+ requireJdbcTemplate ().update (this .renameGroupSql , newName , oldName );
392
393
}
393
394
394
395
@ Override
@@ -397,7 +398,7 @@ public void addUserToGroup(final String username, final String groupName) {
397
398
Assert .hasText (username , "username should have text" );
398
399
Assert .hasText (groupName , "groupName should have text" );
399
400
int id = findGroupId (groupName );
400
- getJdbcTemplate ().update (this .insertGroupMemberSql , (ps ) -> {
401
+ requireJdbcTemplate ().update (this .insertGroupMemberSql , (ps ) -> {
401
402
ps .setInt (1 , id );
402
403
ps .setString (2 , username );
403
404
});
@@ -410,7 +411,7 @@ public void removeUserFromGroup(final String username, final String groupName) {
410
411
Assert .hasText (username , "username should have text" );
411
412
Assert .hasText (groupName , "groupName should have text" );
412
413
int id = findGroupId (groupName );
413
- getJdbcTemplate ().update (this .deleteGroupMemberSql , (ps ) -> {
414
+ requireJdbcTemplate ().update (this .deleteGroupMemberSql , (ps ) -> {
414
415
ps .setInt (1 , id );
415
416
ps .setString (2 , username );
416
417
});
@@ -421,7 +422,7 @@ public void removeUserFromGroup(final String username, final String groupName) {
421
422
public List <GrantedAuthority > findGroupAuthorities (String groupName ) {
422
423
this .logger .debug ("Loading authorities for group '" + groupName + "'" );
423
424
Assert .hasText (groupName , "groupName should have text" );
424
- return getJdbcTemplate ().query (this .groupAuthoritiesSql , this .grantedAuthorityMapper , groupName );
425
+ return requireJdbcTemplate ().query (this .groupAuthoritiesSql , this .grantedAuthorityMapper , groupName );
425
426
}
426
427
427
428
private GrantedAuthority mapToGrantedAuthority (ResultSet rs , int rowNum ) throws SQLException {
@@ -435,7 +436,7 @@ public void removeGroupAuthority(String groupName, final GrantedAuthority author
435
436
Assert .hasText (groupName , "groupName should have text" );
436
437
Assert .notNull (authority , "authority cannot be null" );
437
438
int id = findGroupId (groupName );
438
- getJdbcTemplate ().update (this .deleteGroupAuthoritySql , (ps ) -> {
439
+ requireJdbcTemplate ().update (this .deleteGroupAuthoritySql , (ps ) -> {
439
440
ps .setInt (1 , id );
440
441
ps .setString (2 , authority .getAuthority ());
441
442
});
@@ -447,14 +448,20 @@ public void addGroupAuthority(final String groupName, final GrantedAuthority aut
447
448
Assert .hasText (groupName , "groupName should have text" );
448
449
Assert .notNull (authority , "authority cannot be null" );
449
450
int id = findGroupId (groupName );
450
- getJdbcTemplate ().update (this .insertGroupAuthoritySql , (ps ) -> {
451
+ requireJdbcTemplate ().update (this .insertGroupAuthoritySql , (ps ) -> {
451
452
ps .setInt (1 , id );
452
453
ps .setString (2 , authority .getAuthority ());
453
454
});
454
455
}
455
456
456
457
private int findGroupId (String group ) {
457
- return getJdbcTemplate ().queryForObject (this .findGroupIdSql , Integer .class , group );
458
+ return requireJdbcTemplate ().queryForObject (this .findGroupIdSql , Integer .class , group );
459
+ }
460
+
461
+ private JdbcTemplate requireJdbcTemplate () {
462
+ JdbcTemplate jdbc = getJdbcTemplate ();
463
+ Assert .notNull (jdbc , "JdbcTemplate cannot be null" );
464
+ return jdbc ;
458
465
}
459
466
460
467
/**
0 commit comments