从 EJB 拦截器抛出异常

发布于 2024-10-07 01:31:02 字数 750 浏览 0 评论 0原文

假设我有一个看起来像这样的拦截器:

public class AuthorizationInterceptor {

  Logger log = Logger.getLogger(getClass().getName());

  @AroundInvoke
  private Object authorize(InvocationContext ic) throws Exception{
    // ... some other logic for authorization

    if (!allowedMethods.contains(ic.getMethod().getName())){
      log.info("Authorization failed. Preparing to throw exception");
      throw new AuthException("Authorization failed for method " +
                ic.getMethod().getName());
    }

    return ic.proceed();
  }
}

它应用于与我的 EJB 不同的方法。

我通常希望将异常传递给调用客户端,就像所有正常的 EJB 异常一样。

显然,如果我从拦截器中抛出它,就不会发生这种情况......它甚至没有登录到服务器;就像它从未被抛出一样,尽管它是这样的 - return 语句从未被执行。

我做错了什么?

我用的是GF 3.0.1

Let's say I have an interceptor that looks smth like this:

public class AuthorizationInterceptor {

  Logger log = Logger.getLogger(getClass().getName());

  @AroundInvoke
  private Object authorize(InvocationContext ic) throws Exception{
    // ... some other logic for authorization

    if (!allowedMethods.contains(ic.getMethod().getName())){
      log.info("Authorization failed. Preparing to throw exception");
      throw new AuthException("Authorization failed for method " +
                ic.getMethod().getName());
    }

    return ic.proceed();
  }
}

which is applied to different methods from my EJBs.

I would normally expect the exception throed to be passed to the invoking client, like all normal EJB exceptions.

Apparently this doesn't happen if I throw it from an Interceptor... It's not even logged on the server; like it's never thrown although it is - the return statement is never executed.

What am I doing wrong?

I'm using GF 3.0.1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

GRAY°灰色天空 2024-10-14 01:31:02

对这个问题进行一番搜索后,我发现 这篇 SO 帖子 几分钟前得到了答复。引用:

我认为没有正确的方法
这样做。方法应该只抛出
他们声明的例外情况,以及
拦截器不应添加新拦截器。
我的个人情况通过添加得到解决
我们默认异常的错误代码
所有方法都会抛出该异常。

问题作者是回答并接受这个答案的同一个人,所以我猜他正在尝试解决与您相同的问题,并得出了无法完成的结论。

After searching a bit for this issue, I found this SO post which was answered a few minutes ago. Quote:

I don't think there is a correct way
to do that. Methods should throw only
the exceptions they declared, and an
interceptor shouldn't add a new one.
My personal case got fixed by adding
an error code to our default exception
which is thrown by all methods.

Question author is the same person who answered and accepted this answer, so I guess he was trying to solve the same issue as you and came to conclusion that it cannot be done.

萌梦深 2024-10-14 01:31:02

这里有一些可以尝试的事情:

1. Check that the authorize(...) method is called.
2. Try making the authorize(...) method public instead of private.
3. Check that the EJB has an annotation like this:
      @Interceptors(AuthorizationInterceptor.class)

Here are a couple of things to try:

1. Check that the authorize(...) method is called.
2. Try making the authorize(...) method public instead of private.
3. Check that the EJB has an annotation like this:
      @Interceptors(AuthorizationInterceptor.class)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文