首页 > 解决方案 > 拦截器没有捕获 EJBAccessException

问题描述

我正在使用 Wildfly 12 (EE 8)。

我试图通过我的拦截器捕获 EJBAccessException 以将其重新抛出为我自己的异常类型,但无济于事。我的 Interceptor 中的代码在被抛出时根本不会被调用。它确实有效并且确实捕获了其他异常,所以我不确定出了什么问题。关于这个“内部”问题,EJB 是否以某种方式无法识别我的拦截器,或者它是否可能仅在 Wildfly 自己的 AuthorizationInterceptor 被调用后才被调用?

我的 ejb-jar.xml:

<interceptors>
    <interceptor>
        <interceptor-class>myExceptionInterceptor</interceptor-class>
    </interceptor>
</interceptors>
<assembly-descriptor>
    <interceptor-binding>
        <ejb-name>*</ejb-name>
        <interceptor-class>myExceptionInterceptor</interceptor-class>
    </interceptor-binding>
</assembly-descriptor>

我的 beans.xml 是空的,我也尝试将 Interceptor 声明放在那里,但没有任何效果。

我的拦截器:

@Throws
@Interceptor
@Priority(Interceptor.Priority.APPLICATION) // Tried playing with this too
public class myExceptionInterceptor implements Serializable {
    @AroundInvoke
    public Object check(InvocationContext invocationContext) throws Exception {
    try {
        return invocationContext.proceed();
    }
    catch (javax.ejb.EJBAccessException e) {
        //rethrow as own exception
    }

我关于 EJB 安全性的standalone.xml 的一部分:

<default-security-domain value="jaspitest"/>
<default-missing-method-permissions-deny-access value="true"/>

我的拦截器绑定:

@Inherited
@InterceptorBinding
@Retention(RetentionPolicy.RUNTIME)
@Target( {ElementType.METHOD, ElementType.TYPE} )
public @interface Throws {}

我正在调用它的示例 bean:

@Stateless
public class UserService extends Service<User> {
   @RolesAllowed(Role.ADMIN)
   public void delete() {}
}

标签: jakarta-eeejbwildfly

解决方案


这不起作用,因为您的拦截器必须能够访问与其关联的业务方法所具有的相同javax.ejb.EJBContext对象。请参阅 EJB 规范 (3.2) §4.7.2 中的表 2。

因此,在调用拦截器之前应用安全检查。

解决这个问题的唯一方法是将您的服务包装在一个不安全的外观中,并将您的拦截器应用于该外观。


推荐阅读