2014-03-12 124 views
1

我想设置请求过滤器,因为我想在调用我的逻辑之前检查一些http头信息。不幸的是我的过滤器从来没有在JBoss AS7.1.1中被调用。我已经尝试将RestEasy实现(模块)更新为3.0.6,如​​RestEasy文档中所述。它说你只需要替换新的实现压缩文件提供的目录,我就这么做了。 AS启动时没有任何错误,但行为不以任何方式进行更改。每个请求保持不被拦截。我这个提取代码更复杂的例子,但即使这样简单的东西不起作用:ContainerRequestFilter JBoss AS 7.1.1 RestEasy 3.0.6.Final

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 {} 

如果我尝试了使用的拦截和资源没有标注我不会改变任何东西。我希望所有的请求都被拦截,但不会。我没有任何胶水做什么。有人可以帮我们提供一些建议吗?让添加一些额外的东西。我用Wildfly(JBoss 8.0.0 Final)尝试了同样的事情。使用@NameBinding进行拦截并应用自定义注释也不起作用,但如果我不使用注释并仅用@Provider注释拦截器,则我的所有请求都会被拦截。我必须迁移到Wild or还是什么?

+0

可能的重复[Match Filter with specific方法通过NameBinding RESTeasy](http://stackoverflow.com/questions/15119309/match-filter-with-specific-method-through-name-binding-on-resteasy) –

回答

3

我们遇到了同样的问题。 我们使用3.0.8.Final版本的RESTeasy与POM依赖关系。
还与zip文件resteasy-jaxrs-3.0.7.Final的内容更新JBOSS(EAP 6.1)的模块。
唯一diferece的是,在过滤器,我们使用:

@Provider @PreMatching 
@Precedence("SECURITY") 
public class SecurityInterceptor implements ContainerRequestFilter{ 
@Override 
public void filter(ContainerRequestContext arg0) throws IOException { 
    ... 
    } 
} 

此外,我们还需要在web.xml中手动注册提供商有:

<context-param> 
    <param-name>resteasy.providers</param-name> 
    <param-value>com.neology.rest.SecurityInterceptor</param-value> 
</context-param> 

我们遇到的其他问题与集成有关。
首先是验证*听众

<!-- 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> 


顺序也请检查您的安全过滤器链。如果你已经配置好了你的rest servlet映射如/rest/*,那么你需要让Spring到不要过滤的REST服务。我们在Spring xml配置中使用这个:

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> 
<mvc:resources mapping="/rest/**" location="/rest/" /> 

希望这有助于!

1

您可以使用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 
    } 
} 
+4

PreProcessInterceptor是depreca摊晒 – Reza

0

添加提供程序文件的META-INF /服务,如下:/META-INF/services/javax.ws.rs.ext.Providers,使用javax。 ws.rs.ext.Providers是一个文件,内容是你的过滤路径,如:com.hujiang.foe.instalment.liquidation.biz.impl.account.filter.ContextCleanFilter

相关问题