1 17 18 19 package org.apache.catalina.realm; 20 21 22 import java.security.Principal ; 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import javax.security.auth.Subject ; 28 import javax.security.auth.login.AccountExpiredException ; 29 import javax.security.auth.login.CredentialExpiredException ; 30 import javax.security.auth.login.FailedLoginException ; 31 import javax.security.auth.login.LoginContext ; 32 import javax.security.auth.login.LoginException ; 33 34 import org.apache.catalina.Container; 35 import org.apache.catalina.LifecycleException; 36 import org.apache.catalina.util.StringManager; 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 40 41 125 126 public class JAASRealm 127 extends RealmBase 128 { 129 private static Log log = LogFactory.getLog(JAASRealm.class); 130 131 133 134 138 protected String appName = null; 139 140 141 144 protected static final String info = 145 "org.apache.catalina.realm.JAASRealm/1.0"; 146 147 148 151 protected static final String name = "JAASRealm"; 152 153 154 157 protected List roleClasses = new ArrayList (); 158 159 160 163 protected static final StringManager sm = 164 StringManager.getManager(Constants.Package); 165 166 167 170 protected List userClasses = new ArrayList (); 171 172 173 178 protected boolean useContextClassLoader = true; 179 180 181 183 184 188 public void setAppName(String name) { 189 appName = name; 190 } 191 192 195 public String getAppName() { 196 return appName; 197 } 198 199 205 public void setUseContextClassLoader(boolean useContext) { 206 useContextClassLoader = useContext; 207 log.info("Setting useContextClassLoader = " + useContext); 208 } 209 210 216 public boolean isUseContextClassLoader() { 217 return useContextClassLoader; 218 } 219 220 public void setContainer(Container container) { 221 super.setContainer(container); 222 223 if( appName==null ) { 224 String name=container.getName(); 225 name = makeLegalForJAAS(name); 226 227 appName=name; 228 229 log.info("Set JAAS app name " + appName); 230 } 231 } 232 233 237 protected String roleClassNames = null; 238 239 public String getRoleClassNames() { 240 return (this.roleClassNames); 241 } 242 243 251 public void setRoleClassNames(String roleClassNames) { 252 this.roleClassNames = roleClassNames; 253 roleClasses.clear(); 254 String temp = this.roleClassNames; 255 if (temp == null) { 256 return; 257 } 258 while (true) { 259 int comma = temp.indexOf(','); 260 if (comma < 0) { 261 break; 262 } 263 roleClasses.add(temp.substring(0, comma).trim()); 264 temp = temp.substring(comma + 1); 265 } 266 temp = temp.trim(); 267 if (temp.length() > 0) { 268 roleClasses.add(temp); 269 } 270 } 271 272 273 277 protected String userClassNames = null; 278 279 public String getUserClassNames() { 280 return (this.userClassNames); 281 } 282 283 291 public void setUserClassNames(String userClassNames) { 292 this.userClassNames = userClassNames; 293 userClasses.clear(); 294 String temp = this.userClassNames; 295 if (temp == null) { 296 return; 297 } 298 while (true) { 299 int comma = temp.indexOf(','); 300 if (comma < 0) { 301 break; 302 } 303 userClasses.add(temp.substring(0, comma).trim()); 304 temp = temp.substring(comma + 1); 305 } 306 temp = temp.trim(); 307 if (temp.length() > 0) { 308 userClasses.add(temp); 309 } 310 } 311 312 313 315 316 329 public Principal authenticate(String username, String credentials) { 330 331 try { 333 LoginContext loginContext = null; 334 if( appName==null ) appName="Tomcat"; 335 336 if( log.isDebugEnabled()) 337 log.debug(sm.getString("jaasRealm.beginLogin", username, appName)); 338 339 ClassLoader ocl = null; 341 342 if (isUseContextClassLoader()) { 343 ocl=Thread.currentThread().getContextClassLoader(); 344 Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader()); 345 } 346 347 try { 348 loginContext = new LoginContext 349 (appName, new JAASCallbackHandler(this, username, 350 credentials)); 351 } catch (Throwable e) { 352 log.error(sm.getString("jaasRealm.unexpectedError"), e); 353 return (null); 354 } finally { 355 if( isUseContextClassLoader()) { 356 Thread.currentThread().setContextClassLoader(ocl); 357 } 358 } 359 360 if( log.isDebugEnabled()) 361 log.debug("Login context created " + username); 362 363 Subject subject = null; 365 try { 366 loginContext.login(); 367 subject = loginContext.getSubject(); 368 if (subject == null) { 369 if( log.isDebugEnabled()) 370 log.debug(sm.getString("jaasRealm.failedLogin", username)); 371 return (null); 372 } 373 } catch (AccountExpiredException e) { 374 if (log.isDebugEnabled()) 375 log.debug(sm.getString("jaasRealm.accountExpired", username)); 376 return (null); 377 } catch (CredentialExpiredException e) { 378 if (log.isDebugEnabled()) 379 log.debug(sm.getString("jaasRealm.credentialExpired", username)); 380 return (null); 381 } catch (FailedLoginException e) { 382 if (log.isDebugEnabled()) 383 log.debug(sm.getString("jaasRealm.failedLogin", username)); 384 return (null); 385 } catch (LoginException e) { 386 log.warn(sm.getString("jaasRealm.loginException", username), e); 387 return (null); 388 } catch (Throwable e) { 389 log.error(sm.getString("jaasRealm.unexpectedError"), e); 390 return (null); 391 } 392 393 if( log.isDebugEnabled()) 394 log.debug(sm.getString("jaasRealm.loginContextCreated", username)); 395 396 Principal principal = createPrincipal(username, subject); 398 if (principal == null) { 399 log.debug(sm.getString("jaasRealm.authenticateFailure", username)); 400 return (null); 401 } 402 if (log.isDebugEnabled()) { 403 log.debug(sm.getString("jaasRealm.authenticateSuccess", username)); 404 } 405 406 return (principal); 407 } catch( Throwable t) { 408 log.error( "error ", t); 409 return null; 410 } 411 } 412 413 414 416 417 419 420 423 protected String getName() { 424 425 return (name); 426 427 } 428 429 430 433 protected String getPassword(String username) { 434 435 return (null); 436 437 } 438 439 440 443 protected Principal getPrincipal(String username) { 444 445 return (null); 446 447 } 448 449 450 462 protected Principal createPrincipal(String username, Subject subject) { 463 String password = null; 466 List roles = new ArrayList (); 467 Principal userPrincipal = null; 468 469 Iterator principals = subject.getPrincipals().iterator(); 471 while (principals.hasNext()) { 472 Principal principal = (Principal ) principals.next(); 473 474 String principalClass = principal.getClass().getName(); 475 476 if( log.isDebugEnabled() ) { 477 log.debug(sm.getString("jaasRealm.checkPrincipal", principal, principalClass)); 478 } 479 480 if (userPrincipal == null && userClasses.contains(principalClass)) { 481 userPrincipal = principal; 482 if( log.isDebugEnabled() ) { 483 log.debug(sm.getString("jaasRealm.userPrincipalSuccess", principal.getName())); 484 } 485 } 486 487 if (roleClasses.contains(principalClass)) { 488 roles.add(principal.getName()); 489 if( log.isDebugEnabled() ) { 490 log.debug(sm.getString("jaasRealm.rolePrincipalAdd", principal.getName())); 491 } 492 } 493 } 494 495 if (userPrincipal == null) { 497 if (log.isDebugEnabled()) { 498 log.debug(sm.getString("jaasRealm.userPrincipalFailure")); 499 log.debug(sm.getString("jaasRealm.rolePrincipalFailure")); 500 } 501 } else { 502 if (roles.size() == 0) { 503 if (log.isDebugEnabled()) { 504 log.debug(sm.getString("jaasRealm.rolePrincipalFailure")); 505 } 506 } 507 } 508 509 return new GenericPrincipal(this, username, null, roles, userPrincipal); 511 } 512 513 522 protected String makeLegalForJAAS(final String src) { 523 String result = src; 524 525 if(result == null) { 527 result = "other"; 528 } 529 530 if(result.startsWith("/")) { 533 result = result.substring(1); 534 } 535 536 return result; 537 } 538 539 540 542 543 550 public void start() throws LifecycleException { 551 552 super.start(); 554 555 } 556 557 558 564 public void stop() throws LifecycleException { 565 566 super.stop(); 568 569 } 570 571 572 } 573 | Popular Tags |