Search in sources :

Example 1 with ConstraintSecurityHandler

use of org.eclipse.jetty.security.ConstraintSecurityHandler in project camel by apache.

the class JettyTestServer method basicAuth.

private SecurityHandler basicAuth(String username, String password, String realm) {
    HashLoginService l = new HashLoginService();
    l.putUser(username, Credential.getCredential(password), new String[] { "user" });
    l.setName(realm);
    Constraint constraint = new Constraint();
    constraint.setName(Constraint.__BASIC_AUTH);
    constraint.setRoles(new String[] { "user" });
    constraint.setAuthenticate(true);
    ConstraintMapping cm = new ConstraintMapping();
    cm.setConstraint(constraint);
    cm.setPathSpec("/*");
    ConstraintSecurityHandler csh = new ConstraintSecurityHandler();
    csh.setAuthenticator(new BasicAuthenticator());
    csh.setRealmName("myrealm");
    csh.addConstraintMapping(cm);
    csh.setLoginService(l);
    return csh;
}
Also used : HashLoginService(org.eclipse.jetty.security.HashLoginService) ConstraintMapping(org.eclipse.jetty.security.ConstraintMapping) BasicAuthenticator(org.eclipse.jetty.security.authentication.BasicAuthenticator) Constraint(org.eclipse.jetty.util.security.Constraint) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler)

Example 2 with ConstraintSecurityHandler

use of org.eclipse.jetty.security.ConstraintSecurityHandler in project camel by apache.

the class HttpAuthMethodPriorityTest method getSecurityHandler.

private SecurityHandler getSecurityHandler() throws IOException {
    Constraint constraint = new Constraint(Constraint.__BASIC_AUTH, "user");
    constraint.setAuthenticate(true);
    ConstraintMapping cm = new ConstraintMapping();
    cm.setPathSpec("/*");
    cm.setConstraint(constraint);
    ConstraintSecurityHandler sh = new ConstraintSecurityHandler();
    sh.setAuthenticator(new BasicAuthenticator());
    sh.setConstraintMappings(Arrays.asList(new ConstraintMapping[] { cm }));
    HashLoginService loginService = new HashLoginService("MyRealm", "src/test/resources/myRealm.properties");
    sh.setLoginService(loginService);
    sh.setConstraintMappings(Arrays.asList(new ConstraintMapping[] { cm }));
    return sh;
}
Also used : ConstraintMapping(org.eclipse.jetty.security.ConstraintMapping) HashLoginService(org.eclipse.jetty.security.HashLoginService) BasicAuthenticator(org.eclipse.jetty.security.authentication.BasicAuthenticator) Constraint(org.eclipse.jetty.util.security.Constraint) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler)

Example 3 with ConstraintSecurityHandler

use of org.eclipse.jetty.security.ConstraintSecurityHandler in project jetty.project by eclipse.

the class ServletContextHandlerTest method testFindContainer.

@Test
public void testFindContainer() throws Exception {
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    _server.setHandler(contexts);
    ServletContextHandler root = new ServletContextHandler(contexts, "/", ServletContextHandler.SESSIONS);
    SessionHandler session = root.getSessionHandler();
    ServletHandler servlet = root.getServletHandler();
    SecurityHandler security = new ConstraintSecurityHandler();
    root.setSecurityHandler(security);
    _server.start();
    assertEquals(root, AbstractHandlerContainer.findContainerOf(_server, ContextHandler.class, session));
    assertEquals(root, AbstractHandlerContainer.findContainerOf(_server, ContextHandler.class, security));
    assertEquals(root, AbstractHandlerContainer.findContainerOf(_server, ContextHandler.class, servlet));
}
Also used : SessionHandler(org.eclipse.jetty.server.session.SessionHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) SecurityHandler(org.eclipse.jetty.security.SecurityHandler) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) Test(org.junit.Test)

Example 4 with ConstraintSecurityHandler

use of org.eclipse.jetty.security.ConstraintSecurityHandler in project jetty.project by eclipse.

