use of org.eclipse.jetty.security.ConstraintSecurityHandler in project camel by apache.
the class HttpBasicAuthTest 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;
}
use of org.eclipse.jetty.security.ConstraintSecurityHandler 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();
}
use of org.eclipse.jetty.security.ConstraintSecurityHandler in project calcite-avatica by apache.
the class HttpServer method configureCommonAuthentication.
protected ConstraintSecurityHandler configureCommonAuthentication(Server server, ServerConnector connector, AvaticaServerConfiguration config, String constraintName, String[] allowedRoles, Authenticator authenticator, String realm, LoginService loginService) {
Constraint constraint = new Constraint();
constraint.setName(constraintName);
constraint.setRoles(allowedRoles);
// This is telling Jetty to not allow unauthenticated requests through (very important!)
constraint.setAuthenticate(true);
ConstraintMapping cm = new ConstraintMapping();
cm.setConstraint(constraint);
cm.setPathSpec("/*");
ConstraintSecurityHandler sh = new ConstraintSecurityHandler();
sh.setAuthenticator(authenticator);
sh.setLoginService(loginService);
sh.setConstraintMappings(new ConstraintMapping[] { cm });
sh.setRealmName(realm);
return sh;
}
use of org.eclipse.jetty.security.ConstraintSecurityHandler in project bnd by bndtools.
the class HttpConnectorTest method startJetty.
private static Server startJetty() throws Exception {
Server server = new Server();
// Create the login service
String REQUIRED_ROLE = "users";
HashLoginService loginSvc = new HashLoginService(REQUIRED_ROLE, USER_ROLE_FILE);
server.addBean(loginSvc);
// Start HTTP and HTTPS connectors
SelectChannelConnector httpConnector = new SelectChannelConnector();
httpConnector.setPort(0);
httpConnector.setHost(LOCALHOST);
server.addConnector(httpConnector);
SslSelectChannelConnector sslConnector = new SslSelectChannelConnector();
sslConnector.setPort(0);
sslConnector.setHost(LOCALHOST);
SslContextFactory contextFactory = sslConnector.getSslContextFactory();
contextFactory.setKeyStorePath(KEYSTORE_PATH);
contextFactory.setKeyStorePassword(KEYSTORE_PASS);
server.addConnector(sslConnector);
// Create the resource handler to serve files
ResourceHandler resourceHandler = new ETaggingResourceHandler();
resourceHandler.setResourceBase(RESOURCE_BASE);
resourceHandler.setDirectoriesListed(true);
// Setup user role constraints
Constraint constraint = new Constraint();
constraint.setName(Constraint.__BASIC_AUTH);
constraint.setRoles(new String[] { REQUIRED_ROLE });
constraint.setAuthenticate(true);
// Map constraints to the secured directory
ConstraintMapping cm = new ConstraintMapping();
cm.setConstraint(constraint);
cm.setPathSpec(SECURED_PATH);
// Setup the constraint handler
ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();
securityHandler.setAuthMethod("BASIC");
securityHandler.setHandler(resourceHandler);
securityHandler.setLoginService(loginSvc);
securityHandler.setConstraintMappings(new ConstraintMapping[] { cm });
// Finally!! Start the server
server.setHandler(securityHandler);
server.start();
while (!server.isRunning()) {
Thread.sleep(10);
}
HTTP_PORT = httpConnector.getLocalPort();
HTTPS_PORT = sslConnector.getLocalPort();
assertNotSame(Integer.valueOf(0), Integer.valueOf(HTTP_PORT));
assertNotSame(Integer.valueOf(-1), Integer.valueOf(HTTP_PORT));
assertNotSame(Integer.valueOf(0), Integer.valueOf(HTTPS_PORT));
assertNotSame(Integer.valueOf(-1), Integer.valueOf(HTTPS_PORT));
assertNotSame(Integer.valueOf(HTTP_PORT), Integer.valueOf(HTTPS_PORT));
return server;
}
Aggregations