简体   繁体   English

如何将session_id用于REST调用?

[英]How to use session_id for REST calls?

I'm making a multiple service calls to a service. 我正在对一个服务进行多个服务调用。 Let's say i have to make 10 POST calls to a service. 假设我必须对服务进行10次POST调用。 Each service require session_id cookie. 每个服务都需要session_id cookie。 If it's not provided, then the service will generate one and use it for all the service calls. 如果未提供,则该服务将生成一个并将其用于所有服务调用。

In my app i make several consecutive service calls. 在我的应用程序中,我进行了多次连续的服务呼叫。 I can invoke those calls again in the "loop", but i need a different session_id for a single iteration. 我可以在“循环”中再次调用这些调用,但是单次迭代需要使用不同的session_id。

In the code MyService component is making a service call. 在代码中,MyService组件正在进行服务调用。 Filter of that service will generate session_id. 该服务的过滤器将生成session_id。 But invoke() method might be invoked multiple times and I want filter to generate a different session_id for each invocation of invoke() method. 但是invoke()方法可能会被多次调用,我希望过滤器为invoke()方法的每次调用生成一个不同的session_id。 Currently, session_id is getting generated once 目前,session_id生成一次

@Component
class MyService {
    @Autowired
    private RestTemplate restTemplate;

    /**
     * T - is a request type
     * R - is a response class type
    */
    private <T,R> Optional<R> doPost(String url, T request, Class<R> responseType) {
        return Optional.ofNullable(restTemplate.postForObject(url, request, responseType));
    }

    public void invoke() {
         doPost("url1", someRequest1, SomeResponse1.class);
         doPost("url2", someRequest2, SomeResponse2.class);
    }
}

I can create a session_id in invoke() method and make sure that a different session_id is used to make these calls. 我可以在invoke()方法中创建一个session_id,并确保使用不同的session_id进行这些调用。 All service calls in invoke() will have the same session_id and next invocation will have a different one. invoke()中的所有服务调用将具有相同的session_id,而下一次调用将具有不同的session_id。 But i am not sure if this approach is a right way to achieve this goal. 但是我不确定这种方法是否是实现此目标的正确方法。

What can you advise me to use? 您可以建议我使用什么?

session_id is generated by each client (if you test from other web browser you will see a different session_id), the only way that receive a different for each request is invalidating the session, althoung if your require a uuid by request use a listener session_id由每个客户端生成(如果您从其他Web浏览器进行测试,您将看到一个不同的session_id),则为每个请求接收不同内容的唯一方法是使会话无效,如果您要求按请求使用uuid,则使用监听器

public class ProjectRequestListener implements ServletRequestListener {    

 @Override
 public void requestInitialized(ServletRequestEvent requestEvent) {
     requestEvent.getServletRequest().setAttribute("requestId", UUID.randomUUID());
 }

 @Override
 public void requestDestroyed(ServletRequestEvent requestEvent) {
    requestEvent.getServletRequest().setAttribute("requestId", null);
 }
}

and add the listener to the project web.xml 并将侦听器添加到项目web.xml

<listener>
    <listener-class>
    com.project.server.ProjectRequestListener
    </listener-class>
</listener>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在REST API中为app(android端)使用身份验证令牌而不是session_id - why use authentication token instead of session_id for app(android side) in REST API 会话ID如何存储在Jersey REST服务中 - How session id is stored in Jersey REST service 如何在多个 api 调用中使用相同的跟踪 ID - How to use the same trace id in multiple api calls 如何优化REST API调用 - How to Optimize REST API calls 我应该使用POJO或JSONObject进行REST调用 - Shall I use POJO or JSONObject for REST calls 如何配置JMeter始终使用新的SSL会话ID? - How to configure JMeter to always use a new SSL session id? 如何限制具有有效会话ID的用户使用Rest Client访问我们的服务? - How do i restrict a User Accessing our services using Rest Client with a valid Session ID? 如何使用基于管理证书的身份验证对Azure进行REST API调用? - How to use Management certificate based authentication for making REST API calls to Azure? 如何从android(java)中的json响应中获取内部索引对象,该对象对rest api调用使用改造 - How to get inner indexed object from json response in android (java) which use retrofit for rest api calls 如何管理休息服务中的会话? - How to manage session in rest services?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM