配置拦截器以用于应用程序内的所有 CDI-Bean

发布于 2024-09-19 04:03:05 字数 1233 浏览 4 评论 0原文

在我的 JEE6-CDI-webapp 中,我声明了一个安全拦截器,如下所示:

//Secure.java
@Inherited
@Target({TYPE, METHOD})
@Retention(RUNTIME)
@InterceptorBinding
public @interface Secure
{}

//SecurityInterceptor.java
@Secure
@Interceptor
public class SecurityInterceptor
{
    @AroundInvoke
    protected Object invoke(InvocationContext ctx) throws Exception
    {
        // do stuff
        ctx.proceed();
    }
}

并在 beans.xml 中声明它:

//beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
      http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
   <alternatives/>
   <decorators/>
   <interceptors>
     <class>com.profitbricks.security.SecurityInterceptor</class>
   </interceptors>
</beans>

要使用它,我相应地注释 CDI-bean:

//CDI bean using Inteceptor
@Named @RequestScoped
@Secure
public class TestBean {
    public String doStuff() {
    }
}

现在我问自己,我是否必须注释所有我的 CDI-Beans 使用这个拦截器?或者有没有办法配置 beans.xml 以对我的所有 CDI-beans 使用拦截器,而不必为每个单独的 bean 声明它?

In my JEE6-CDI-webapp, i declared a Security Interceptor, like this one:

//Secure.java
@Inherited
@Target({TYPE, METHOD})
@Retention(RUNTIME)
@InterceptorBinding
public @interface Secure
{}

//SecurityInterceptor.java
@Secure
@Interceptor
public class SecurityInterceptor
{
    @AroundInvoke
    protected Object invoke(InvocationContext ctx) throws Exception
    {
        // do stuff
        ctx.proceed();
    }
}

And declared it inside the beans.xml:

//beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
      http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
   <alternatives/>
   <decorators/>
   <interceptors>
     <class>com.profitbricks.security.SecurityInterceptor</class>
   </interceptors>
</beans>

To use it, i annotate a CDI-bean accordingly:

//CDI bean using Inteceptor
@Named @RequestScoped
@Secure
public class TestBean {
    public String doStuff() {
    }
}

Now i ask myself, do i have to annotate ALL my CDI-Beans to use this interceptor? Or is there a way to configure the beans.xml to use the interceptor for all my CDI-beans, without having to declare it for every single bean?

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

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

发布评论

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

评论(4

从来不烧饼 2024-09-26 04:03:05

我认为你不能。但是,您可以通过使用构造型来节省一些打字时间:

@Named
@RequestScoped
@Secure
@Stereotype
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Secure {

}

然后仅使用 @Secure 注释您的 bean

I don't think you can. You can however spare a bit of typing by using stereotypes:

@Named
@RequestScoped
@Secure
@Stereotype
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Secure {

}

And then annotate your beans with only @Secure

三寸金莲 2024-09-26 04:03:05

您可以尝试使用我几个月前编写的小型 CDI 扩展:

https://github.com/struberg/InterDyn

这将允许您通过正则表达式样式动态地将 CDI 拦截器应用到一堆类。

它很快就会成为 Apache MyFaces CODI 的一部分,我只需要先找一些时间清理配置部分;)

You could try to use the small CDI Extension I wrote a few months ago:

https://github.com/struberg/InterDyn

This will allow you to dynamically apply CDI Interceptors to a bunch of classes via regexp style.

It will soon be part of Apache MyFaces CODI, I just need to find some time to cleanup the config part first ;)

旧情别恋 2024-09-26 04:03:05

这些可能会晚一些,但我遇到了一个需要全局/应用程序范围拦截器的要求。

要启用应用程序拦截器,请将拦截器注释为:

@Priority(Interceptor.Priority.APPLICATION)
@Interceptor
@Logging
public class MyLoggingInterceptor {}

在这种情况下,好消息是您不必在 beans.xml 中声明拦截器:

Oracle javaee7 教程

These may be late, but i encountered a requirement for which i needed a global/application wide interceptor.

To enable application interceptor, annotate the interceptor as:

@Priority(Interceptor.Priority.APPLICATION)
@Interceptor
@Logging
public class MyLoggingInterceptor {}

In this case, the good news is that you dont have to declare the interceptor in the beans.xml:

Oracle javaee7 tutorial

如梦初醒的夏天 2024-09-26 04:03:05

所有的豆子都没有用。您可以在引导期间操作 bean - 例如 codi-addons 的 ultra_lite_ejbs(请参阅 bitbucket org)使用它。也许这对你来说是一个灵感。恕我直言,例如像 openwebbeans.apache.org 这样的社区将更适合您的 CDI 相关问题。

All beans aren't useful. You can manipulate beans during bootstrapping - e.g. ultra_lite_ejbs at codi-addons (see bitbucket org) uses it. Maybe it's an inspiration for you. IMHO e.g. a community like openwebbeans.apache.org would be a better fit for your CDI related questions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文