5
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
6
* (the "License"); you may not use this file except in compliance with
7
7
* the License. You may obtain a copy of the License at
8
- *
8
+ *
9
9
* http://www.apache.org/licenses/LICENSE-2.0
10
- *
10
+ *
11
11
* Unless required by applicable law or agreed to in writing, software
12
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -271,13 +271,13 @@ public String getInfo() {
271
271
*/
272
272
@ Override
273
273
public Principal authenticate (String username , String credentials ) {
274
-
274
+
275
275
// No user or no credentials
276
276
// Can't possibly authenticate, don't bother the database then
277
277
if (username == null || credentials == null ) {
278
278
return null ;
279
279
}
280
-
280
+
281
281
Connection dbConnection = null ;
282
282
283
283
// Ensure that we have an open database connection
@@ -286,7 +286,7 @@ public Principal authenticate(String username, String credentials) {
286
286
// If the db connection open fails, return "not authenticated"
287
287
return null ;
288
288
}
289
-
289
+
290
290
try
291
291
{
292
292
// Acquire a Principal object for this user
@@ -331,6 +331,8 @@ protected Principal authenticate(Connection dbConnection,
331
331
332
332
if (dbCredentials == null ) {
333
333
// User was not found in the database.
334
+ // Waste a bit of time as not to reveal that the user does not exist.
335
+ compareCredentials (credentials , getClass ().getName ());
334
336
335
337
if (containerLog .isTraceEnabled ())
336
338
containerLog .trace (sm .getString ("dataSourceRealm.authenticateFailure" ,
@@ -374,7 +376,7 @@ protected void close(Connection dbConnection) {
374
376
try {
375
377
if (!dbConnection .getAutoCommit ()) {
376
378
dbConnection .commit ();
377
- }
379
+ }
378
380
} catch (SQLException e ) {
379
381
containerLog .error ("Exception committing connection before closing:" , e );
380
382
}
@@ -408,7 +410,7 @@ protected Connection open() {
408
410
} catch (Exception e ) {
409
411
// Log the problem for posterity
410
412
containerLog .error (sm .getString ("dataSourceRealm.exception" ), e );
411
- }
413
+ }
412
414
return null ;
413
415
}
414
416
@@ -437,18 +439,18 @@ protected String getPassword(String username) {
437
439
}
438
440
439
441
try {
440
- return getPassword (dbConnection , username );
442
+ return getPassword (dbConnection , username );
441
443
} finally {
442
444
close (dbConnection );
443
445
}
444
446
}
445
-
447
+
446
448
/**
447
449
* Return the password associated with the given principal's user name.
448
450
* @param dbConnection The database connection to be used
449
451
* @param username Username for which password should be retrieved
450
452
*/
451
- protected String getPassword (Connection dbConnection ,
453
+ protected String getPassword (Connection dbConnection ,
452
454
String username ) {
453
455
454
456
ResultSet rs = null ;
@@ -463,7 +465,7 @@ protected String getPassword(Connection dbConnection,
463
465
}
464
466
465
467
return (dbCredentials != null ) ? dbCredentials .trim () : null ;
466
-
468
+
467
469
} catch (SQLException e ) {
468
470
containerLog .error (
469
471
sm .getString ("dataSourceRealm.getPassword.exception" ,
@@ -480,10 +482,10 @@ protected String getPassword(Connection dbConnection,
480
482
containerLog .error (
481
483
sm .getString ("dataSourceRealm.getPassword.exception" ,
482
484
username ), e );
483
-
485
+
484
486
}
485
487
}
486
-
488
+
487
489
return null ;
488
490
}
489
491
@@ -527,15 +529,15 @@ protected ArrayList<String> getRoles(String username) {
527
529
close (dbConnection );
528
530
}
529
531
}
530
-
532
+
531
533
/**
532
534
* Return the roles associated with the given user name
533
535
* @param dbConnection The database connection to be used
534
536
* @param username Username for which roles should be retrieved
535
537
*/
536
538
protected ArrayList <String > getRoles (Connection dbConnection ,
537
539
String username ) {
538
-
540
+
539
541
if (allRolesMode != AllRolesMode .STRICT_MODE && !isRoleStoreDefined ()) {
540
542
// Using an authentication only configuration and no role store has
541
543
// been defined so don't spend cycles looking
@@ -545,12 +547,12 @@ protected ArrayList<String> getRoles(Connection dbConnection,
545
547
ResultSet rs = null ;
546
548
PreparedStatement stmt = null ;
547
549
ArrayList <String > list = null ;
548
-
550
+
549
551
try {
550
552
stmt = roles (dbConnection , username );
551
553
rs = stmt .executeQuery ();
552
554
list = new ArrayList <String >();
553
-
555
+
554
556
while (rs .next ()) {
555
557
String role = rs .getString (1 );
556
558
if (role != null ) {
@@ -576,7 +578,7 @@ protected ArrayList<String> getRoles(Connection dbConnection,
576
578
username ), e );
577
579
}
578
580
}
579
-
581
+
580
582
return null ;
581
583
}
582
584
@@ -600,7 +602,7 @@ private PreparedStatement credentials(Connection dbConnection,
600
602
return (credentials );
601
603
602
604
}
603
-
605
+
604
606
/**
605
607
* Return a PreparedStatement configured to perform the SELECT required
606
608
* to retrieve user roles for the specified username.
@@ -613,7 +615,7 @@ private PreparedStatement credentials(Connection dbConnection,
613
615
private PreparedStatement roles (Connection dbConnection , String username )
614
616
throws SQLException {
615
617
616
- PreparedStatement roles =
618
+ PreparedStatement roles =
617
619
dbConnection .prepareStatement (preparedRoles );
618
620
619
621
roles .setString (1 , username );
@@ -659,7 +661,7 @@ protected void startInternal() throws LifecycleException {
659
661
temp .append (userNameCol );
660
662
temp .append (" = ?" );
661
663
preparedCredentials = temp .toString ();
662
-
664
+
663
665
super .startInternal ();
664
666
}
665
667
}
0 commit comments