Search in sources :

Example 71 with Constraint

use of org.eclipse.jetty.util.security.Constraint in project airlift by airlift.

the class HttpServer method createSecurityHandler.

private static SecurityHandler createSecurityHandler(LoginService loginService) {
    Constraint constraint = new Constraint();
    constraint.setAuthenticate(false);
    ConstraintMapping constraintMapping = new ConstraintMapping();
    constraintMapping.setConstraint(constraint);
    constraintMapping.setPathSpec("/*");
    ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();
    securityHandler.setLoginService(loginService);
    // TODO: support for other auth schemes (digest, etc)
    securityHandler.setAuthenticator(new BasicAuthenticator());
    securityHandler.setConstraintMappings(ImmutableList.of(constraintMapping));
    return securityHandler;
}
Also used : 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 72 with Constraint

use of org.eclipse.jetty.util.security.Constraint in project EventHub by Codecademy.

the class EventHubHandler method main.

public static void main(String[] args) throws Exception {
    Properties properties = new Properties();
    properties.load(EventHub.class.getClassLoader().getResourceAsStream("hub.properties"));
    properties.load(EventHubHandler.class.getClassLoader().getResourceAsStream("web.properties"));
    properties.putAll(System.getProperties());
    Injector injector = Guice.createInjector(Modules.override(new DmaIdListModule(), new DatedEventIndexModule(), new ShardedEventIndexModule(), new PropertiesIndexModule(), new UserEventIndexModule(), new EventStorageModule(), new UserStorageModule(), new EventHubModule(properties)).with(new Module()));
    final EventHubHandler eventHubHandler = injector.getInstance(EventHubHandler.class);
    int port = injector.getInstance(Key.get(Integer.class, Names.named("eventhubhandler.port")));
    final Server server = new Server(port);
    @SuppressWarnings("ConstantConditions") String webDir = EventHubHandler.class.getClassLoader().getResource("frontend").toExternalForm();
    HashLoginService loginService = new HashLoginService();
    loginService.putUser(properties.getProperty("eventhubhandler.username"), new Password(properties.getProperty("eventhubhandler.password")), new String[] { "user" });
    server.addBean(loginService);
    ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();
    Constraint constraint = new Constraint();
    constraint.setName("auth");
    constraint.setAuthenticate(true);
    constraint.setRoles(new String[] { "user", "admin" });
    ConstraintMapping mapping = new ConstraintMapping();
    mapping.setPathSpec("/*");
    mapping.setConstraint(constraint);
    securityHandler.setConstraintMappings(Collections.singletonList(mapping));
    securityHandler.setAuthenticator(new BasicAuthenticator());
    securityHandler.setLoginService(loginService);
    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setDirectoriesListed(false);
    resourceHandler.setWelcomeFiles(new String[] { "main.html" });
    resourceHandler.setResourceBase(webDir);
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { new JsonpCallbackHandler(eventHubHandler), securityHandler });
    server.setHandler(handlers);
    securityHandler.setHandler(resourceHandler);
    server.start();
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

        @Override
        public void run() {
            if (server.isStarted()) {
                try {
                    server.stop();
                    eventHubHandler.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }, "Stop Jetty Hook"));
    server.join();
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) UserEventIndexModule(com.codecademy.eventhub.index.UserEventIndexModule) Server(org.eclipse.jetty.server.Server) Constraint(org.eclipse.jetty.util.security.Constraint) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) Properties(java.util.Properties) HashLoginService(org.eclipse.jetty.security.HashLoginService) BasicAuthenticator(org.eclipse.jetty.security.authentication.BasicAuthenticator) Injector(com.google.inject.Injector) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) DatedEventIndexModule(com.codecademy.eventhub.index.DatedEventIndexModule) Password(org.eclipse.jetty.util.security.Password) ConstraintMapping(org.eclipse.jetty.security.ConstraintMapping) PropertiesIndexModule(com.codecademy.eventhub.index.PropertiesIndexModule) EventHubModule(com.codecademy.eventhub.EventHubModule) Constraint(org.eclipse.jetty.util.security.Constraint) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) DmaIdListModule(com.codecademy.eventhub.list.DmaIdListModule) ShardedEventIndexModule(com.codecademy.eventhub.index.ShardedEventIndexModule) EventStorageModule(com.codecademy.eventhub.storage.EventStorageModule) UserStorageModule(com.codecademy.eventhub.storage.UserStorageModule) DatedEventIndexModule(com.codecademy.eventhub.index.DatedEventIndexModule) UserEventIndexModule(com.codecademy.eventhub.index.UserEventIndexModule) PropertiesIndexModule(com.codecademy.eventhub.index.PropertiesIndexModule) EventHubModule(com.codecademy.eventhub.EventHubModule) DmaIdListModule(com.codecademy.eventhub.list.DmaIdListModule) UserStorageModule(com.codecademy.eventhub.storage.UserStorageModule) ShardedEventIndexModule(com.codecademy.eventhub.index.ShardedEventIndexModule) EventStorageModule(com.codecademy.eventhub.storage.EventStorageModule)

