【问题标题】:How to invalidate session by CXF Interceptor?如何通过 CXF 拦截器使会话无效?
【发布时间】:2015-06-16 08:27:53
【问题描述】:

我是 Cxf 网络服务的新手,对所提供的信息有点不知所措。我写了一个扩展 AbstractPhaseInterceptor 的 cxf Interceptor(cxf:outInterceptors)。我试图在服务执行后使当前会话无效。我的问题是:

  1. 我是否在构造函数中使用了正确的阶段? (阶段。发送)
  2. 如何从消息中获取会话信息?现在,对于下面的方法请求实例返回 null。

或者有没有更好的方法来解决这个问题?

  public ServiceSessionInvalidator() {
      super(Phase.SEND);
  }

@Override
public void handleMessage(Message message) throws Fault {

    System.out.println("Hitting Handler");

    HttpSession session;

    HttpServletRequest req = (HttpServletRequest)message.get(AbstractHTTPDestination.HTTP_REQUEST);

    session = (req != null)? req.getSession(): null;

    if(req == null ){

        System.out.println("Request is Null");

    }


}

输出:

16 Jun 2015 13:35:40 INFO   - Inbound Message
----------------------------

ID: 1

Address: http://localhost:8090/services/VariableService/variableService/

Http-Method: GET

---
---

Hitting Handler

Request is Null

16 Jun 2015 13:35:41 INFO   - Outbound Message

---------------------------

ID: 1

Response-Code: 200

【问题讨论】:

    标签: cxf


    【解决方案1】:
    @Resource
    WebServiceContext wsContext;
    @Context
    private HttpServletRequest request;
    
    
    
     private HttpServletRequest invalidateSession() {
        MessageContext mc = wsContext.getMessageContext();
        HttpServletRequest req;
        if (mc != null) {
            req = (HttpServletRequest) mc.get(MessageContext.SERVLET_REQUEST);
        } else {
            req = request;
        }
    
        req.getSession().invalidate();
    }
    

    【讨论】:

      【解决方案2】:

      我不确定这个,但请查看 CXF 阶段描述,例如 hereSend 是最后一个阶段,在“协议”和“字节”级别处理消息之后。 session的概念和HTTP协议有关,request也一样(见类名)。

      我的建议:试试Phase.MARSHAL

      【讨论】:

      • 我通过在入站阶段(INVOKE)使会话无效而不影响响应来使其工作。我不确定它是否会过早终止请求。但是请求成功了。
      猜你喜欢
      • 1970-01-01
      • 2011-06-07
      • 1970-01-01
      • 2017-08-02
      • 2023-04-07
      • 2011-12-25
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      相关资源
      最近更新 更多