1 16 package org.mortbay.http.handler; 17 18 import java.io.IOException ; 19 20 import org.apache.commons.logging.Log; 21 import org.mortbay.log.LogFactory; 22 import org.mortbay.http.BasicAuthenticator; 23 import org.mortbay.http.ClientCertAuthenticator; 24 import org.mortbay.http.HttpException; 25 import org.mortbay.http.HttpRequest; 26 import org.mortbay.http.HttpResponse; 27 import org.mortbay.http.SecurityConstraint; 28 29 30 35 public class SecurityHandler extends AbstractHttpHandler 36 { 37 private static Log log = LogFactory.getLog(SecurityHandler.class); 38 39 40 private String _authMethod=SecurityConstraint.__BASIC_AUTH; 41 42 43 public String getAuthMethod() 44 { 45 return _authMethod; 46 } 47 48 49 public void setAuthMethod(String method) 50 { 51 if (isStarted() && _authMethod!=null && !_authMethod.equals(method)) 52 throw new IllegalStateException ("Handler started"); 53 _authMethod = method; 54 } 55 56 57 public void start() 58 throws Exception 59 { 60 if (getHttpContext().getAuthenticator()==null) 61 { 62 if (SecurityConstraint.__BASIC_AUTH.equalsIgnoreCase(_authMethod)) 64 getHttpContext().setAuthenticator(new BasicAuthenticator()); 65 else if (SecurityConstraint.__CERT_AUTH.equalsIgnoreCase(_authMethod)) 66 getHttpContext().setAuthenticator(new ClientCertAuthenticator()); 67 else 68 log.warn("Unknown Authentication method:"+_authMethod); 69 } 70 71 super.start(); 72 } 73 74 75 public void handle(String pathInContext, 76 String pathParams, 77 HttpRequest request, 78 HttpResponse response) 79 throws HttpException, IOException 80 { 81 getHttpContext().checkSecurityConstraints(pathInContext,request,response); 82 } 83 84 } 85 86 | Popular Tags |