Siv*_*mar 5 java ajax atmosphere angularjs
我是 Atmosphere 框架和 websockets 的新手。
我正在开发一个单页应用程序,作为第一步,我想使用 AngularJs(客户端)和 Atmosphere websockets(服务器端)实现基本身份验证。
对于身份验证,一开始,我使用了普通的 servlet,并将 User bean 保存在 HttpSession 中。但是后来我明白了,我无法访问类中的会话属性(websocket服务)
@AtmosphereService(broadcaster = JerseyBroadcaster.class)
Run Code Online (Sandbox Code Playgroud)
因此,我创建了另一个 Login AtmosphereService,其中 suspend 方法检查用户以及用户是否存在,然后使用以下方法将 UserBean 存储在 ConcurrentMap 中针对 uuid 键,以便我将来可以访问。
public void createSession(AtmosphereResource resource) {
logger.debug("Create session ({})", resource.uuid());
ConcurrentMap<String, Object> session = new ConcurrentHashMap<String, Object>();
ConcurrentMap<String, Object> prevSession = sessionsByUuid.putIfAbsent(resource.uuid(), session);
if (prevSession != null) {
logger.warn("Session already exists ({})", resource.uuid());
}
}
Run Code Online (Sandbox Code Playgroud)
所以在客户端,由于我有 AngularJS 和配置的路由,当用户登录时,我将用户密钥存储在 rootscope 变量中,当路由更改时,它会按如下方式检查。
$rootScope.$on("$routeChangeStart", function(event, next, current) {
aNext = next;
if (next.templateUrl != "./partials/login.html") {
if($rootScope.loggedUser !=null && $rootScope.loggedUser!=""){
$location.path(nextPage);
}else{
$rootScope.goLoginPage();
}
}
});
Run Code Online (Sandbox Code Playgroud)
但现在的问题是如果用户刷新页面它会进入登录页面,因为 $rootScope.loggedUser 将不包含任何内容。
之前,当我使用简单的 servlet 进行身份验证时,我使用对 servlet 的 ajax 调用来检查用户是否存在于会话中,如果他存在,我在 $rootScope.loggedUser 中分配了用户密钥,并且路由用于正确更改。
现在,当我使用 AtmosphereService 而不是普通的 servlet 进行身份验证时,我如何向 servlet(我应该在其中访问 AtmosphereResource)发出请求,或者我应该编写另一个 AtmosphereService 来获取存储的 UserBean?