在我的 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 bean 使用拦截器,而不必为每个 bean 声明它?