播放和门栓错误

Play & Deadbolt error

每当我使用

设置我的任何方法时,我都会得到以下堆栈跟踪(bont 不在加载的页面上!)

@SubjectRequired@SubjectNotRequired注解:

play.api.http.HttpErrorHandlerExceptions$$anon: Execution exception[[CompletionException: java.lang.NullPointerException]]
    at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:293)
    at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:220)
    at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160)
    at play.api.DefaultGlobal$.onError(GlobalSettings.scala:188)
    at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:100)
    at play.core.server.netty.PlayRequestHandler$$anonfun$$anonfun$apply.applyOrElse(PlayRequestHandler.scala:100)
    at play.core.server.netty.PlayRequestHandler$$anonfun$$anonfun$apply.applyOrElse(PlayRequestHandler.scala:99)
    at scala.concurrent.Future$$anonfun$recoverWith.apply(Future.scala:344)
    at scala.concurrent.Future$$anonfun$recoverWith.apply(Future.scala:343)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
Caused by: java.util.concurrent.CompletionException: java.lang.NullPointerException
    at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:273)
    at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:280)
    at java.util.concurrent.CompletableFuture.uniCompose(CompletableFuture.java:961)
    at java.util.concurrent.CompletableFuture$UniCompose.tryFire(CompletableFuture.java:926)
    at java.util.concurrent.CompletableFuture$Completion.run(CompletableFuture.java:442)
    at play.core.j.HttpExecutionContext$$anon.run(HttpExecutionContext.scala:56)
    at play.core.j.HttpExecutionContext$$anon.run(HttpExecutionContext.scala:56)
    at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:39)
    at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:415)
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
Caused by: java.lang.NullPointerException: null
    at be.objectify.deadbolt.java.cache.DefaultSubjectCache.apply(DefaultSubjectCache.java:80)
    at be.objectify.deadbolt.java.cache.DefaultSubjectCache.apply(DefaultSubjectCache.java:38)
    at be.objectify.deadbolt.java.ConstraintLogic.getSubject(ConstraintLogic.java:348)
    at be.objectify.deadbolt.java.ConstraintLogic.subjectTest(ConstraintLogic.java:116)
    at be.objectify.deadbolt.java.ConstraintLogic.subjectPresent(ConstraintLogic.java:72)
    at be.objectify.deadbolt.java.actions.SubjectPresentAction.lambda$testSubject9(SubjectPresentAction.java:91)
    at java.util.Optional.orElseGet(Optional.java:267)
    at be.objectify.deadbolt.java.actions.AbstractSubjectAction.lambda$execute8(AbstractSubjectAction.java:76)
    at java.util.concurrent.CompletableFuture.uniCompose(CompletableFuture.java:952)
    at java.util.concurrent.CompletableFuture$UniCompose.tryFire(CompletableFuture.java:926)

我的代码如下所示:

在class实施DeadboltHandler

@Override
public CompletionStage<Optional<? extends Subject>> getSubject(Context elContext) {
    return CompletableFuture.supplyAsync(
        () -> {
            try {
                return Optional.ofNullable(da.getUser(Integer.parseInt( elContext.session().get("userId"))));
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            } 
        });
}

其中有一个构造函数:

DataAccess da;

DeadboltSecureHandler(Database db){
    da = new DataAccess(db);
}

并在 class 实施 HandlerCache

@Inject 
public DeadboltHandlerCache(Database db){
    DeadboltHandler defaultHandler = new DeadboltSecureHandler(db);
    handlers.put(HandlerKeys.DEFAULT.key, defaultHandler);
}

@Override
public DeadboltHandler apply(final String key){
    return handlers.get(key);
}

@Override
public DeadboltHandler get(){
    return defaultHandler;
}

知道我搞砸了什么吗?

一些事情...

您使用的 deadboltHandler 为空,最可能的解释是 HandlerKeys.DEFAULT.keybe.objectify.deadbolt.java.ConfigKeys.DEFAULT_HANDLER_KEY 不同。

如果您检查 documentation,您会发现以下内容(这使我猜测您使用的 Deadbolt 版本早于 2.5.3):

Note the use of ConfigKeys.DEFAULT_HANDLER_KEY - this is the default handler key specified by all annotations. In previous versions of Deadbolt, annotation-driven constraints would use HandlerCache#apply(DEFAULT_HANDLER_KEY) to obtain the handler and so the default handler had to be associated with DEFAULT_HANDLER_KEY. As of Deadbolt 2.5.3, this has been improved and any annotation-driven constraint using the default handler key will instead use HandlerCache#get.

您当前代码要遇到的下一个问题是:

当您 return null 时,您违反了 getSubject 方法的约定。每当 returns CompletionStage<Optional<? extends Subject>> 时,CompletionStage 必须包含一个 Optional。而不是 returning null, return Optional.empty().