2016-03-20 9 views
1

で私はのRestletを持っているとして構築される(2.2.3)アプリケーション(私はRestletのために新たなんだ):これは、スタンドアロンのJavaアプリケーションとして実行確保コンポーネントの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); 

。私はそれにセキュリティを追加したいと思います。ここで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); 

私は私の現在のコードにそのセキュリティメカニズムを統合することができますどのように?

答えて

1

ガードの次の要素としてRestletアプリケーションを指定する必要があります。このようにして、アプリケーションは処理チェーン内の次の要素になり、認証が成功すると呼び出されます。

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