How to pass information from DeadboltHanlder to Controller-Method (Play Framework 2.8)

107 views Asked by At

In our Play Framework application, we are using the Deadbolt module for user authentication. This means that the getSubject() method of our DeadboltHandler-implementation will be called before the actual Controller method. However, we need to transport the determined User-ID from the DeadboltHandler's getSubject() method to the Controller method.

Up to and including Play Framework 2.6 this could easily be done by adding the User-ID to the HttpContext.args, because the HttpContext was available in getSubject() as well as in the Controller-Method. Unfortuately, in Play Framework 2.7 and later, there no longer is a HttpContext - we have to work with HttpRequest directly. But it seems that HttpRequest is immutable; we cannot add the determined User-ID to HttpRequest easily.

Yes, there is Http.Request.withAttrs(), but this does not modify the HttpRequest object but instead returns a new HttpRequest object. So this is not helpful for passing any information from getSubject() method to the Controller method, as far as I can tell.

So, what is the recommended way to pass Request-specific information from the DeadboltHandler to the Controller-Method in Play Framework 2.7+ ???

Controller class:

public class MyController extends Controller
{
    @SubjectPresent
    public CompletionStage<Result> search()
    {
        final Subject subject = Thingamabob.currentSubject();
        /* I need something like this ^^^ */
    }
}

DeadboltHandler implementation:

class public class MyDeadboltHandler extends AbstractDeadboltHandler {
    @Override
    public CompletionStage<Optional<? extends Subject>>
            getSubject(final Http.RequestHeader request) {
        final Subject subject = determineSubjectFromRequestHeader(request);
        return CompletableFuture.completedFuture(Optional.of(subject));
    }
}

Note: The method MyDeadboltHandler.subject(), which determines the Subject, is called implicitely by the framework before the actual controller method will be called.

Thanks you and best regards.

0

There are 0 answers