How to inject Session/User object after bootstrapping?

拟墨画扇 提交于 2019-12-23 11:54:07

问题


There are examples where different kinds of objects are getting injected into a presenter, but I can't find an explanation how this can be done.

In the Bootstrap-Code example they are injecting e.g. a SecurityDelegate object.

Also in the Gatekeeper example I see things being injected, e.g. MyGatekeeper, but how is this done?

What I want is to first check if the user is logged in and then create a CurrentSession object or something like this. But how can I pass/inject this object around?

At the moment I am initializing a singleton object CurrentUser which is kind of ugly imho. I would like to get the GWTP support running, but how?


Take this example of the CurrentSession being injected into the gatekeeper:

@DefaultGatekeeper
public class LoggedInGatekeeper implements Gatekeeper {
    private final CurrentSession currentSession;

    @Inject
    LoggedInGatekeeper(CurrentSession currentSession) {
        this.currentSession = currentSession;
    }

    @Override
    public boolean canReveal() {
        return currentSession.isLoggedIn();
    }
}

How do I inject CurrentSession here?


回答1:


Here is a tutorial that explains how to use Gatekeeper : http://dev.arcbees.com/gwtp/tutorials/tutorial-part2.html

Declare CurrentSession's class ( CurrentUser in the tutorial ) as Singleton in your Gin's module like below :

public class YourGinModule extends AbstractGinModule {

    @Override
    protected void configure() {
        bind( CurrentSession.class ).in ( Singleton.class );
        ...
    }

}

Here you can find another example using GWTP Gatekeeper on client side and Spring Security on server side : https://github.com/imrabti/gwtp-spring-security



来源:https://stackoverflow.com/questions/37906753/how-to-inject-session-user-object-after-bootstrapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!