1 22 package org.jboss.test.web.servlets; 23 24 import java.io.IOException ; 25 import javax.servlet.Filter ; 26 import javax.servlet.FilterConfig ; 27 import javax.servlet.ServletException ; 28 import javax.servlet.ServletRequest ; 29 import javax.servlet.ServletResponse ; 30 import javax.servlet.FilterChain ; 31 import javax.servlet.http.HttpServletResponse ; 32 import javax.security.auth.Subject ; 33 import javax.naming.NamingException ; 34 import javax.naming.InitialContext ; 35 36 import org.jboss.security.SubjectSecurityManager; 37 38 42 public class SubjectFilter implements Filter 43 { 44 public void init(FilterConfig filterConfig) throws ServletException 45 { 46 47 } 48 49 public void doFilter(ServletRequest request, ServletResponse response, 50 FilterChain filterChain) 51 throws IOException , ServletException 52 { 53 HttpServletResponse httpResponse = (HttpServletResponse ) response; 54 try 55 { 56 Subject userSubject = getActiveSubject(httpResponse); 57 if (userSubject == null) 58 throw new ServletException ("Active subject was null"); 59 } 60 catch (NamingException e) 61 { 62 throw new ServletException ("Failed to lookup active subject", e); 63 } 64 filterChain.doFilter(request, response); 65 } 66 67 public void destroy() 68 { 69 } 70 71 protected Subject getActiveSubject(HttpServletResponse httpResponse) 72 throws NamingException 73 { 74 InitialContext ctx = new InitialContext (); 75 SubjectSecurityManager mgr = (SubjectSecurityManager) ctx.lookup("java:comp/env/security/securityMgr"); 76 Subject s0 = mgr.getActiveSubject(); 77 httpResponse.addHeader("X-SubjectFilter-SubjectSecurityManager", s0.toString()); 78 Subject s1 = (Subject ) ctx.lookup("java:comp/env/security/subject"); 79 httpResponse.addHeader("X-SubjectFilter-ENC", s1.toString()); 80 return s1; 81 } 82 } 83 | Popular Tags |