有没有办法从 CDI 拦截器中检索会话 ID?

Is there any way to retrieve the session id from a CDI interceptor?

我已经创建了一个标准的 CDI (WELD) 拦截器来记录方法调用:

@MyInterceptorBinding
@Interceptor
public class MyInterceptor implements Serializable {

    @AroundInvoke
    public Object interceptMethod(InvocationContext ctx) throws Exception {

        // Do some Logging Operations

        try {
            Object result = ctx.proceed();
            return result;
        } catch (Exception e) {
            throw e;
        }
    }
}

我还想记录从中调用该方法的会话 ID(也记录请求 ID 会很棒!)。

有什么办法吗?

是的。

因此您需要创建一个 @WebFilter 安装在您的应用程序中,将会话 ID 存储在 requestScoped bean 中。然后你可以将所述 requestScoped bean 注入你的拦截器并检索它。