1 22 package org.jboss.web.tomcat.security; 23 24 import java.io.IOException ; 25 import java.security.Principal ; 26 import javax.naming.InitialContext ; 27 import javax.naming.NamingException ; 28 import javax.servlet.ServletException ; 29 import javax.servlet.http.HttpSession ; 30 import javax.security.auth.Subject ; 31 32 import org.apache.catalina.Session; 33 import org.apache.catalina.Wrapper; 34 import org.apache.catalina.Manager; 35 import org.apache.catalina.connector.Request; 36 import org.apache.catalina.connector.Response; 37 import org.apache.catalina.valves.ValveBase; 38 import org.jboss.logging.Logger; 39 import org.jboss.metadata.WebMetaData; 40 import org.jboss.security.AuthenticationManager; 41 import org.jboss.security.RunAsIdentity; 42 import org.jboss.security.plugins.JaasSecurityManagerServiceMBean; 43 44 52 public class SecurityAssociationValve extends ValveBase 53 { 54 private static Logger log = Logger.getLogger(SecurityAssociationValve.class); 55 public static ThreadLocal userPrincipal = new ThreadLocal (); 56 57 public static ThreadLocal activeWebMetaData = new ThreadLocal (); 58 59 60 private WebMetaData metaData; 61 62 private String subjectAttributeName = null; 63 64 private JaasSecurityManagerServiceMBean secMgrService; 65 private boolean trace; 66 67 public SecurityAssociationValve(WebMetaData metaData, 68 JaasSecurityManagerServiceMBean secMgrService) 69 { 70 this.metaData = metaData; 71 this.secMgrService = secMgrService; 72 this.trace = log.isTraceEnabled(); 73 } 74 75 80 public void setSubjectAttributeName(String subjectAttributeName) 81 { 82 this.subjectAttributeName = subjectAttributeName; 83 if (subjectAttributeName != null && subjectAttributeName.length() == 0) 84 this.subjectAttributeName = null; 85 } 86 87 public void invoke(Request request, Response response) 88 throws IOException , ServletException 89 { 90 Session session = null; 91 Principal caller = request.getPrincipal(); 94 JBossGenericPrincipal principal = null; 96 HttpSession hsession = request.getSession(false); 97 98 if( trace ) 99 log.trace("Begin invoke, caller"+caller); 100 activeWebMetaData.set(metaData); 102 try 103 { 104 try 105 { 106 Wrapper servlet = request.getWrapper(); 107 if (servlet != null) 108 { 109 String name = servlet.getName(); 110 RunAsIdentity identity = metaData.getRunAsIdentity(name); 111 if (identity != null) 112 { 113 if (trace) 114 log.trace(name + ", runAs: " + identity); 115 } 116 SecurityAssociationActions.pushRunAsIdentity(identity); 117 } 118 userPrincipal.set(caller); 119 120 Manager manager = container.getManager(); 122 if (manager != null && hsession != null) 123 { 124 try 125 { 126 session = manager.findSession(hsession.getId()); 127 } 128 catch (IOException ignore) 129 { 130 } 131 } 132 133 if (caller == null || (caller instanceof JBossGenericPrincipal) == false) 134 { 135 if (session != null) 137 { 138 principal = 139 (JBossGenericPrincipal) session.getPrincipal(); 140 } 141 } 142 else 143 { 144 principal = (JBossGenericPrincipal) caller; 146 } 147 148 if (principal != null) 150 { 151 if (trace) 152 log.trace("Restoring principal info from cache"); 153 SecurityAssociationActions.setPrincipalInfo(principal.getAuthPrincipal(), 154 principal.getCredentials(), principal.getSubject()); 155 } 156 if (subjectAttributeName != null) 158 { 159 javax.naming.Context securityCtx = getSecurityContext(); 160 if (securityCtx != null) 161 { 162 AuthenticationManager securityMgr = (AuthenticationManager) securityCtx.lookup("securityMgr"); 164 Subject subject = securityMgr.getActiveSubject(); 165 request.getRequest().setAttribute(subjectAttributeName, subject); 166 } 167 } 168 } 169 catch (Throwable e) 170 { 171 log.debug("Failed to determine servlet", e); 172 } 173 getNext().invoke(request, response); 175 SecurityAssociationActions.popRunAsIdentity(); 176 177 180 if( secMgrService != null && 181 session != null && session.isValid() == false && 182 metaData.isFlushOnSessionInvalidation() == true ) 183 { 184 if( principal != null ) 185 { 186 String securityDomain = metaData.getSecurityDomain(); 187 if (trace) 188 { 189 log.trace("Session is invalid, security domain: "+securityDomain 190 +", user="+principal); 191 } 192 try 193 { 194 Principal authPrincipal = principal.getAuthPrincipal(); 195 secMgrService.flushAuthenticationCache(securityDomain, authPrincipal); 196 } 197 catch(Exception e) 198 { 199 log.debug("Failed to flush auth cache", e); 200 } 201 } 202 } 203 } 204 finally 205 { 206 if( trace ) 207 log.trace("End invoke, caller"+caller); 208 activeWebMetaData.set(null); 209 userPrincipal.set(null); 210 } 211 } 212 213 private javax.naming.Context getSecurityContext() 214 { 215 javax.naming.Context securityCtx = null; 216 try 218 { 219 InitialContext iniCtx = new InitialContext (); 220 securityCtx = (javax.naming.Context ) iniCtx.lookup("java:comp/env/security"); 221 } 222 catch (NamingException e) 223 { 224 } 226 return securityCtx; 227 } 228 } 229 | Popular Tags |