use of io.helidon.security.Security in project helidon by oracle.
the class SecureServer method createWebServer.
/**
* Create the web server.
*/
private static WebServer createWebServer(Config config, Security security) {
Routing routing = Routing.builder().register(WebSecurity.create(security).securityDefaults(WebSecurity.authenticate())).register(new RestService()).build();
WebServer webServer = WebServer.create(routing, config);
webServer.start().thenAccept(s -> {
System.out.println("Web server is UP! http://localhost:" + s.port());
s.whenShutdown().thenRun(() -> System.out.println("gRPC server is DOWN. Good bye!"));
}).exceptionally(t -> {
System.err.println("Web server startup failed: " + t.getMessage());
t.printStackTrace(System.err);
return null;
});
return webServer;
}
use of io.helidon.security.Security in project helidon by oracle.
the class SecureServer method createGrpcServer.
/**
* Create the gRPC server.
*/
private static GrpcServer createGrpcServer(Config config, Security security) {
GrpcRouting grpcRouting = GrpcRouting.builder().intercept(GrpcSecurity.create(security).securityDefaults(GrpcSecurity.authenticate())).register(new StringService(), GrpcSecurity.rolesAllowed("admin")).register(new GreetService()).build();
GrpcServer grpcServer = GrpcServer.create(GrpcServerConfiguration.create(config), grpcRouting);
grpcServer.start().thenAccept(s -> {
System.out.println("gRPC server is UP! http://localhost:" + s.port());
s.whenShutdown().thenRun(() -> System.out.println("gRPC server is DOWN. Good bye!"));
}).exceptionally(t -> {
System.err.println("gRPC server startup failed: " + t.getMessage());
t.printStackTrace(System.err);
return null;
});
return grpcServer;
}
use of io.helidon.security.Security in project helidon by oracle.
the class SecureGreetClient method main.
/**
* Main entry point.
*
* @param args the program arguments - {@code arg[0]} is the user name
* and {@code arg[1] is the password}
*/
public static void main(String[] args) {
Channel channel = ManagedChannelBuilder.forAddress("localhost", 1408).usePlaintext().build();
// Obtain the user name and password from the program arguments
String user = args.length >= 2 ? args[0] : "Ted";
String password = args.length >= 2 ? args[1] : "secret";
Config config = Config.create();
// configure Helidon security and add the basic auth provider
Security security = Security.builder().addProvider(HttpBasicAuthProvider.create(config.get("http-basic-auth"))).build();
// create the gRPC client security call credentials
// setting the properties used by the basic auth provider for user name and password
GrpcClientSecurity clientSecurity = GrpcClientSecurity.builder(security.createContext("test.client")).property(HttpBasicAuthProvider.EP_PROPERTY_OUTBOUND_USER, user).property(HttpBasicAuthProvider.EP_PROPERTY_OUTBOUND_PASSWORD, password).build();
// Create the client service descriptor and add the call credentials
ClientServiceDescriptor descriptor = ClientServiceDescriptor.builder(GreetServiceGrpc.getServiceDescriptor()).callCredentials(clientSecurity).build();
// create the client for the service
GrpcServiceClient client = GrpcServiceClient.create(channel, descriptor);
greet(client);
setGreeting(client);
greet(client);
}
use of io.helidon.security.Security in project helidon by oracle.
the class SecureServer method main.
/**
* Main entry point.
*
* @param args the program arguments
*/
public static void main(String[] args) {
LogConfig.configureRuntime();
Config config = Config.create();
Security security = Security.builder().addProvider(HttpBasicAuthProvider.create(config.get("http-basic-auth"))).build();
ServiceDescriptor greetService1 = ServiceDescriptor.builder(new GreetService(config)).name("GreetService").intercept(GrpcSecurity.rolesAllowed("user")).intercept("SetGreeting", GrpcSecurity.rolesAllowed("admin")).build();
GrpcRouting grpcRouting = GrpcRouting.builder().intercept(GrpcSecurity.create(security).securityDefaults(GrpcSecurity.authenticate())).register(greetService1).register(new StringService()).build();
GrpcServerConfiguration serverConfig = GrpcServerConfiguration.create(config.get("grpc"));
GrpcServer grpcServer = GrpcServer.create(serverConfig, grpcRouting);
grpcServer.start().thenAccept(s -> {
System.out.println("gRPC server is UP! http://localhost:" + s.port());
s.whenShutdown().thenRun(() -> System.out.println("gRPC server is DOWN. Good bye!"));
}).exceptionally(t -> {
System.err.println("Startup failed: " + t.getMessage());
t.printStackTrace(System.err);
return null;
});
}
use of io.helidon.security.Security in project helidon by oracle.
the class DigestExampleBuilderMain method main.
/**
* Starts this example. Programmatical configuration. See standard output for instructions.
*
* @param args ignored
*/
public static void main(String[] args) {
// load logging configuration
LogConfig.configureRuntime();
// build routing (same as done in application.conf)
Routing routing = Routing.builder().register(buildWebSecurity().securityDefaults(WebSecurity.authenticate())).get("/noRoles", WebSecurity.enforce()).get("/user[/{*}]", WebSecurity.rolesAllowed("user")).get("/admin", WebSecurity.rolesAllowed("admin")).get("/deny", WebSecurity.rolesAllowed("deny").audit()).any("/noAuthn", WebSecurity.rolesAllowed("admin").authenticationOptional().audit()).get("/{*}", (req, res) -> {
Optional<SecurityContext> securityContext = req.context().get(SecurityContext.class);
res.headers().contentType(MediaType.TEXT_PLAIN.withCharset("UTF-8"));
res.send("Hello, you are: \n" + securityContext.map(ctx -> ctx.user().orElse(SecurityContext.ANONYMOUS).toString()).orElse("Security context is null"));
}).build();
// start server (blocks until started)
server = DigestExampleUtil.startServer(routing);
}
Aggregations