0

我正在尝试将 Deadbolt 集成到我的应用程序中。我遇到了这个问题,不知道该怎么办。当我转到带有注释的受限页面时:@Restrict(@Group("admin")) 程序抛出以下异常:

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[RuntimeEx
ception: java.lang.NullPointerException]]
        at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(
HttpErrorHandler.scala:269)
        at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.
scala:195)
        at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160)
        at play.core.j.JavaGlobalSettingsAdapter.play$core$j$JavaGlobalSettingsA
dapter$$super$onError(JavaGlobalSettingsAdapter.scala:35)
        at play.core.j.JavaGlobalSettingsAdapter$$anonfun$onError$2.apply(JavaGl
obalSettingsAdapter.scala:35)
        at play.core.j.JavaGlobalSettingsAdapter$$anonfun$onError$2.apply(JavaGl
obalSettingsAdapter.scala:35)
        at scala.Option.getOrElse(Option.scala:121)
        at play.core.j.JavaGlobalSettingsAdapter.onError(JavaGlobalSettingsAdapt
er.scala:35)
        at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorH
andler.scala:98)
        at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1
.applyOrElse(PlayRequestHandler.scala:99)
Caused by: java.lang.RuntimeException: java.lang.NullPointerException
        at be.objectify.deadbolt.java.actions.AbstractDeadboltAction.call(Abstra
ctDeadboltAction.java:129)
        at play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:108)
        at play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:108)
        at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(
Future.scala:24)
        at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.sca
la:24)
        at play.core.j.HttpExecutionContext$$anon$2.run(HttpExecutionContext.sca
la:56)
        at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:
70)
        at play.core.j.HttpExecutionContext.execute(HttpExecutionContext.scala:4
8)
        at scala.concurrent.impl.Future$.apply(Future.scala:31)
        at scala.concurrent.Future$.apply(Future.scala:492)
Caused by: java.lang.NullPointerException: null
        at be.objectify.deadbolt.java.actions.AbstractDeadboltAction.preAuth(Abs
tractDeadboltAction.java:272)
        at be.objectify.deadbolt.java.actions.AbstractRestrictiveAction.execute(
AbstractRestrictiveAction.java:62)
        at be.objectify.deadbolt.java.actions.AbstractDeadboltAction.call(Abstra
ctDeadboltAction.java:121)
        at play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:108)
        at play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:108)
        at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(
Future.scala:24)
        at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.sca
la:24)
        at play.core.j.HttpExecutionContext$$anon$2.run(HttpExecutionContext.sca
la:56)
        at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:
70)
        at play.core.j.HttpExecutionContext.execute(HttpExecutionContext.scala:4
8)

我的 DeadboltHandler 实现如下所示:

package security;

import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;

import be.objectify.deadbolt.java.AbstractDeadboltHandler;
import be.objectify.deadbolt.java.DynamicResourceHandler;
import be.objectify.deadbolt.java.ExecutionContextProvider;
import be.objectify.deadbolt.java.models.Subject;
import models.User;
import play.mvc.Http;
import play.mvc.Result;
import views.html.accessFailed;

public class DeadboltHandlerImpl extends AbstractDeadboltHandler {

    public DeadboltHandlerImpl(ExecutionContextProvider ecProvider) {
        super(ecProvider);
    }

    public CompletionStage<Optional<Result>> beforeAuthCheck(final Http.Context context) {
        return CompletableFuture.completedFuture(Optional.empty());
    }

    public CompletionStage<Optional<? extends Subject>> getSubject(final Http.Context context) {

        String email = context.session().get("email");

        return CompletableFuture.supplyAsync(() -> Optional.ofNullable(User.findByEmail(email)),
                                             (Executor) executionContextProvider.get());
    }

    public CompletionStage<Optional<DynamicResourceHandler>> getDynamicResourceHandler(final Http.Context context) {
        return null;
    }

    @Override
    public CompletionStage<Result> onAuthFailure(final Http.Context context, final Optional<String> content) {

        return CompletableFuture.completedFuture(ok(accessFailed.render()));
    }

}

你能帮我解决这个问题吗?如果您需要更多信息,请告诉我。我正在使用 2.5.0 版本的 play 和 deadbolt。

谢谢, 维塔利

4

0 回答 0