2014-02-19 45 views
0

我的任務是繼續處理java restlet服務器。Restlet - 如何在創建新用戶後更新驗證者驗證者映射?

我能夠根據需要設置我的路由,特別是有一個用戶資源可以在URL上通過POST調用創建。發生這種情況時,會在數據庫上創建一個新用戶。

從這一點上我希望新用戶能夠進行身份驗證。問題是入站根中的驗證器在開始時加載一次用戶名和密碼。因此,如果創建了新用戶,除非重新啓動服務器,否則他無法進行身份驗證。

必須有一個簡單的方法來處理這個!

這是應用程序:

public class APIServerApplication extends Application { 


    @Override 
    public Restlet createInboundRoot(){ 
      //some routers and filter.. 

      //here the verifier is initialized with the current users and passwords: 
      MapVerifier verifier = new MapVerifier(); 

      //get users and pwd from DB 
      HashMap<String,String> usrPwdMap = SomeDBClass.getVerifierMap(); 
      for(String uname : usrPwdMap.keySet()){ 
       verifier.getLocalSecrets().put(uname, (usrPwdMap.get(uname)).toCharArray()); 
      } 

      //..verifier is used to build the authenticator... etc 

    } 

用戶資源看起來像:

public class UserResource extends ServerResource{ 

     @Post 
    public Representation acceptItem(Representation entity) { 
      //get the data from the form  
      //insert the new user in the db 


      /* TODO: I think i should add something here 
       to refresh the verifier map! 
       */ 
     } 
} 

我怎麼能刷新驗證地圖?

回答

0

這是在生產應用中做事情的一種不正確的方式。切勿加載內存中的用戶名/密碼組合。這太過於矯枉過正。

事實上,請修改您的應用程序,以檢查每個http請求上針對該用戶數據庫中存儲的用戶名/密碼。這是寧靜的做事方式。如果你認爲這不是一個好的做法,我建議你閱讀這個問題的答案:RESTful authentication - resulting poor performance on high load?

+2

我同意,爲了生產目的,你應該建立自定義的Restlet Verifier(查看子類以及)查找結果對於來自用戶目錄(數據庫或其他)的每個呼叫。 –

+0

謝謝!我正在研究這個 –