2016-03-20 23 views
1

azından bir Restlet var olarak oluşturulur, bu sayede (2.2.3) uygulama (ı Restlet yeniyim): Bu tek başına bir java uygulaması olarak çalışırgüvenliğini Bileşenleri 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); 

. Güvenlik eklemek istiyorum. İşte restlet authentication documentation temel kimlik doğrulama kodudur:

// 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); 

benim şimdiki koduna güvenlik mekanizması olduğunu entegre edebilirsiniz nasıl? Restlet uygulamanızı bekçi için bir sonraki öğe olarak belirtmeniz gerekir.

cevap

1

Bu şekilde uygulama işlem zincirindeki bir sonraki eleman olacaktır ve kimlik doğrulaması başarılı olursa çağrılabilir.

// 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);