如何在 REST Jersey Web 应用程序中创建、管理、关联会话 [英] How to create, manage, associate a session in REST Jersey Web Application

查看:28
本文介绍了如何在 REST Jersey Web 应用程序中创建、管理、关联会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个 HTML5 UI 连接到后端(REST Jersey 到 Hibernate 和 DB 的业务逻辑).我需要为每个用户登录创建并维护一个会话,直到用户注销.

A HTML5 UI is connected to the backend (REST Jersey to business logic to Hibernate and DB). I need to create and maintain a session for each user login until the user logs out.

能否请您指导我可以使用哪些技术/API.是否也需要在 REST 客户端处理某些事情..

Can you please guide me on what technologies/ APIs can be used. Does something need to be handled at the REST Client end also..

推荐答案

将 JAX-RS 用于 RESTful Web 服务非常简单.这是基础知识.您通常通过 JAX-RS annotations,像这样一个:

Using JAX-RS for RESTful web services is fairly straightforward. Here are the basics. You usually define one or more service classes/interfaces that define your REST operations via JAX-RS annotations, like this one:

@Path("/user")
public class UserService {
    // ...
}

您可以通过这些注释将对象自动注入到您的方法中:

You can have your objects automagically injected in your methods via these annotations:

// Note: you could even inject this as a method parameter
@Context private HttpServletRequest request;

@POST
@Path("/authenticate")
public String authenticate(@FormParam("username") String username, 
        @FormParam("password") String password) {

    // Implementation of your authentication logic
    if (authenticate(username, password)) {
        request.getSession(true);
        // Set the session attributes as you wish
    }
}

HTTP 会话 可以从 HTTP 请求 对象通过 getSession()getSession(boolean) 像往常一样.其他有用的注释有 @RequestParam@CookieParam 甚至 @MatrixParam 等等.

HTTP Sessions are accessible from the HTTP Request object via getSession() and getSession(boolean) as usual. Other useful annotations are @RequestParam, @CookieParam or even @MatrixParam among many others.

有关更多信息,您可能需要阅读 RESTEasy 用户指南泽西岛用户指南,因为两者都是极好的资源.

For further info you may want to read the RESTEasy User Guide or the Jersey User Guide since both are excellent resources.

这篇关于如何在 REST Jersey Web 应用程序中创建、管理、关联会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