2012-02-11 56 views
1

我在服務器端使用HTTP摘要身份驗證機制,客戶端是Firefox。Restlet中的HTTP身份驗證,驗證子網址

這是服務器端代碼

Application application = new Vehicle(); 

component.getDefaultHost().attachDefault(application); 
component.getDefaultHost().attach("/home",new Home()); 

DigestAuthenticator guard = new DigestAuthenticator(null, "TestRealm","mySecretServerKey"); 
Instantiates a Verifier of identifier/secret couples based on a simple Map. 
MapVerifier mapVerifier = new MapVerifier(); 

負載,單一的靜態登錄/祕密對。

mapVerifier.getLocalSecrets().put("login", "secret".toCharArray()); 
guard.setWrappedVerifier(mapVerifier); 

衛隊的Restlet

guard.setNext(application); 
component.getDefaultHost().attachDefault(guard); 
component.start(); 

在家庭類

Router router = new Router(getContext()); 
router.attach("/People", People.class); 
router.attach("/categories/",Categories.class); 

return router; 

,如果我要求http://localhost:8182/ HTTP驗證工作,但http://localhost:8182/home/categories/不要求任何http authentication如果我們首先嚐試/home/categories/代替http://localhost:8182/它會給出結果與任何身份驗證機制SM。如何解決這個問題?

回答

0

您只將防護裝置附加到默認路線,以便與其他路線不匹配的路線。查看javadoc瞭解attachDefault:

* Attaches a Resource class to this router as the default target to invoke 
* when no route matches. It actually sets a default route that scores all 
* calls to 1.0. 

你的其他途徑都沒有缺省路由,所以他們沒有守衛

router.attach("/People", People.class); 
router.attach("/categories/",Categories.class); 

必須連接要保護像這樣每個路由之間的保護:

DigestAuthenticator peopleGuard = new DigestAuthenticator(null, "TestRealm","mySecretServerKey"); 
peopleGuard.setNext(People.class); 
router.attach("/People", peopleGuard); 
+0

感謝您的回覆,將嘗試您的建議。 – ridy 2012-02-15 05:40:33