Reputation: 101
I am trying to set up a request filter because I'd like to check some http header information before calling my logic. Unfortunately my filter never gets called in JBoss AS7.1.1. I already tried to update the RestEasy implementation (module) to 3.0.6 like described in the RestEasy documentation. It says you just have to replace the directories delivered by the new implementation zip-file and I did so. The AS started without any errors but the behavior does not change in any way. Each request stays unintercepted. I extracted this code from a more complex example but even this simple thing does not work:
Filter.java
import java.io.IOException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.ext.Provider;
@Provider
@Secured
public class SecurityFilter implements ContainerRequestFilter {
public void filter(ContainerRequestContext crc) throws IOException {
throw new IllegalStateException("Not in my house");
}
}
ItemsResource.java
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
@Path("/Items")
public class ItemsResource {
@GET
@Produces("application/json")
@Secured
public String getJson() {
return "{\"id:\" \"1\"}";
}
}
ApplicationConfig.java
@javax.ws.rs.ApplicationPath("rest")
public class ApplicationConfig extends Application {
}
Secured.java
import javax.ws.rs.NameBinding;
@NameBinding
public @interface Secured {}
I does not change anything if I try to use the interceptor and the resource without the annotation. I would expect all the requests have to be intercepted but non will. I don't have any glue what to do. May anybody help we with some piece of advice? Let be add some addition thing. I tried the same thing with Wildfly (JBoss 8.0.0 Final). Interception by using @NameBinding and applying a custom annotation does not work also but if I don't use the annotation and just annotate the interceptor with @Provider all of my request get intercepted. Do I have to migrate to Wildfly or what?
Upvotes: 1
Views: 6221
Reputation: 11
Add a Provider file under your META-INF/services , like : /META-INF/services/javax.ws.rs.ext.Providers , javax.ws.rs.ext.Providers is a file , the content is your path of filter like : com.hujiang.foe.instalment.liquidation.biz.impl.account.filter.ContextCleanFilter
Upvotes: 0
Reputation: 24367
You can use Resteasy PreProcessInterceptor
:
import org.jboss.resteasy.annotations.interception.ServerInterceptor;
import org.jboss.resteasy.spi.interception.PreProcessInterceptor;
import javax.ws.rs.ext.Provider;
@Provider
@ServerInterceptor
public class SecurityInterceptor implements PreProcessInterceptor {
@Override
public ServerResponse preProcess(HttpRequest request, ResourceMethod method) throws Failure, WebApplicationException {
if (/* Check if a user is currently authenticated */) {
// no current authenticated user: throw exception
throw new NotAuthenticatedException();
}
return null; // = continue without interrupting
}
}
Upvotes: 1
Reputation: 991
Your problem is already mentioned: Match Filter with specific Method through NameBinding on RESTeasy
you need to remove the @PreMatching and to make sure your Secured annotation is retained during Runtime (Retention = RUNTIME).
Upvotes: 0
Reputation: 351
We were in the same problem.
We used 3.0.8.Final version of RESTeasy with the POM dependencies.
Also update the modules of the JBOSS (EAp 6.1) with the content of the zip file resteasy-jaxrs-3.0.7.Final.
The only diferece is that in the filter we use:
@Provider @PreMatching
@Precedence("SECURITY")
public class SecurityInterceptor implements ContainerRequestFilter{
@Override
public void filter(ContainerRequestContext arg0) throws IOException {
...
}
}
In addition we also need to register manually the Providers in the web.xml with:
<context-param>
<param-name>resteasy.providers</param-name>
<param-value>com.neology.rest.SecurityInterceptor</param-value>
</context-param>
Other problem in that we get stuck was with the Spring integration.
First is to verify the order of the *Listeners
<!-- RESTeasy Listener 1st -->
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
<!-- Spring Listener 2nd -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
Please also check your security filter chain. If you have configured your rest servlet mapping something like /rest/*
you need to make Spring to don't filter the REST service. We use this in the Spring xml configuration:
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<mvc:resources mapping="/rest/**" location="/rest/" />
Hope this helps!
Upvotes: 4