2012-11-07 53 views
2

我閱讀了關於如何實現基本HTTP認證的Restlet文檔,但是當我向資源發出請求時,我的工作不正常。我的任何理由不工作?如何在路由器上實現Restlet Basic HTTP Authentication?

應用程序上下文:

<!-- Used to map routes to Restlet resources --> 
    <bean id="router" class="org.restlet.ext.spring.SpringRouter"> 
     <property name="attachments"> 
      <map> 
       <!-- I removed the actual values because it references a company --> 
       <entry key="/getCompanies" value="ClassResource" /> 
       <entry key="/getList" value="ClassResource" /> 
       <entry key="/getFile" value="ClassResource" /> 
       <entry key="/archiveFile" value="ClassResource" /> 
      </map> 
     </property> 
    </bean> 

    <!-- Used to have login authentication for requests --> 
    <bean id="challengeAuthenticator" class="org.restlet.security.ChallengeAuthenticator"> 
     <constructor-arg><null /></constructor-arg> 
     <!-- Sets the Challenge scheme parameter to the static class member --> 
     <constructor-arg value="#{ T(org.restlet.data.ChallengeScheme).HTTP_BASIC }" /> 
     <constructor-arg value="WSRealm" /> 
     <property name="next" ref="router" /> 
    </bean> 

    <!-- Creates a restlet component that contains the server and attachs the application --> 
    <bean id="restletComponent" class="org.restlet.ext.spring.SpringComponent"> 
     <!-- Sets the server in the Restlet component --> 
     <property name="server" ref="server" /> 
     <!-- Attachs the application to the virtual host --> 
     <property name="defaultTarget" ref="application" /> 
    </bean> 

我是假設,因爲我設置的挑戰,身份驗證方法旁邊的路由器,當我提出一個要求它擊中路由器纔去資源擊中認證。

Java代碼:

ApplicationContext springContext = new GenericXmlApplicationContext("applicationContext.xml"); 
Component restletComponent = (Component) springContext.getBean("restletComponent"); 
GetFilesApplication application = (GetFilesApplication) springContext.getBean("application"); 
ChallengeAuthenticator challengeAuthenticator = 
      (ChallengeAuthenticator) springContext.getBean("challengeAuthenticator"); 
Config config = application.getConfig(); 
MapVerifier mapVerifier = new MapVerifier(); 

// Puts the user name and password (encrypted) in the map verifier 
mapVerifier.getLocalSecrets().put(config.getUsername(), StringCipher.encrypt(
      config.getPassword()).toCharArray()); 
challengeAuthenticator.setVerifier(mapVerifier); 
restletComponent.getDefaultHost().attachDefault(challengeAuthenticator); 

// Start the component 
restletComponent.start(); 

就像我前面說的,我唯一能看到不對的地方是,我不確定設定挑戰認證下一個方法值到路由器。

也爲客戶端添加的:

clientResource.setChallengeResponse(ChallengeScheme.HTTP_BASIC, "correctUser", StringCipher.encrypt("password")); 

忘了提,我在我的本地機器上測試該客戶端和Web服務。

回答

1

解決了它。花了很長時間才弄清楚,但這是我如何運作的。

Java代碼的服務器端:

// Removed and added to Application Context 
restletComponent.getDefaultHost().attachDefault(challengeAuthenticator); 

應用程序上下文:

<bean id="propertyConfigurer" 
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="locations"> 
      <list> 
       <value>configuration.properties</value> 
       <value>log4j.properties</value> 
      </list> 
     </property> 
    </bean> 

    <bean id="config" class="Config class path location"> 
     <property name="filePath" value="${Properties entry value}"/> 
     <property name="archivePath" value="${Properties entry value}"/> 
     <property name="username" value="${Properties entry value}"/> 
     <property name="password" value="${Properties entry value}"/> 
    </bean> 

    <!-- Restlet application --> 
    <bean id="application" class="Application class path location" scope="singleton"> 
     <!-- Sets the router for the application --> 
     <property name="root" ref="router" /> 
     <property name="config" ref="config" /> 
    </bean> 

    <!-- Sets up the server --> 
    <bean id="server" class="org.restlet.ext.spring.SpringServer"> 
     <constructor-arg value="${Properties entry value}" /> 
     <constructor-arg value="${Properties entry value}" /> 
    </bean> 

    <!-- Used to map routes to Restlet resources --> 
    <bean id="router" class="org.restlet.ext.spring.SpringRouter"> 
     <property name="attachments"> 
      <map> 
       <entry key="/getCompanies" value="Resource class path location" /> 
       <entry key="/getList" value="Resource class path location" /> 
       <entry key="/getFile" value="Resource class path location" /> 
       <entry key="/archiveFile" value="Resource class path location" /> 
      </map> 
     </property> 
    </bean> 

    <!-- Creates a restlet component that contains the server and attachs the application --> 
    <bean id="restletComponent" class="org.restlet.ext.spring.SpringComponent"> 
     <!-- Sets the server in the Restlet component --> 
     <property name="server" ref="server" /> 
     <!-- Attachs the application to the virtual host --> 
     <property name="defaultTarget" ref="application" /> 
     <property name="defaultHost" ref="defaultHost" /> 
    </bean> 

    <!-- Used to have login authentication for requests --> 
    <bean id="challengeAuthenticator" class="org.restlet.security.ChallengeAuthenticator"> 
     <constructor-arg><null /></constructor-arg> 
     <!-- Sets the Challenge scheme parameter to the static class member --> 
     <constructor-arg value="#{ T(org.restlet.data.ChallengeScheme).HTTP_BASIC }" /> 
     <constructor-arg value="GetWSRealm" /> 
     <property name="next" ref="application" /> 
    </bean> 

    <bean id="defaultHost" class="org.restlet.ext.spring.SpringHost"> 
     <constructor-arg ref="restletComponent" /> 
     <property name="defaultAttachment" ref="challengeAuthenticator" /> 
    </bean> 

希望這有助於其他人試圖把他們的應用程序的工作。我花了一段時間纔得到這個工作。 :)