1 17 18 19 package org.apache.catalina.realm; 20 21 22 import java.security.Principal ; 23 import java.sql.Connection ; 24 import java.sql.Driver ; 25 import java.sql.PreparedStatement ; 26 import java.sql.ResultSet ; 27 import java.sql.SQLException ; 28 import java.util.ArrayList ; 29 import java.util.Properties ; 30 31 import org.apache.catalina.LifecycleException; 32 import org.apache.catalina.util.StringManager; 33 34 35 50 51 public class JDBCRealm 52 extends RealmBase { 53 54 55 57 58 61 protected String connectionName = null; 62 63 64 67 protected String connectionPassword = null; 68 69 70 73 protected String connectionURL = null; 74 75 76 79 protected Connection dbConnection = null; 80 81 82 85 protected Driver driver = null; 86 87 88 91 protected String driverName = null; 92 93 94 97 protected static final String info = 98 "org.apache.catalina.realm.JDBCRealm/1.0"; 99 100 101 104 protected static final String name = "JDBCRealm"; 105 106 107 110 protected PreparedStatement preparedCredentials = null; 111 112 113 117 protected PreparedStatement preparedRoles = null; 118 119 120 123 protected String roleNameCol = null; 124 125 126 129 protected static final StringManager sm = 130 StringManager.getManager(Constants.Package); 131 132 133 136 protected String userCredCol = null; 137 138 139 142 protected String userNameCol = null; 143 144 145 148 protected String userRoleTable = null; 149 150 151 154 protected String userTable = null; 155 156 157 159 163 public String getConnectionName() { 164 return connectionName; 165 } 166 167 172 public void setConnectionName(String connectionName) { 173 this.connectionName = connectionName; 174 } 175 176 180 public String getConnectionPassword() { 181 return connectionPassword; 182 } 183 184 189 public void setConnectionPassword(String connectionPassword) { 190 this.connectionPassword = connectionPassword; 191 } 192 193 197 public String getConnectionURL() { 198 return connectionURL; 199 } 200 201 206 public void setConnectionURL( String connectionURL ) { 207 this.connectionURL = connectionURL; 208 } 209 210 214 public String getDriverName() { 215 return driverName; 216 } 217 218 223 public void setDriverName( String driverName ) { 224 this.driverName = driverName; 225 } 226 227 231 public String getRoleNameCol() { 232 return roleNameCol; 233 } 234 235 240 public void setRoleNameCol( String roleNameCol ) { 241 this.roleNameCol = roleNameCol; 242 } 243 244 248 public String getUserCredCol() { 249 return userCredCol; 250 } 251 252 257 public void setUserCredCol( String userCredCol ) { 258 this.userCredCol = userCredCol; 259 } 260 261 265 public String getUserNameCol() { 266 return userNameCol; 267 } 268 269 274 public void setUserNameCol( String userNameCol ) { 275 this.userNameCol = userNameCol; 276 } 277 278 282 public String getUserRoleTable() { 283 return userRoleTable; 284 } 285 286 291 public void setUserRoleTable( String userRoleTable ) { 292 this.userRoleTable = userRoleTable; 293 } 294 295 299 public String getUserTable() { 300 return userTable; 301 } 302 303 308 public void setUserTable( String userTable ) { 309 this.userTable = userTable; 310 } 311 312 313 315 316 330 public synchronized Principal authenticate(String username, String credentials) { 331 332 int numberOfTries = 2; 341 while (numberOfTries>0) { 342 try { 343 344 open(); 346 347 Principal principal = authenticate(dbConnection, 349 username, credentials); 350 351 352 return (principal); 354 355 } catch (SQLException e) { 356 357 containerLog.error(sm.getString("jdbcRealm.exception"), e); 359 360 if (dbConnection != null) 362 close(dbConnection); 363 364 } 365 366 numberOfTries--; 367 } 368 369 return null; 371 372 } 373 374 375 377 378 380 381 390 public synchronized Principal authenticate(Connection dbConnection, 391 String username, 392 String credentials) { 393 394 if (username == null) { 396 return (null); 397 } 398 399 String dbCredentials = getPassword(username); 401 402 boolean validated = false; 404 if (hasMessageDigest()) { 405 validated = (digest(credentials).equalsIgnoreCase(dbCredentials)); 407 } else { 408 validated = (digest(credentials).equals(dbCredentials)); 409 } 410 411 if (validated) { 412 if (containerLog.isTraceEnabled()) 413 containerLog.trace(sm.getString("jdbcRealm.authenticateSuccess", 414 username)); 415 } else { 416 if (containerLog.isTraceEnabled()) 417 containerLog.trace(sm.getString("jdbcRealm.authenticateFailure", 418 username)); 419 return (null); 420 } 421 422 ArrayList roles = getRoles(username); 423 424 return (new GenericPrincipal(this, username, credentials, roles)); 426 427 } 428 429 430 435 protected void close(Connection dbConnection) { 436 437 if (dbConnection == null) 439 return; 440 441 try { 443 preparedCredentials.close(); 444 } catch (Throwable f) { 445 ; 446 } 447 this.preparedCredentials = null; 448 449 450 try { 451 preparedRoles.close(); 452 } catch (Throwable f) { 453 ; 454 } 455 this.preparedRoles = null; 456 457 458 try { 460 dbConnection.close(); 461 } catch (SQLException e) { 462 containerLog.warn(sm.getString("jdbcRealm.close"), e); } finally { 464 this.dbConnection = null; 465 } 466 467 } 468 469 470 479 protected PreparedStatement credentials(Connection dbConnection, 480 String username) 481 throws SQLException { 482 483 if (preparedCredentials == null) { 484 StringBuffer sb = new StringBuffer ("SELECT "); 485 sb.append(userCredCol); 486 sb.append(" FROM "); 487 sb.append(userTable); 488 sb.append(" WHERE "); 489 sb.append(userNameCol); 490 sb.append(" = ?"); 491 492 if(containerLog.isDebugEnabled()) { 493 containerLog.debug("credentials query: " + sb.toString()); 494 } 495 496 preparedCredentials = 497 dbConnection.prepareStatement(sb.toString()); 498 } 499 500 if (username == null) { 501 preparedCredentials.setNull(1,java.sql.Types.VARCHAR); 502 } else { 503 preparedCredentials.setString(1, username); 504 } 505 506 return (preparedCredentials); 507 } 508 509 510 513 protected String getName() { 514 515 return (name); 516 517 } 518 519 520 523 protected String getPassword(String username) { 524 525 String dbCredentials = null; 527 PreparedStatement stmt = null; 528 ResultSet rs = null; 529 530 int numberOfTries = 2; 539 while (numberOfTries>0) { 540 try { 541 542 open(); 544 545 try { 546 stmt = credentials(dbConnection, username); 547 rs = stmt.executeQuery(); 548 549 if (rs.next()) { 550 dbCredentials = rs.getString(1); 551 } 552 rs.close(); 553 rs = null; 554 if (dbCredentials == null) { 555 return (null); 556 } 557 558 dbCredentials = dbCredentials.trim(); 559 return dbCredentials; 560 561 } finally { 562 if (rs!=null) { 563 try { 564 rs.close(); 565 } catch(SQLException e) { 566 containerLog.warn(sm.getString("jdbcRealm.abnormalCloseResultSet")); 567 } 568 } 569 dbConnection.commit(); 570 } 571 572 } catch (SQLException e) { 573 574 containerLog.error(sm.getString("jdbcRealm.exception"), e); 576 577 if (dbConnection != null) 579 close(dbConnection); 580 581 } 582 583 numberOfTries--; 584 } 585 586 return (null); 587 } 588 589 590 593 protected Principal getPrincipal(String username) { 594 595 return (new GenericPrincipal(this, 596 username, 597 getPassword(username), 598 getRoles(username))); 599 600 } 601 602 603 606 protected ArrayList getRoles(String username) { 607 608 PreparedStatement stmt = null; 609 ResultSet rs = null; 610 611 int numberOfTries = 2; 620 while (numberOfTries>0) { 621 try { 622 623 open(); 625 626 try { 627 ArrayList roleList = new ArrayList (); 629 stmt = roles(dbConnection, username); 630 rs = stmt.executeQuery(); 631 while (rs.next()) { 632 String role = rs.getString(1); 633 if (null!=role) { 634 roleList.add(role.trim()); 635 } 636 } 637 rs.close(); 638 rs = null; 639 640 return (roleList); 641 642 } finally { 643 if (rs!=null) { 644 try { 645 rs.close(); 646 } catch(SQLException e) { 647 containerLog.warn(sm.getString("jdbcRealm.abnormalCloseResultSet")); 648 } 649 } 650 dbConnection.commit(); 651 } 652 653 } catch (SQLException e) { 654 655 containerLog.error(sm.getString("jdbcRealm.exception"), e); 657 658 if (dbConnection != null) 660 close(dbConnection); 661 662 } 663 664 numberOfTries--; 665 } 666 667 return (null); 668 669 } 670 671 672 678 protected Connection open() throws SQLException { 679 680 if (dbConnection != null) 682 return (dbConnection); 683 684 if (driver == null) { 686 try { 687 Class clazz = Class.forName(driverName); 688 driver = (Driver ) clazz.newInstance(); 689 } catch (Throwable e) { 690 throw new SQLException (e.getMessage()); 691 } 692 } 693 694 Properties props = new Properties (); 696 if (connectionName != null) 697 props.put("user", connectionName); 698 if (connectionPassword != null) 699 props.put("password", connectionPassword); 700 dbConnection = driver.connect(connectionURL, props); 701 dbConnection.setAutoCommit(false); 702 return (dbConnection); 703 704 } 705 706 707 712 protected void release(Connection dbConnection) { 713 714 ; 716 } 717 718 719 728 protected PreparedStatement roles(Connection dbConnection, String username) 729 throws SQLException { 730 731 if (preparedRoles == null) { 732 StringBuffer sb = new StringBuffer ("SELECT "); 733 sb.append(roleNameCol); 734 sb.append(" FROM "); 735 sb.append(userRoleTable); 736 sb.append(" WHERE "); 737 sb.append(userNameCol); 738 sb.append(" = ?"); 739 preparedRoles = 740 dbConnection.prepareStatement(sb.toString()); 741 } 742 743 preparedRoles.setString(1, username); 744 return (preparedRoles); 745 746 } 747 748 749 751 752 759 public void start() throws LifecycleException { 760 761 super.start(); 763 764 try { 767 open(); 768 } catch (SQLException e) { 769 containerLog.error(sm.getString("jdbcRealm.open"), e); 770 } 771 772 } 773 774 775 781 public void stop() throws LifecycleException { 782 783 super.stop(); 785 786 close(this.dbConnection); 788 789 } 790 791 792 } 793 | Popular Tags |