I am currently using Restlet v2.0.10.
The problem with ChallengeAuthenticator.isOptional() is that it is all or nothing. An alternative to the answer above in @ sea36 is to override ChallengeAuthenticator.beforeHandle() to authenticate or skip it based on the request method. For example, the resource below will only ask for authentication when the GET method is used.
Application:
package example; import org.restlet.*; import org.restlet.data.ChallengeScheme; import org.restlet.routing.Router; import org.restlet.security.ChallengeAuthenticator; import org.restlet.security.MapVerifier; public class ExampleApp extends Application { private ChallengeAuthenticator createAuthenticator() { Context context = getContext(); ChallengeScheme challengeScheme = ChallengeScheme.HTTP_BASIC; String realm = "Example site";
Resource:
package example; import org.restlet.resource.Get; import org.restlet.resource.Post; import org.restlet.representation.Representation; import org.restlet.resource.ServerResource; public class UserResource extends ServerResource { @Get public Representation listUsers() {
Please note: this example applies the authentication policy in GET only to UserResource , and not to other resources handled by the router.
jaguild
source share