Implementing the CDI interceptor

To implement the CDI interceptor, we need to create a qualifier to be used to configure a class or method to be intercepted. In the following code block, we have a qualifier, called Authentication, with a parameter, called roles, and these are used to declare the roles that are permitted to access a resource:

import javax.enterprise.util.Nonbinding;import javax.interceptor.InterceptorBinding;import java.lang.annotation.*;@Inherited@InterceptorBinding@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.METHOD, ElementType.TYPE})public @interface Authentication {    @Nonbinding String[] roles() default {};}

In the following code block, we have the AuthenticationInteceptor class, which is used as an interceptor. ...

Get Java EE 8 Design Patterns and Best Practices now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.