Frage

I read the Restlet documentation on how to implement Basic HTTP Authentication but mine is not working when I make a request to a resource. Any reason why mine is not working?

Application Context:

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

I was assuming that since I set the challenge Authenticator next method to the router when I make a request it hits the router and hits the authenticator before going to the resource.

Java Code:

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

Like I said earlier, the only thing I can see wrong with it is, I am unsure about setting challenge authenticator next method value to the router.

Also for the client side added:

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

Forgot to mention that I am testing this on my local machine the client and the web service.

War es hilfreich?

Lösung

Solved it. It took so long to figure out but here is how I got it to work.

Java Code on Server Side:

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

Application Context:

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

Hope this helps others trying to get their application working. It took me a while to get this to work. :)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top