mkurz/deadbolt-2-java

No implementation for be.objectify.deadbolt.java.cache.HandlerCache was bound.

danbim opened this issue · 3 comments

Trying to to run deadbolt 2 with play 2.4 (using "be.objectify" %% "deadbolt-java" % "2.4.0")

application.conf:

play {
  modules {
    enabled += "be.objectify.deadbolt.java.DeadboltModule"
  }
}

deadbolt {
  java {
    handler= auth.MyAuthorizationHandler
    cache-user=true
  }
}

Any ideas?

This is how I did it:

in application.conf:
play.modules.enabled += "modules.DeadboltHook"

package modules;

import auth.MyDeadboltHandlerCache;
import auth.MyDeadboltTemplateFailureListener;

import be.objectify.deadbolt.java.TemplateFailureListener;
import be.objectify.deadbolt.java.cache.HandlerCache;

import javax.inject.Singleton;

import play.api.Configuration;
import play.api.Environment;
import play.api.inject.Binding;
import play.api.inject.Module;

import scala.collection.Seq;

/**
 * Creates a binding for a custom template failure listener.
 *
 * @author Johan Dahlberg <johan.dahlberg@mobitec.eu>
 */
public class DeadboltHook extends Module {
    @Override
    public Seq<Binding<?>> bindings(final Environment environment, final Configuration configuration) {
        return seq(bind(TemplateFailureListener.class).to(MyDeadboltTemplateFailureListener.class).in(Singleton.class),
                bind(HandlerCache.class).to(MyDeadboltHandlerCache.class).in(Singleton.class));
    }
}
package auth;

import be.objectify.deadbolt.java.DeadboltHandler;
import be.objectify.deadbolt.java.cache.HandlerCache;

import java.util.HashMap;
import java.util.Map;
import javax.inject.Singleton;

/**
 * @author Johan Dahlberg <johan.dahlberg@mobitec.eu>
 */
@Singleton
public class MyDeadboltHandlerCache implements HandlerCache {
    private final DeadboltHandler defaultHandler = new MyDeadboltHandler();
    private final Map<String, DeadboltHandler> handlers = new HashMap<>();

    public MyDeadboltHandlerCache() {
        handlers.put(MyDeadboltHandlerKeys.DEFAULT.key, defaultHandler);
    }

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

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

import be.objectify.deadbolt.java.TemplateFailureListener;

import javax.inject.Singleton;

import play.Logger;

/**
 * @author Johan Dahlberg <johan.dahlberg@mobitec.eu>
 */
@Singleton
public class MyDeadboltTemplateFailureListener implements TemplateFailureListener {
    private static Logger.ALogger LOGGER = Logger.of(MyDeadboltTemplateFailureListener.class);

    @Override
    public void failure(final String message, final long timeout) {
        LOGGER.error("Template constraint failure: message [{}]  timeout [{}]ms", message, timeout);
    }
}

It's covered in the quick-start guide and @johdah also covers it nicely here.

There's also a discussion here which might be useful for you.

It's worth noting that TemplateFailureListener is optional - you don't need to implement and expose one unless you have a use for it.

D'oh! I kind of overlooked the quick-start guide and just did the changes mentioned in the 2.4.0 release notes in README.md... Thanks guys for helping out here and sorry for the work!