Search in sources :

Example 16 with ConstraintSecurityHandler

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

the class TestServletAnnotations method testDeclareRoles.

@Test
public void testDeclareRoles() throws Exception {
    WebAppContext wac = new WebAppContext();
    ConstraintSecurityHandler sh = new ConstraintSecurityHandler();
    wac.setSecurityHandler(sh);
    sh.setRoles(new HashSet<String>(Arrays.asList(new String[] { "humpty", "dumpty" })));
    DeclareRolesAnnotationHandler handler = new DeclareRolesAnnotationHandler(wac);
    handler.doHandle(ServletC.class);
    assertTrue(sh.getRoles().contains("alice"));
    assertTrue(sh.getRoles().contains("humpty"));
    assertTrue(sh.getRoles().contains("dumpty"));
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) Test(org.junit.Test)

Example 17 with ConstraintSecurityHandler

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

the class SecuredHelloHandler method main.

public static void main(String[] args) throws Exception {
    // Create a basic jetty server object that will listen on port 8080.
    // Note that if you set this to port 0 then a randomly available port
    // will be assigned that you can either look in the logs for the port,
    // or programmatically obtain it for use in test cases.
    Server server = new Server(8080);
    // Since this example is for our test webapp, we need to setup a
    // LoginService so this shows how to create a very simple hashmap based
    // one. The name of the LoginService needs to correspond to what is
    // configured a webapp's web.xml and since it has a lifecycle of its own
    // we register it as a bean with the Jetty server object so it can be
    // started and stopped according to the lifecycle of the server itself.
    // In this example the name can be whatever you like since we are not
    // dealing with webapp realms.
    LoginService loginService = new HashLoginService("MyRealm", "src/test/resources/realm.properties");
    server.addBean(loginService);
    // A security handler is a jetty handler that secures content behind a
    // particular portion of a url space. The ConstraintSecurityHandler is a
    // more specialized handler that allows matching of urls to different
    // constraints. The server sets this as the first handler in the chain,
    // effectively applying these constraints to all subsequent handlers in
    // the chain.
    ConstraintSecurityHandler security = new ConstraintSecurityHandler();
    server.setHandler(security);
    // This constraint requires authentication and in addition that an
    // authenticated user be a member of a given set of roles for
    // authorization purposes.
    Constraint constraint = new Constraint();
    constraint.setName("auth");
    constraint.setAuthenticate(true);
    constraint.setRoles(new String[] { "user", "admin" });
    // Binds a url pattern with the previously created constraint. The roles
    // for this constraing mapping are mined from the Constraint itself
    // although methods exist to declare and bind roles separately as well.
    ConstraintMapping mapping = new ConstraintMapping();
    mapping.setPathSpec("/*");
    mapping.setConstraint(constraint);
    // First you see the constraint mapping being applied to the handler as
    // a singleton list, however you can passing in as many security
    // constraint mappings as you like so long as they follow the mapping
    // requirements of the servlet api. Next we set a BasicAuthenticator
    // instance which is the object that actually checks the credentials
    // followed by the LoginService which is the store of known users, etc.
    security.setConstraintMappings(Collections.singletonList(mapping));
    security.setAuthenticator(new BasicAuthenticator());
    security.setLoginService(loginService);
    // The Hello Handler is the handler we are securing so we create one,
    // and then set it as the handler on the
    // security handler to complain the simple handler chain.
    HelloHandler hh = new HelloHandler();
    // chain the hello handler into the security handler
    security.setHandler(hh);
    // Start things up!
    server.start();
    // The use of server.join() the will make the current thread join and
    // wait until the server is done executing.
    // See
    // http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join()
    server.join();
}
Also used : HashLoginService(org.eclipse.jetty.security.HashLoginService) ConstraintMapping(org.eclipse.jetty.security.ConstraintMapping) Server(org.eclipse.jetty.server.Server) BasicAuthenticator(org.eclipse.jetty.security.authentication.BasicAuthenticator) Constraint(org.eclipse.jetty.util.security.Constraint) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) HashLoginService(org.eclipse.jetty.security.HashLoginService) LoginService(org.eclipse.jetty.security.LoginService)

Example 18 with ConstraintSecurityHandler

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

the class JaspiTest method before.

@Before
public void before() throws Exception {
    System.setProperty("org.apache.geronimo.jaspic.configurationFile", "src/test/resources/jaspi.xml");
    _server = new Server();
    _connector = new LocalConnector(_server);
    _server.addConnector(_connector);
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    _server.setHandler(contexts);
    TestLoginService loginService = new TestLoginService("TestRealm");
    loginService.putUser("user", new Password("password"), new String[] { "users" });
    loginService.putUser("admin", new Password("secret"), new String[] { "users", "admins" });
    _server.addBean(loginService);
    ContextHandler context = new ContextHandler();
    contexts.addHandler(context);
    context.setContextPath("/ctx");
    JaspiAuthenticatorFactory jaspiAuthFactory = new JaspiAuthenticatorFactory();
    ConstraintSecurityHandler security = new ConstraintSecurityHandler();
    context.setHandler(security);
    security.setAuthenticatorFactory(jaspiAuthFactory);
    // security.setAuthenticator(new BasicAuthenticator());
    Constraint constraint = new Constraint("All", "users");
    constraint.setAuthenticate(true);
    ConstraintMapping mapping = new ConstraintMapping();
    mapping.setPathSpec("/jaspi/*");
    mapping.setConstraint(constraint);
    security.addConstraintMapping(mapping);
    TestHandler handler = new TestHandler();
    security.setHandler(handler);
    ContextHandler other = new ContextHandler();
    contexts.addHandler(other);
    other.setContextPath("/other");
    ConstraintSecurityHandler securityOther = new ConstraintSecurityHandler();
    other.setHandler(securityOther);
    securityOther.setAuthenticatorFactory(jaspiAuthFactory);
    securityOther.addConstraintMapping(mapping);
    securityOther.setHandler(new TestHandler());
    _server.start();
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) 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) LocalConnector(org.eclipse.jetty.server.LocalConnector) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) Password(org.eclipse.jetty.util.security.Password) Before(org.junit.Before)

Example 19 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 20 with ConstraintSecurityHandler

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

the class HttpBasicAuthComponentConfiguredTest 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)

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