【问题标题】:How do you implement Restlet Basic HTTP Authentication on a router?如何在路由器上实现 Restlet 基本 HTTP 身份验证?
【发布时间】:2012-11-07 18:42:58
【问题描述】:

我阅读了有关如何实现基本 HTTP 身份验证的 Restlet 文档,但是当我向资源发出请求时,我的文档无法正常工作。为什么我的不工作有什么原因?

应用程序上下文:

<!-- 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>

我假设由于我在发出请求时将挑战 Authenticator next 方法设置为路由器,因此它会在访问资源之前命中路由器并命中身份验证器。

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

忘了提到我正在本地机器上测试这个客户端和网络服务。

【问题讨论】:

    标签: spring restlet applicationcontext restlet-2.0


    【解决方案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>
    

    希望这有助于其他人尝试让他们的应用程序正常工作。我花了一段时间才让它工作。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-14
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多