[英]Extract JSESSIONID in REST after login
I'm working on system which will allow users to create workspaces.After workspace creation user may login by providing username, password and workspace. 我正在开发允许用户创建工作区的系统。创建工作区后,用户可以通过提供用户名,密码和工作区来登录。 And i need jsessionid generated after login to create connection between workspace and jsessionid.
我需要在登录后生成jsessionid来创建工作空间和jsessionid之间的连接。
Login goes thru REST. 登录通过REST。
@POST
@Path("login")
@Produces(ContentType.APPLICATION_JSON_CHARSET_UTF_8)
public JsonObject login(LoginCredentials loginCredentials) throws UnAuthorizedException {
//login logic
}
I also have interceptor implementing ContainerResponseFilter, WriterInterceptor. 我还有拦截器实现ContainerResponseFilter,WriterInterceptor。
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext context)throws IOException {
//...
}
how can i get generated jsessionid (which i see in response header)? 我怎样才能生成jsessionid(我在响应头中看到)?
If you have a http request object you can get hold of the associated session. 如果您有一个http请求对象,则可以获取关联的会话。 Once you've got the session you can get its id
一旦你有了会话,你就可以获得它的身份
request.getSession(true).getId()
Passing true
to getSession
causes a new session to be created if one does not already exist. 将
true
传递给getSession
会导致创建一个新会话(如果尚未存在)。
Assuming you are using Jersey for the REST services, You can use eg 假设您使用Jersey作为REST服务,您可以使用例如
@Context HttpServletRequest request
in your service method signatures in order to access the response headers. 在您的服务方法签名中,以便访问响应标头。 Take a look at https://jersey.java.net/documentation/latest/jaxrs-resources.html#d0e2742 and Under what conditions is a JSESSIONID created?
看看https://jersey.java.net/documentation/latest/jaxrs-resources.html#d0e2742 , 在什么条件下创建了一个JSESSIONID? (this one is about JSessionID)
(这个是关于JSessionID的)
In your interceptor you can get a request coming to your service and extract Headers or anything you put in the Request (so your JSESSIONID as well): 在您的拦截器中,您可以获得一个请求到您的服务并提取Headers或您在Request中放置的任何内容(所以您的JSESSIONID也是如此):
HttpServletRequest request = (HttpServletRequest) message
.getExchange().getInMessage()
.get(AbstractHTTPDestination.HTTP_REQUEST);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.