在我的 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-Bean 才能使用此拦截器?或者有没有办法配置 beans.xml 以对我的所有 CDI-beans 使用拦截器,而不必为每个单独的 bean 声明它?
最佳答案
我认为你做不到。不过,您可以通过使用构造型来节省一些打字时间:
@Named
@RequestScoped
@Secure
@Stereotype
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Secure {
}
然后仅使用 @Secure
注释您的 Bean
关于java-ee-6 - 配置拦截器以用于应用程序内的所有 CDI-Bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3674883/