1

我想这可能是不可能的,但以防万一......

我们有一个前端 Java 应用程序,它通过 Hessian 与(我们的)后端应用程序进行通信。出于日志记录和审计目的,我们需要将登录(在前端)的用户的 user_id 传递给后端。一个明显的解决方案当然是为所有方法调用添加一个 String 参数,但这需要大量工作并且不是很干净。

有没有办法拦截呼叫,用 user_id 装饰它(从会话中检索)并在后端获取该 user_id(然后将其设置在 ThreadLocal 等)?

4

1 回答 1

0

这是可能的:

public class CustomHessionProxyFactoryBean extends HessianProxyFactoryBean {

public CustomHessionProxyFactoryBean() {
    setProxyFactory(new CustomHessianProxyFactory());
}

private static class CustomHessianProxyFactory extends HessianProxyFactory {

    @Override
    public Object create(Class<?> api, URL url, ClassLoader loader) {
        return Proxy.newProxyInstance(loader, new Class[] { api, HessianRemoteObject.class }, new CustomHessianProxy(url, this, api));
    }

    private static class CustomHessianProxy extends HessianProxy {

        protected CustomHessianProxy(URL url, HessianProxyFactory factory) {
            super(url, factory);
        }

        public CustomHessianProxy(URL url, HessianProxyFactory factory, Class<?> type) {
            super(url, factory, type);
        }

        @Override
        protected void addRequestHeaders(HessianConnection conn) {
            super.addRequestHeaders(conn);

            if(StringUtils.isNotBlank(SsoIdUtil.getThreadSsoId())){
                conn.addHeader("some_key", "some_value_from_threadlocal");
            }
        }
    }
}

}

于 2013-07-19T15:38:42.277 回答