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.PreparedStatement ; 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 import java.util.ArrayList ; 28 29 import javax.naming.Context ; 30 import javax.sql.DataSource ; 31 32 import org.apache.naming.ContextBindings; 33 import org.apache.catalina.LifecycleException; 34 import org.apache.catalina.ServerFactory; 35 import org.apache.catalina.core.StandardServer; 36 import org.apache.catalina.util.StringManager; 37 38 50 51 public class DataSourceRealm 52 extends RealmBase { 53 54 55 57 58 61 private String preparedRoles = null; 62 63 64 67 private String preparedCredentials = null; 68 69 70 73 protected String dataSourceName = null; 74 75 76 79 protected static final String info = 80 "org.apache.catalina.realm.DataSourceRealm/1.0"; 81 82 83 86 protected boolean localDataSource = false; 87 88 89 92 protected static final String name = "DataSourceRealm"; 93 94 95 98 protected String roleNameCol = null; 99 100 101 104 protected static final StringManager sm = 105 StringManager.getManager(Constants.Package); 106 107 108 111 protected String userCredCol = null; 112 113 114 117 protected String userNameCol = null; 118 119 120 123 protected String userRoleTable = null; 124 125 126 129 protected String userTable = null; 130 131 132 134 135 139 public String getDataSourceName() { 140 return dataSourceName; 141 } 142 143 148 public void setDataSourceName( String dataSourceName) { 149 this.dataSourceName = dataSourceName; 150 } 151 152 155 public boolean getLocalDataSource() { 156 return localDataSource; 157 } 158 159 165 public void setLocalDataSource(boolean localDataSource) { 166 this.localDataSource = localDataSource; 167 } 168 169 173 public String getRoleNameCol() { 174 return roleNameCol; 175 } 176 177 182 public void setRoleNameCol( String roleNameCol ) { 183 this.roleNameCol = roleNameCol; 184 } 185 186 190 public String getUserCredCol() { 191 return userCredCol; 192 } 193 194 199 public void setUserCredCol( String userCredCol ) { 200 this.userCredCol = userCredCol; 201 } 202 203 207 public String getUserNameCol() { 208 return userNameCol; 209 } 210 211 216 public void setUserNameCol( String userNameCol ) { 217 this.userNameCol = userNameCol; 218 } 219 220 224 public String getUserRoleTable() { 225 return userRoleTable; 226 } 227 228 233 public void setUserRoleTable( String userRoleTable ) { 234 this.userRoleTable = userRoleTable; 235 } 236 237 241 public String getUserTable() { 242 return userTable; 243 } 244 245 250 public void setUserTable( String userTable ) { 251 this.userTable = userTable; 252 } 253 254 255 257 258 271 public Principal authenticate(String username, String credentials) { 272 273 if (username == null) { 275 return null; 276 } 277 278 Connection dbConnection = null; 279 280 try { 281 282 dbConnection = open(); 284 if (dbConnection == null) { 285 return null; 287 } 288 289 return authenticate(dbConnection, username, credentials); 291 292 } catch (SQLException e) { 293 containerLog.error(sm.getString("dataSourceRealm.exception"), e); 295 296 return (null); 298 299 } finally { 300 close(dbConnection); 301 } 302 303 } 304 305 306 308 309 311 312 321 protected Principal authenticate(Connection dbConnection, 322 String username, 323 String credentials) throws SQLException { 324 325 String dbCredentials = getPassword(dbConnection, username); 326 327 boolean validated = false; 329 if (hasMessageDigest()) { 330 validated = (digest(credentials).equalsIgnoreCase(dbCredentials)); 332 } else 333 validated = (digest(credentials).equals(dbCredentials)); 334 335 if (validated) { 336 if (containerLog.isTraceEnabled()) 337 containerLog.trace( 338 sm.getString("dataSourceRealm.authenticateSuccess", 339 username)); 340 } else { 341 if (containerLog.isTraceEnabled()) 342 containerLog.trace( 343 sm.getString("dataSourceRealm.authenticateFailure", 344 username)); 345 return (null); 346 } 347 348 ArrayList list = getRoles(dbConnection, username); 349 350 return (new GenericPrincipal(this, username, credentials, list)); 352 353 } 354 355 356 361 protected void close(Connection dbConnection) { 362 363 if (dbConnection == null) 365 return; 366 367 try { 369 if (!dbConnection.getAutoCommit()) { 370 dbConnection.commit(); 371 } 372 } catch (SQLException e) { 373 containerLog.error("Exception committing connection before closing:", e); 374 } 375 376 try { 378 dbConnection.close(); 379 } catch (SQLException e) { 380 containerLog.error(sm.getString("dataSourceRealm.close"), e); } 382 383 } 384 385 390 protected Connection open() { 391 392 try { 393 Context context = null; 394 if (localDataSource) { 395 context = ContextBindings.getClassLoader(); 396 context = (Context ) context.lookup("comp/env"); 397 } else { 398 StandardServer server = 399 (StandardServer) ServerFactory.getServer(); 400 context = server.getGlobalNamingContext(); 401 } 402 DataSource dataSource = (DataSource )context.lookup(dataSourceName); 403 return dataSource.getConnection(); 404 } catch (Exception e) { 405 containerLog.error(sm.getString("dataSourceRealm.exception"), e); 407 } 408 return null; 409 } 410 411 414 protected String getName() { 415 416 return (name); 417 418 } 419 420 423 protected String getPassword(String username) { 424 425 Connection dbConnection = null; 426 427 dbConnection = open(); 429 if (dbConnection == null) { 430 return null; 431 } 432 433 try { 434 return getPassword(dbConnection, username); 435 } finally { 436 close(dbConnection); 437 } 438 } 439 440 445 protected String getPassword(Connection dbConnection, 446 String username) { 447 448 ResultSet rs = null; 449 PreparedStatement stmt = null; 450 String dbCredentials = null; 451 452 try { 453 stmt = credentials(dbConnection, username); 454 rs = stmt.executeQuery(); 455 if (rs.next()) { 456 dbCredentials = rs.getString(1); 457 } 458 459 return (dbCredentials != null) ? dbCredentials.trim() : null; 460 461 } catch(SQLException e) { 462 containerLog.error( 463 sm.getString("dataSourceRealm.getPassword.exception", 464 username)); 465 } finally { 466 try { 467 if (rs != null) { 468 rs.close(); 469 } 470 if (stmt != null) { 471 stmt.close(); 472 } 473 } catch (SQLException e) { 474 containerLog.error( 475 sm.getString("dataSourceRealm.getPassword.exception", 476 username)); 477 478 } 479 } 480 481 return null; 482 } 483 484 485 488 protected Principal getPrincipal(String username) { 489 Connection dbConnection = open(); 490 if (dbConnection == null) { 491 return new GenericPrincipal(this,username, null, null); 492 } 493 try { 494 return (new GenericPrincipal(this, 495 username, 496 getPassword(dbConnection, username), 497 getRoles(dbConnection, username))); 498 } finally { 499 close(dbConnection); 500 } 501 502 } 503 504 508 protected ArrayList getRoles(String username) { 509 510 Connection dbConnection = null; 511 512 dbConnection = open(); 514 if (dbConnection == null) { 515 return null; 516 } 517 518 try { 519 return getRoles(dbConnection, username); 520 } finally { 521 close(dbConnection); 522 } 523 } 524 525 530 protected ArrayList getRoles(Connection dbConnection, 531 String username) { 532 533 ResultSet rs = null; 534 PreparedStatement stmt = null; 535 ArrayList list = null; 536 537 try { 538 stmt = roles(dbConnection, username); 539 rs = stmt.executeQuery(); 540 list = new ArrayList (); 541 542 while (rs.next()) { 543 String role = rs.getString(1); 544 if (role != null) { 545 list.add(role.trim()); 546 } 547 } 548 return list; 549 } catch(SQLException e) { 550 containerLog.error( 551 sm.getString("dataSourceRealm.getRoles.exception", username)); 552 } 553 finally { 554 try { 555 if (rs != null) { 556 rs.close(); 557 } 558 if (stmt != null) { 559 stmt.close(); 560 } 561 } catch (SQLException e) { 562 containerLog.error( 563 sm.getString("dataSourceRealm.getRoles.exception", 564 username)); 565 } 566 } 567 568 return null; 569 } 570 571 580 private PreparedStatement credentials(Connection dbConnection, 581 String username) 582 throws SQLException { 583 584 PreparedStatement credentials = 585 dbConnection.prepareStatement(preparedCredentials); 586 587 credentials.setString(1, username); 588 return (credentials); 589 590 } 591 592 601 private PreparedStatement roles(Connection dbConnection, String username) 602 throws SQLException { 603 604 PreparedStatement roles = 605 dbConnection.prepareStatement(preparedRoles); 606 607 roles.setString(1, username); 608 return (roles); 609 610 } 611 612 614 615 622 public void start() throws LifecycleException { 623 624 super.start(); 626 627 StringBuffer temp = new StringBuffer ("SELECT "); 629 temp.append(roleNameCol); 630 temp.append(" FROM "); 631 temp.append(userRoleTable); 632 temp.append(" WHERE "); 633 temp.append(userNameCol); 634 temp.append(" = ?"); 635 preparedRoles = temp.toString(); 636 637 temp = new StringBuffer ("SELECT "); 639 temp.append(userCredCol); 640 temp.append(" FROM "); 641 temp.append(userTable); 642 temp.append(" WHERE "); 643 temp.append(userNameCol); 644 temp.append(" = ?"); 645 preparedCredentials = temp.toString(); 646 } 647 648 649 655 public void stop() throws LifecycleException { 656 657 super.stop(); 659 660 } 661 662 663 } 664 | Popular Tags |