2

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.

dtr84
  • 241
  • 1
  • 2
  • 10
  • Have you tried to use filters https://www.playframework.com/documentation/2.8.x/JavaHttpFilters#Filters? You can inject DeadboltHandler instance, modify request with `Http.Request.withAttrs()` and pass modified object to next filter. – Valerii Rusakov Aug 12 '21 at 14:58
  • Can you give some more detail please? The `getSubject()` method of the DeadboltHandler implementation - which is called ***implicitely*** before the controller method - has *one* parameter of type `Http.RequestHeader`, and it has return type of `Optional extends Subject>`. So, when implementing this method, I *have* to return a Subject. How would I go about "pass modified object to next filter"? I really see no way to do this from the DeadboltHanlder... – dtr84 Aug 13 '21 at 15:27

0 Answers0