2017-04-18 80 views
0

我正在尝试为以下这些问题开发的REST API创建筛选器Best practice for REST token-based authentication with JAX-RS and JerseyContainerRequestFilter不在JAX-RS/RESTEasy应用程序中执行

问题是我调用过滤器似乎无法正常工作的任何方法。

这些都是我的课:

Secured.java

@NameBinding 
@Retention(RetentionPolicy.RUNTIME) 
@Target({ElementType.TYPE, ElementType.METHOD}) 
public @interface Secured { 

} 

AuthenticationFilter.java

@Secured 
@Provider 
@Priority(Priorities.AUTHENTICATION) 
public class AuthenticationFilter implements ContainerRequestFilter{ 

    @Override 
    public void filter(ContainerRequestContext requestContext) throws IOException { 
     // Get the HTTP Authorization header from the request 
     String authorizationHeader = 
      requestContext.getHeaderString(HttpHeaders.AUTHORIZATION); 

     // Check if the HTTP Authorization header is present and formatted correctly 
     if (authorizationHeader == null || !authorizationHeader.startsWith("Bearer ")) { 
      throw new NotAuthorizedException("Authorization header must be provided"); 
     } 

     // Extract the token from the HTTP Authorization header 
     String token = authorizationHeader.substring("Bearer".length()).trim(); 

     try { 

      // Validate the token 
      validateToken(token); 

     } catch (Exception e) { 
      requestContext.abortWith(
       Response.status(Response.Status.UNAUTHORIZED).build()); 
     } 
    } 

    private void validateToken(String token) throws Exception { 
     // Check if it was issued by the server and if it's not expired 
     // Throw an Exception if the token is invalid 
    } 

} 

RestService.java

@Path("/test") 
public class RestService { 

TestDAO testDAO; 

    @GET 
    @Secured 
    @Path("/myservice") 
    @Produces("application/json") 
    public List<Test> getEverisTests() { 
     testDAO=(TestDAO) SpringApplicationContext.getBean("testDAO"); 

     long start = System.currentTimeMillis(); 

     List<Test> ret = testDAO.getTests(); 

     long end = System.currentTimeMillis(); 

     System.out.println("TIEMPO TOTAL: " + (end -start)); 

     return ret; 

    } 
} 

RestApplication.java

public class RestApplication extends Application{ 
    private Set<Object> singletons = new HashSet<Object>(); 

    public RestApplication() { 
     singletons.add(new RestService()); 
     singletons.add(new AuthenticationFilter()); 
    } 

    @Override 
    public Set<Object> getSingletons() { 
     return singletons; 
    } 
} 

我失去了一些东西?提前致谢。

+0

确保您的'AuthenticationFilter'已注册。你的'Application'子类是什么样的? –

回答

0

的解决方案是以下resteasy这个页面并选择我用的是RestEasy的版本更新RestEasy的Jboss的的模块。

感谢您的答复!

0

我还不能发表评论所以这将进入一个答案:

我不明白@Secured机制是如何工作的。您是否尝试删除所有@Secured注释?过滤器应该对所有端点都有效。

如果它仍然无法正常工作,您可能需要在应用程序中手动注册它。

如果它之后你至少有上哪里寻找这个问题的提示工作...

+1

“@ Secured”是[名称绑定注释](http://stackoverflow.com/a/38523942/1426227)。它将过滤器绑定到一个或多个资源类和/或方法。 –

+1

很酷,我忽略了@NameBinding注释。谢谢! – martinw

1

AuthenticationFilter可能未注册。

很可能你在你的应用程序的某个地方有一个Application子类。用它来登记过滤器:

@ApplicationPath("api") 
public class ApiConfig extends Application { 

    @Override 
    public Set<Class<?>> getClasses() { 
     HashSet<Class<?>> classes = new HashSet<>(); 
     classes.add(AuthenticationFilter.class); 
     ... 
     return classes; 
    } 
} 
+0

感谢您的回答!我的过滤器被注册在我的应用程序子类中。问题是jboss安装中的resteasy模块。 – A1t0r

相关问题