Search code examples
javaapirestjax-rsrestlet

RESTlet + JAX-RS Authentication Middleware


I'm making a backend API using RESTlet and JAX-RS on app engine. I'd like to make a method that is called before each request to check whether the user is authenticated (has to make a query to an external API via HTTP) and then continue or stop depending.

How could I do this?

Thanks, Daniel


Solution

  • Turns out to be very simple.

    Create a subclass of Authenticator (http://restlet.com/technical-resources/restlet-framework/javadocs/2.3/jse/api/org/restlet/security/Authenticator.html?is-external=true) and implement authenticate() with custom authentication and any other functions that are needed.

    For example,

    public class MyAuthenticator extends Authenticator {
    
    public MyAuthenticator(Context context) {
        super(context);
    }
    
    @Override
    protected boolean authenticate(Request request, Response response) {
        // do your custom authentication here and return true or false depending on result
    }