Search in sources :

Example 36 with BasicAuthenticator

use of org.eclipse.jetty.security.authentication.BasicAuthenticator 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)

Example 37 with BasicAuthenticator

use of org.eclipse.jetty.security.authentication.BasicAuthenticator in project calcite-avatica by apache.

the class HttpServer method configureBasicAuthentication.

protected ConstraintSecurityHandler configureBasicAuthentication(Server server, AvaticaServerConfiguration config) {
    final String[] allowedRoles = config.getAllowedRoles();
    final String realm = config.getHashLoginServiceRealm();
    final String loginServiceProperties = config.getHashLoginServiceProperties();
    HashLoginService loginService = new HashLoginService(realm, loginServiceProperties);
    server.addBean(loginService);
    return configureCommonAuthentication(Constraint.__BASIC_AUTH, allowedRoles, new BasicAuthenticator(), null, loginService);
}
Also used : HashLoginService(org.eclipse.jetty.security.HashLoginService) BasicAuthenticator(org.eclipse.jetty.security.authentication.BasicAuthenticator)

Example 38 with BasicAuthenticator

use of org.eclipse.jetty.security.authentication.BasicAuthenticator 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 39 with BasicAuthenticator

use of org.eclipse.jetty.security.authentication.BasicAuthenticator 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 40 with BasicAuthenticator

use of org.eclipse.jetty.security.authentication.BasicAuthenticator 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)

Aggregations

BasicAuthenticator (org.eclipse.jetty.security.authentication.BasicAuthenticator)40 Constraint (org.eclipse.jetty.util.security.Constraint)27 ConstraintMapping (org.eclipse.jetty.security.ConstraintMapping)17 ConstraintSecurityHandler (org.eclipse.jetty.security.ConstraintSecurityHandler)17 HashLoginService (org.eclipse.jetty.security.HashLoginService)17 Test (org.junit.Test)12 Server (org.eclipse.jetty.server.Server)6 HashSet (java.util.HashSet)3 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)3 Password (org.eclipse.jetty.util.security.Password)3 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 LoginService (org.eclipse.jetty.security.LoginService)2 ClientCertAuthenticator (org.eclipse.jetty.security.authentication.ClientCertAuthenticator)2 DigestAuthenticator (org.eclipse.jetty.security.authentication.DigestAuthenticator)2 FormAuthenticator (org.eclipse.jetty.security.authentication.FormAuthenticator)2 SpnegoAuthenticator (org.eclipse.jetty.security.authentication.SpnegoAuthenticator)2 Connector (org.eclipse.jetty.server.Connector)2 Handler (org.eclipse.jetty.server.Handler)2