In the following code snippet, we have a JAX-RS resource class, called HelloWorld. This class receives a request and returns a message to the client. Before the execution of the resource logic, AuthenticationInterceptor intercepts a call to a method and processes the authentication logic:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
@Path("helloworld")
public class HelloWorld {
@GET
@Authentication(roles = {"user"})
public Response helloWorld(@Context HttpServletRequest request,
@Context HttpServletResponse response) {
return Response
.ok("Hello World. Welcome to App with validation by
authentication interceptor!")
.build();
}
}
Note that the helloWorld(@Context HttpServletRequest request, @Context HttpServletResponse response) method has the @Authentication(roles={"user}) annotation, along with the HttpServletRequest and HttpServletResponse parameters. With @Authentication(roles={"user}), we configured the helloWorld(@Context HttpServletRequest request, @Context HttpServletResponse response) method to be intercepted by AuthenticationInterceptor. As previously mentioned, the target method must have HttpServletRequest and HttpServletResponse as parameters:
@GET
@Authentication(roles = {"user"})
public Response helloWorld(@Context HttpServletRequest request, @Context HttpServletResponse response) {
...
}