1 18 package org.apache.beehive.netui.tomcat; 19 20 import org.apache.catalina.*; 21 import org.apache.catalina.deploy.SecurityConstraint; 22 import org.apache.catalina.authenticator.BasicAuthenticator; 23 import org.apache.catalina.authenticator.Constants; 24 25 import javax.servlet.ServletException ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpSession ; 28 import javax.security.auth.login.LoginException ; 29 import javax.security.auth.login.FailedLoginException ; 30 import java.io.IOException ; 31 import java.security.Principal ; 32 33 39 public class PageflowValve extends BasicAuthenticator 40 { 41 public void invoke(Request request, Response response, ValveContext valveContext) 42 throws IOException , ServletException 43 { 44 if (!(request instanceof HttpRequest) || 46 !(response instanceof HttpResponse)) { 47 valveContext.invokeNext(request, response); 48 return; 49 } 50 51 if (cache) { 54 Principal principal = 55 ((HttpServletRequest ) request.getRequest()).getUserPrincipal(); 56 if (principal == null) { 57 Session session = getSession((HttpRequest)request); 58 if (session != null) { 59 principal = session.getPrincipal(); 60 if (principal != null) { 61 if (debug >= 1) 62 log("We have cached auth type " + 63 session.getAuthType() + 64 " for principal " + 65 session.getPrincipal()); 66 ((HttpRequest)request).setAuthType(session.getAuthType()); 67 ((HttpRequest)request).setUserPrincipal(principal); 68 } 69 } 70 } 71 } 72 73 HttpServletRequest hreq = 75 (HttpServletRequest ) request.getRequest(); 76 PageflowHelper helper = new PageflowHelperImpl(); 77 ((PageflowHelperImpl)helper).initRequest( (HttpRequest)request, (HttpResponse)response, this ); 78 hreq.setAttribute( PageflowHelper.PAGEFLOW_HELPER_KEY, helper ); 79 80 valveContext.invokeNext(request, response); 81 } 82 83 void login( String username, String password, HttpRequest request, HttpResponse response ) 84 throws LoginException 85 { 86 Principal principal = context.getRealm().authenticate(username, password); 88 if (principal != null) 89 { 90 register(request, response, principal, Constants.BASIC_METHOD, 91 username, password); 92 return; 93 } 94 95 throw new FailedLoginException ( "Page Flow login failed: " + username ); } 97 98 void logout( boolean invalidateSessions, HttpRequest request, HttpResponse response ) 99 { 100 if ( invalidateSessions ) 101 { 102 HttpServletRequest hreq = 105 (HttpServletRequest ) request.getRequest(); 106 PageflowHelper pfh = (PageflowHelper)hreq.getAttribute( PageflowHelper.PAGEFLOW_HELPER_KEY ); 107 HttpSession session = hreq.getSession(false); 108 if ( session != null ) 109 session.invalidate(); 110 if ( pfh != null ) 111 hreq.setAttribute( PageflowHelper.PAGEFLOW_HELPER_KEY, pfh ); 112 } 113 register( request, response, null, null, null, null ); 114 } 115 116 125 public boolean checkSecurity( HttpRequest request, HttpResponse response, SecurityConstraint constraint ) 126 throws IOException 127 { 128 return ! authenticate( request, response, null ); 130 } 131 132 133 Context getContext() 134 { 135 return context; 136 } 137 } 138 | Popular Tags |