Search code examples
java-ee-6cdiinterceptor

Interceptors are not intercepting


I am having problems setting up a Java EE 6 CDI interceptors. I am using embedded glassfish, I have specified the interceptor in beans.xml in the web application.

<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">
    <interceptors>
        <class>ServiceInterceptor</class>
    </interceptors>
</beans>

I am trying to secure this bean:

@Named
//@Stateless
@RequestScoped
public class SecuredMethodJSFBean /*implements Serializable*/{
    @Inject
    protected SecuredMethodSample securedMethodSample;

    /*
    @CurrentUser
    @SessionScoped
    @Inject
    protected RuntimePrincipalAware principal;
    //protected JSFLoginBean jsfLoginBean;
    */

    public SecuredMethodJSFBean()
    {
        super();
        System.out.println("creating secured method jsf bean");
    }

    @Secured("adfadfafd")
    public void doSomething()
    {
        //System.out.println("\n\n\n\nprincipal:" + principal);
        //System.out.println("principal:" + jsfLoginBean.getPrincipal());

        //securedMethodSample.doSomething(jsfLoginBean.getPrincipal().getName());
        //return(jsfLoginBean.getPrincipal().getName());

        //securedMethodSample.doSomething(principal.getName());
        //return(principal.getName());
        //return("secured-method");

        securedMethodSample.doSomething("testing ...");
    }
}

What do I need to do to get my interceptors running?

Also, I am trying to use interceptors to intercept method invocations on beans used by servlets. Since those beans are beans, I should be able to intercept them. However, I am unable to do so. I was originally trying to intercept method invocations in servlets directly, but they're not CDI beans so that does not make sense.

Thanks,

Walter


Solution

  • To answer my question better, I did the following:

    1. I made the project ejb instead of jar (inside maven).
    2. I created an ejb-jar.xml file specifying / declaring my interceptor (src/main/resources/META-INF).
    3. in projects using this interceptor, I specified the type as ejb to it would pick it up correctly.

    Walter