the class DigestPostTest method setUpServer.

@BeforeClass
public static void setUpServer() {
    try {
        _server = new Server();
        _server.setConnectors(new Connector[] { new ServerConnector(_server) });
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SECURITY);
        context.setContextPath("/test");
        context.addServlet(PostServlet.class, "/");
        TestLoginService realm = new TestLoginService("test");
        realm.putUser("testuser", new Password("password"), new String[] { "test" });
        _server.addBean(realm);
        ConstraintSecurityHandler security = (ConstraintSecurityHandler) context.getSecurityHandler();
        security.setAuthenticator(new DigestAuthenticator());
        security.setLoginService(realm);
        Constraint constraint = new Constraint("SecureTest", "test");
        constraint.setAuthenticate(true);
        ConstraintMapping mapping = new ConstraintMapping();
        mapping.setConstraint(constraint);
        mapping.setPathSpec("/*");
        security.setConstraintMappings(Collections.singletonList(mapping));
        HandlerCollection handlers = new HandlerCollection();
        handlers.setHandlers(new Handler[] { context, new DefaultHandler() });
        _server.setHandler(handlers);
        _server.start();
    } catch (final Exception e) {
        e.printStackTrace();
    }
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) ConstraintMapping(org.eclipse.jetty.security.ConstraintMapping) Server(org.eclipse.jetty.server.Server) DigestAuthenticator(org.eclipse.jetty.security.authentication.DigestAuthenticator) Constraint(org.eclipse.jetty.util.security.Constraint) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) IOException(java.io.IOException) Password(org.eclipse.jetty.util.security.Password) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) BeforeClass(org.junit.BeforeClass)

Example 5 with ConstraintSecurityHandler

use of org.eclipse.jetty.security.ConstraintSecurityHandler in project jetty.project by eclipse.

the class HttpClientAuthenticationTest method start.

private void start(Authenticator authenticator, Handler handler) throws Exception {
    server = new Server();
    File realmFile = MavenTestingUtils.getTestResourceFile("realm.properties");
    LoginService loginService = new HashLoginService(realm, realmFile.getAbsolutePath());
    server.addBean(loginService);
    ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();
    Constraint constraint = new Constraint();
    constraint.setAuthenticate(true);
    //allow any authenticated user
    constraint.setRoles(new String[] { "**" });
    ConstraintMapping mapping = new ConstraintMapping();
    mapping.setPathSpec("/secure");
    mapping.setConstraint(constraint);
    securityHandler.addConstraintMapping(mapping);
    securityHandler.setAuthenticator(authenticator);
    securityHandler.setLoginService(loginService);
    securityHandler.setHandler(handler);
    start(securityHandler);
}
Also used : HashLoginService(org.eclipse.jetty.security.HashLoginService) ConstraintMapping(org.eclipse.jetty.security.ConstraintMapping) Server(org.eclipse.jetty.server.Server) Constraint(org.eclipse.jetty.util.security.Constraint) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) File(java.io.File) HashLoginService(org.eclipse.jetty.security.HashLoginService) LoginService(org.eclipse.jetty.security.LoginService)

Aggregations

ConstraintSecurityHandler (org.eclipse.jetty.security.ConstraintSecurityHandler)24 ConstraintMapping (org.eclipse.jetty.security.ConstraintMapping)19 Constraint (org.eclipse.jetty.util.security.Constraint)19 HashLoginService (org.eclipse.jetty.security.HashLoginService)12 BasicAuthenticator (org.eclipse.jetty.security.authentication.BasicAuthenticator)11 Server (org.eclipse.jetty.server.Server)10 ServerConnector (org.eclipse.jetty.server.ServerConnector)5 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)5 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)4 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)4 Password (org.eclipse.jetty.util.security.Password)4 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 Handler (org.eclipse.jetty.server.Handler)3 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)3 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)3 HandlerList (org.eclipse.jetty.server.handler.HandlerList)3 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)3 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)3 File (java.io.File)2