Autenticazione a grana fine con Restlet
-
19-09-2019 - |
Domanda
Voglio esporre una risorsa utilizzando Restlet con un'autenticazione a grana fine. Il mio ServerResource
dovrebbe essere accessibile tramite GET
solo per i membri autenticati (con autenticazione di base). Tuttavia, le richieste utilizzando POST
dovrebbero essere disponibili anche per gli utenti privi alcuna autenticazione.
Al fine di clearify:
http: // percorso / frontend / utente dovrebbe consentire a chiunque di registrarsi utilizzando POST
, ma solo i membri iscritti dovrebbe essere in grado di GET
un elenco di tutti gli utenti.
Sono purtroppo non molto in Restlet e trovo solo degli esempi utilizzando l'autenticazione più grossa per Restlet
s intere o Router
s.
Quindi, come faccio ad attivare l'autenticazione opzionale per le risorse e controllare a livello di singolo-metodo?
Grazie in anticipo!
Soluzione
Per fare l'autenticazione di base in Restlet 2.0 (presumo che si sta utilizzando 2.0 dal momento che si parla ServerResource
), è necessario utilizzare un ChallengeAuthenticator
. Se questo è configurato con optional = true
allora l'autenticazione sarà richiesto solo se si richiama ChallengeAuthenticator.challenge()
.
È possibile creare l'applicazione con un metodo authenticate()
, e chiamare questo ogni volta che è necessario accedere a una risorsa da fissare:
applicazione:
package example;
import org.restlet.*;
import org.restlet.data.ChallengeScheme;
import org.restlet.routing.Router;
import org.restlet.security.*;
public class ExampleApp extends Application {
private ChallengeAuthenticator authenticatior;
private ChallengeAuthenticator createAuthenticator() {
Context context = getContext();
boolean optional = true;
ChallengeScheme challengeScheme = ChallengeScheme.HTTP_BASIC;
String realm = "Example site";
// MapVerifier isn't very secure; see docs for alternatives
MapVerifier verifier = new MapVerifier();
verifier.getLocalSecrets().put("user", "password".toCharArray());
ChallengeAuthenticator auth = new ChallengeAuthenticator(context, optional, challengeScheme, realm, verifier) {
@Override
protected boolean authenticate(Request request, Response response) {
if (request.getChallengeResponse() == null) {
return false;
} else {
return super.authenticate(request, response);
}
}
};
return auth;
}
@Override
public Restlet createInboundRoot() {
this.authenticatior = createAuthenticator();
Router router = new Router();
router.attach("/user", UserResource.class);
authenticatior.setNext(router);
return authenticatior;
}
public boolean authenticate(Request request, Response response) {
if (!request.getClientInfo().isAuthenticated()) {
authenticatior.challenge(response, false);
return false;
}
return true;
}
}
Resource:
package example;
import org.restlet.data.MediaType;
import org.restlet.representation.EmptyRepresentation;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.ServerResource;
public class UserResource extends ServerResource {
@Override
public Representation get() {
ExampleApp app = (ExampleApp) getApplication();
if (!app.authenticate(getRequest(), getResponse())) {
// Not authenticated
return new EmptyRepresentation();
}
// Generate list of users
// ...
}
@Override
public Representation post(Representation entity) {
// Handle post
// ...
}
}
Altri suggerimenti
Sto attualmente utilizzando v2.0.10 Restlet.
Il problema con ChallengeAuthenticator.isOptional()
è che è tutto o niente. In alternativa alla soluzione fornita da @ sea36 sopra è quello di sovrascrivere ChallengeAuthenticator.beforeHandle()
a uno eseguire l'autenticazione o ignorarlo basato sul metodo richiesta. Ad esempio, il reddito al di sotto richiederà l'autenticazione solo quando viene utilizzato il metodo GET.
applicazione:
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";
// MapVerifier isn't very secure; see docs for alternatives
MapVerifier verifier = new MapVerifier();
verifier.getLocalSecrets().put("user", "password".toCharArray());
ChallengeAuthenticator authOnGet = new ChallengeAuthenticator(context, challengeScheme, realm) {
@Override
protected int beforeHandle(Request request, Response response) {
if (request.getMethod() == Method.GET)
return super.beforeHandle(request, response);
response.setStatus(Status.SUCCESS_OK);
return CONTINUE;
}
};
return authOnGet;
}
@Override
public Restlet createInboundRoot() {
ChallengeAuthenticator userResourceWithAuth = createAuthenticator();
userResourceWithAuth.setNext(UserResource.class);
Router router = new Router();
router.attach("/user", userResourceWithAuth);
return router;
}
}
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() {
// retrieve list of users and generate response
// ...
}
@Post
public void register(Representation entity) {
// handle post
// ...
}
}
Si noti che questo esempio si applica la politica di autenticazione su GET solo ai UserResource
e non altre risorse gestite dal router.