Play& Deadbolt错误

时间:2017-02-09 00:23:22

标签: playframework deadbolt

每当我使用

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

@SubjectNotRequiredplay.api.http.HttpErrorHandlerExceptions$$anon$1: 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$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:100) at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:99) at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:344) at scala.concurrent.Future$$anonfun$recoverWith$1.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$2.run(HttpExecutionContext.scala:56) at play.core.j.HttpExecutionContext$$anon$2.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$testSubject$129(SubjectPresentAction.java:91) at java.util.Optional.orElseGet(Optional.java:267) at be.objectify.deadbolt.java.actions.AbstractSubjectAction.lambda$execute$118(AbstractSubjectAction.java:76) at java.util.concurrent.CompletableFuture.uniCompose(CompletableFuture.java:952) at java.util.concurrent.CompletableFuture$UniCompose.tryFire(CompletableFuture.java:926) 注释:

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);
}

其中有一个构造函数:

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; }

{{1}}

我搞砸了什么想法?

1 个答案:

答案 0 :(得分:1)

一些事情......

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

如果您查看documentation,则会发现以下位(这导致我猜测您使用的是早于2.5.3的Deadbolt版本):

  

请注意使用ConfigKeys.DEFAULT_HANDLER_KEY - 这是所有注释指定的默认处理程序密钥。在以前版本的Deadbolt中,注释驱动的约束将使用HandlerCache#apply(DEFAULT_HANDLER_KEY)来获取处理程序,因此默认处理程序必须与DEFAULT_HANDLER_KEY相关联。从Deadbolt 2.5.3开始,这已得到改进,使用默认处理程序密钥的任何注释驱动约束将改为使用HandlerCache#get

您要使用当前代码获得的下一个问题是:

当您返回null时,您违反了getSubject方法的合同。当某些内容返回CompletionStage<Optional<? extends Subject>>时,CompletionStage必须包含Optional。而不是返回null,而是返回Optional.empty()