Example 73 with Constraint

use of org.eclipse.jetty.util.security.Constraint in project Manga by herrlock.

the class JettyServer method createShutdownHandler.

private Handler createShutdownHandler() {
    String pwConfig = new File("conf/jetty-users.dsc").getAbsolutePath();
    HashLoginService loginService = new HashLoginService("MangaDownloader-Realm", pwConfig);
    this.server.addBean(loginService, true);
    ConstraintSecurityHandler secHandler = new ConstraintSecurityHandler();
    secHandler.setAuthenticator(new BasicAuthenticator());
    ConstraintMapping cm = new ConstraintMapping();
    final Constraint userConstraint = new Constraint(Constraint.__BASIC_AUTH, "**");
    userConstraint.setAuthenticate(true);
    cm.setConstraint(userConstraint);
    cm.setPathSpec("/shutdown/*");
    secHandler.setConstraintMappings(Arrays.asList(cm));
    secHandler.setHandler(new ShutdownHandler(SUPER_SECRET_MAGIC_TOKEN_FOR_SHUTDOWN, false, true));
    secHandler.setLoginService(loginService);
    return secHandler;
}
Also used : HashLoginService(org.eclipse.jetty.security.HashLoginService) ConstraintMapping(org.eclipse.jetty.security.ConstraintMapping) BasicAuthenticator(org.eclipse.jetty.security.authentication.BasicAuthenticator) ShutdownHandler(org.eclipse.jetty.server.handler.ShutdownHandler) Constraint(org.eclipse.jetty.util.security.Constraint) ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) File(java.io.File)

Example 74 with Constraint

use of org.eclipse.jetty.util.security.Constraint in project alluxio by Alluxio.

the class WebServer method disableMethod.

/**
 * @param method to disable
 */
private void disableMethod(String method) {
    Constraint constraint = new Constraint();
    constraint.setAuthenticate(true);
    constraint.setName("Disable " + method);
    ConstraintMapping disableMapping = new ConstraintMapping();
    disableMapping.setConstraint(constraint);
    disableMapping.setMethod(method.toUpperCase());
    disableMapping.setPathSpec("/");
    mSecurityHandler.addConstraintMapping(disableMapping);
}
Also used : ConstraintMapping(org.eclipse.jetty.security.ConstraintMapping) Constraint(org.eclipse.jetty.util.security.Constraint)

Example 75 with Constraint

use of org.eclipse.jetty.util.security.Constraint in project jena by apache.

the class FusekiTestAuth method makeSimpleSecurityHandler.

/**
 * Create a Jetty {@link SecurityHandler} for basic authentication, one user/password/role.
 */
public static SecurityHandler makeSimpleSecurityHandler(String pathSpec, String realm, String user, String password, String role) {
    Objects.requireNonNull(user);
    Objects.requireNonNull(password);
    Objects.requireNonNull(role);
    Constraint constraint = new Constraint();
    constraint.setName(Constraint.__BASIC_AUTH);
    String[] roles = new String[] { role };
    constraint.setRoles(roles);
    constraint.setAuthenticate(true);
    ConstraintMapping mapping = new ConstraintMapping();
    mapping.setConstraint(constraint);
    mapping.setPathSpec("/*");
    IdentityService identService = new DefaultIdentityService();
    ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();
    securityHandler.addConstraintMapping(mapping);
    securityHandler.setIdentityService(identService);
    UserStore userStore = JettyLib.makeUserStore(user, password, role);
    HashLoginService loginService = new HashLoginService("Fuseki Authentication");
    loginService.setUserStore(userStore);
    loginService.setIdentityService(identService);
    securityHandler.setLoginService(loginService);
    securityHandler.setAuthenticator(new BasicAuthenticator());
    if (realm != null)
        securityHandler.setRealmName(realm);
    return securityHandler;
}
Also used : BasicAuthenticator(org.eclipse.jetty.security.authentication.BasicAuthenticator) Constraint(org.eclipse.jetty.util.security.Constraint)

Aggregations

Constraint (org.eclipse.jetty.util.security.Constraint)78 ConstraintMapping (org.eclipse.jetty.security.ConstraintMapping)46 ConstraintSecurityHandler (org.eclipse.jetty.security.ConstraintSecurityHandler)34 BasicAuthenticator (org.eclipse.jetty.security.authentication.BasicAuthenticator)27 HashLoginService (org.eclipse.jetty.security.HashLoginService)20 Test (org.junit.Test)15 Server (org.eclipse.jetty.server.Server)13 ArrayList (java.util.ArrayList)9 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)8 Password (org.eclipse.jetty.util.security.Password)7 HashSet (java.util.HashSet)6 File (java.io.File)5 IOException (java.io.IOException)5 ResourceHandler (org.eclipse.jetty.server.handler.ResourceHandler)5 LoginService (org.eclipse.jetty.security.LoginService)4 ServerConnector (org.eclipse.jetty.server.ServerConnector)4 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)4 HandlerList (org.eclipse.jetty.server.handler.HandlerList)4 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)4 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)4