简体   繁体   English

登录后在REST中提取JSESSIONID

[英]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.

相关问题 成功登录后会自动重新创建JSESSIONID - JSESSIONID is getting recreated automatically after successful login 登录后 JSessionId 发生变化,Angular 5 正在发送带有请求的新 JSessionID 并丢弃旧的 jsessionid - JSessionId changes after login and Angular 5 is sending new JSessionID with request and discarding the old jsessionid 登录 Vaadin8 后如何更改 JSESSIONID? - How to change JSESSIONID after login in Vaadin8? 登录后,JSessionID在资源请求上更改,这使会话无效 - JSessionID changes on resource request after login which invalidates the session 身份验证后无法重置JSESSIONID - Cannot Reset JSESSIONID After Authentication 创建会话后缺少JSESSIONID cookie - JSESSIONID cookie missing after creating session 正则表达式:如何从Cookie字符串中提取JSESSIONID Cookie值? - Regex: how to extract a JSESSIONID cookie value from cookie string? 如何从Dojo中的XHR请求中提取JSESSIONID cookie? - How do you extract the JSESSIONID cookie from an XHR request in Dojo? 有没有办法从自定义 Java servlet 中的 JSESSIONID 中提取 Maximo 用户名? - Is there a way to extract Maximo username from JSESSIONID inside of a custom Java servlet? Spring 安全登录不起作用,没有返回 JSESSIONID cookie 并且重定向失败 - Spring security login not working, no JSESSIONID cookie returned and redirect fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM