2016-03-20 9 views
1

Ich habe eine Restlet (2.2.3) Anwendung (Ich bin neu in Restlet), die aufzubauen ist als:Befestigung von Bauteilen an Restlet

component = new Component(); 
component.getServers().add(Protocol.HTTP, port); 
Context childContext = component.getContext().createChildContext(); 
JaxRsApplication application = new JaxRsApplication(childContext); 
application.add(this); 
application.setStatusService(new ErrorStatusService()); 
childContext.getAttributes().put(MY_SERVER, this);  
component.getDefaultHost().attach(application); 

Es läuft als eigenständige Java-Anwendung. Ich möchte Sicherheit hinzufügen. Hier ist der grundlegende Authentifizierungscode von restlet authentication documentation:

// Guard the restlet with BASIC authentication. 
ChallengeAuthenticator guard = new ChallengeAuthenticator(null, ChallengeScheme.HTTP_BASIC, "testRealm"); 
// Instantiates a Verifier of identifier/secret couples based on a simple Map. 
MapVerifier mapVerifier = new MapVerifier(); 
// Load a single static login/secret pair. 
mapVerifier.getLocalSecrets().put("login", "secret".toCharArray()); 
guard.setVerifier(mapVerifier); 

guard.setNext(restlet);  

Component component = new Component(); 
component.getServers().add(Protocol.HTTP, 8182); 
component.getDefaultHost().attachDefault(guard); 

Wie kann ich integrieren, dass Sicherheitsmechanismus in meinen aktuellen Code?

Antwort

1

Sie müssen Ihre Restlet-Anwendung als nächstes Element für den Guard angeben. Auf diese Weise wird die Anwendung das nächste Element in der Verarbeitungskette und wird aufgerufen, wenn die Authentifizierung erfolgreich ist.

// Guard the restlet with BASIC authentication. 
ChallengeAuthenticator guard = new ChallengeAuthenticator(null, 
         ChallengeScheme.HTTP_BASIC, "testRealm"); 
// Instantiates a Verifier of identifier/secret couples based on a simple Map. 
MapVerifier mapVerifier = new MapVerifier(); 
// Load a single static login/secret pair. 
mapVerifier.getLocalSecrets().put("login", "secret".toCharArray()); 
guard.setVerifier(mapVerifier); 

// Application 
JaxRsApplication application = new JaxRsApplication(childContext); 
application.add(this); 
application.setStatusService(new ErrorStatusService()); 

// Set application within guard 
guard.setNext(application); // <-------- 

// Create and configure component 
Component component = new Component(); 
component.getServers().add(Protocol.HTTP, 8182); 
component.getDefaultHost().attachDefault(guard);