|
From: Sparkletron <ssk...@cl...> - 2007-09-07 23:40:32
|
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.security.Constraint;
import org.mortbay.jetty.security.ConstraintMapping;
import org.mortbay.jetty.security.HashUserRealm;
import org.mortbay.jetty.security.SecurityHandler;
import org.mortbay.jetty.security.UserRealm;
import org.mortbay.jetty.servlet.ServletHandler;
public final class Test {
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
ServletHandler servletHandler = new ServletHandler();
servletHandler.addServletWithMapping(Test.TestServlet.class, "/");
Constraint constraint = new Constraint();
constraint.setName(Constraint.__BASIC_AUTH);
constraint.setRoles(new String[] { "admin" });
constraint.setAuthenticate(true);
constraint.isForbidden();
ConstraintMapping constraintMapping = new ConstraintMapping();
constraintMapping.setConstraint(constraint);
constraintMapping.setPathSpec("/*");
HashUserRealm hashUserRealm = new HashUserRealm();
hashUserRealm.setName("Test");
hashUserRealm.put("test", "test");
hashUserRealm.addUserToRole("test", "admin");
SecurityHandler securityHandler = new SecurityHandler();
securityHandler.setUserRealm(hashUserRealm);
securityHandler.setConstraintMappings(new ConstraintMapping[] {
constraintMapping });
server.setUserRealms(new UserRealm[] { hashUserRealm });
// You add a SecurityHandler...
server.addHandler(securityHandler);
// Yet you set a ServletHandler? That's confusing.
server.setHandler(servletHandler);
server.start();
}
public static class TestServlet extends HttpServlet {
public void doGet(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) throws ServletException,
IOException {
System.out.println("Did you successfully authenticate prior to seeing
me?");
}
}
}
--
View this message in context: http://www.nabble.com/Embedded-servlet-w--basic-auth-tf3177608.html#a12565037
Sent from the Jetty Support mailing list archive at Nabble.com.
|