gpt4 book ai didi

java - 引导后如何注入(inject) session /用户对象?

转载 作者:搜寻专家 更新时间:2023-11-01 00:55:02 25 4
gpt4 key购买 nike

有一些例子将不同种类的对象注入(inject)到演示者中,但我找不到如何做到这一点的解释。

Bootstrap-Code他们正在注入(inject)的例子一个 SecurityDelegate 对象。

也在Gatekeeper例如,我看到正在注入(inject)的东西,例如MyGatekeeper,但是这是怎么做到的呢?

我想要的是首先检查用户是否登录,然后创建一个 CurrentSession 对象或类似的东西。但是我怎样才能传递/注入(inject)这个对象呢?

目前我正在初始化一个单例对象 CurrentUser 恕我直言,这有点丑陋。我想让 GWTP 支持运行,但是如何?


以将 CurrentSession 注入(inject)到网守中为例:

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

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

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

如何在此处注入(inject) CurrentSession

最佳答案

这是一个说明如何使用 Gatekeeper 的教程:http://dev.arcbees.com/gwtp/tutorials/tutorial-part2.html

在您的 Gin 模块中将 CurrentSession 的类(教程中的 CurrentUser)声明为 Singleton,如下所示:

public class YourGinModule extends AbstractGinModule {

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

}

在这里您可以找到另一个在客户端使用 GWTP Gatekeeper 并在服务器端使用 Spring Security 的示例:https://github.com/imrabti/gwtp-spring-security

关于java - 引导后如何注入(inject) session /用户对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37906753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com