io.scalecube.services.examples.auth.PrincipalMapperAuthExample Maven / Gradle / Ivy
package io.scalecube.services.examples.auth;
import io.scalecube.services.Microservices;
import io.scalecube.services.Microservices.Context;
import io.scalecube.services.ServiceEndpoint;
import io.scalecube.services.ServiceInfo;
import io.scalecube.services.auth.Authenticator;
import io.scalecube.services.discovery.ScalecubeServiceDiscovery;
import io.scalecube.services.exceptions.UnauthorizedException;
import io.scalecube.services.transport.api.ServiceTransport.CredentialsSupplier;
import io.scalecube.services.transport.rsocket.RSocketServiceTransport;
import io.scalecube.transport.netty.websocket.WebsocketTransportFactory;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import reactor.core.publisher.Mono;
public class PrincipalMapperAuthExample {
/**
* Main program.
*
* @param args arguments
*/
public static void main(String[] args) {
Microservices service =
Microservices.start(
new Context()
.discovery(
serviceEndpoint ->
new ScalecubeServiceDiscovery()
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
.options(opts -> opts.metadata(serviceEndpoint)))
.transport(() -> new RSocketServiceTransport().authenticator(authenticator()))
.services(
ServiceInfo.fromServiceInstance(new SecuredServiceByApiKeyImpl())
.principalMapper(PrincipalMapperAuthExample::apiKeyPrincipalMapper)
.build())
.services(
ServiceInfo.fromServiceInstance(new SecuredServiceByUserProfileImpl())
.principalMapper(PrincipalMapperAuthExample::userProfilePrincipalMapper)
.build()));
Microservices userProfileCaller =
Microservices.start(
new Context()
.discovery(endpoint -> discovery(service, endpoint))
.transport(
() ->
new RSocketServiceTransport().credentialsSupplier(credentialsSupplier())));
String responseByUserProfile =
userProfileCaller
.call()
.api(SecuredServiceByUserProfile.class)
.hello(UUID.randomUUID().toString())
.block(Duration.ofSeconds(3));
System.err.println("### Received 'userProfileCaller' response: " + responseByUserProfile);
Microservices apiKeyCaller =
Microservices.start(
new Context()
.discovery(endpoint -> discovery(service, endpoint))
.transport(
() ->
new RSocketServiceTransport().credentialsSupplier(credentialsSupplier())));
String responseByApiKey =
apiKeyCaller
.call()
.api(SecuredServiceByApiKey.class)
.hello(UUID.randomUUID().toString())
.block(Duration.ofSeconds(3));
System.err.println("### Received 'apiKeyCaller' response: " + responseByApiKey);
}
private static Authenticator © 2015 - 2025 Weber Informatics LLC | Privacy Policy