Adding interceptors for our services

Before we begin developing our services, as part of the design process, we decided that we wanted to restrict access to some methods based on the User role and that some methods would require a Transaction object to be present.

Securing methods with an interceptor

To be able to develop an interceptor that we can use in our services, there are a few pieces that must be created, as shown in the following list:

  1. We need to define an enum function for the possible roles using the following code:
    public enum RoleType {
        GUEST,
        USER,
        ORDER_PROCESSOR,
        ADMIN;
    }
  2. We also need an annotation that we can add to methods to inform CDI that we want them to be intercepted:
    @InterceptorBinding @Target( { TYPE, METHOD } ) @Retention( ...

Get JBoss Weld CDI for Java Platform 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.