1 22 package org.jboss.aspects.security; 23 24 import java.security.Principal ; 25 import java.security.GeneralSecurityException ; 26 import javax.security.auth.Subject ; 27 import org.jboss.logging.Logger; 28 import org.jboss.security.AuthenticationManager; 29 import org.jboss.security.RunAsIdentity; 30 31 38 public class AuthenticationInterceptor implements org.jboss.aop.advice.Interceptor 39 { 40 protected Logger log = Logger.getLogger(this.getClass()); 41 protected AuthenticationManager authenticationManager; 42 43 public AuthenticationInterceptor(AuthenticationManager manager) 44 { 45 authenticationManager = manager; 46 } 47 48 public String getName() 49 { 50 return "AuthenticationInterceptor"; 51 } 52 53 protected void handleGeneralSecurityException(GeneralSecurityException gse) 54 { 55 throw new SecurityException (gse.getMessage()); 56 } 57 58 62 public Object invoke(org.jboss.aop.joinpoint.Invocation invocation) throws Throwable 63 { 64 try 65 { 66 authenticate(invocation); 67 } 68 catch (GeneralSecurityException gse) 69 { 70 handleGeneralSecurityException(gse); 71 } 72 73 Object oldDomain = SecurityContext.currentDomain.get(); 74 try 75 { 76 SecurityContext.currentDomain.set(authenticationManager); 77 return invocation.invokeNext(); 78 } 79 finally 80 { 81 SecurityContext.currentDomain.set(oldDomain); 82 83 RunAsIdentity callerRunAsIdentity = SecurityActions.peekRunAsIdentity(); 86 if (authenticationManager == null || callerRunAsIdentity == null) 87 { 88 SecurityActions.popSubjectContext(); 89 } 90 if(authenticationManager != null) 91 SecurityActions.clearSecurityContext(authenticationManager.getSecurityDomain()); 92 93 if (invocation.getMetaData("security", "principal") != null) 94 { 95 SecurityActions.setPrincipal(null); 96 SecurityActions.setCredential(null); 97 } 98 } 99 } 100 101 protected void authenticate(org.jboss.aop.joinpoint.Invocation invocation) throws Exception 102 { 103 Principal principal = (Principal ) invocation.getMetaData("security", "principal"); 104 Object credential = invocation.getMetaData("security", "credential"); 105 106 if (principal == null) 107 { 108 principal = SecurityActions.getPrincipal(); 109 } 110 if (credential == null) 111 { 112 credential = SecurityActions.getCredential(); 113 } 114 115 if (authenticationManager == null) 116 { 117 SecurityActions.pushSubjectContext(principal, credential, null); 118 return; 119 } 120 121 122 RunAsIdentity callerRunAsIdentity = SecurityActions.peekRunAsIdentity(); 124 if (callerRunAsIdentity == null) 125 { 126 Subject subject = new Subject (); 128 if (authenticationManager.isValid(principal, credential, subject) == false) 129 { 130 135 Exception ex = SecurityActions.getContextException(); 137 if (ex != null) 138 throw ex; 139 String msg = "Authentication exception, principal=" + principal; 141 SecurityException e = new SecurityException (msg); 142 throw e; 143 } 144 else 145 { 146 SecurityActions.pushSubjectContext(principal, credential, subject); 147 SecurityActions.establishSecurityContext(authenticationManager.getSecurityDomain(), 148 principal, credential, subject); 149 if (log.isTraceEnabled()) 150 { 151 log.trace("Authenticated principal=" + principal); 152 } 153 } 154 } 155 } 156 } 157 | Popular Tags |