【问题标题】:ContainerRequestFilter is not executed in JAX-RS / RESTEasy applicationContainerRequestFilter 未在 JAX-RS / RESTEasy 应用程序中执行
【发布时间】:2017-09-14 10:54:29
【问题描述】:

我正在尝试为我根据这些问题Best practice for REST token-based authentication with JAX-RS and Jersey 开发的 REST API 创建一个过滤器。

问题是我调用过滤器的任何方法似乎都不起作用。

这些是我的课程:

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

我错过了什么?提前致谢。

【问题讨论】:

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

标签: java web-services rest resteasy


【解决方案1】:

解决方案是按照此页面resteasy 更新resteasy 的Jboss 模块并选择我正在使用的resteasy 版本。

顺便谢谢你的回答!

【讨论】:

    【解决方案2】:

    您的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;
        }
    }
    

    【讨论】:

    • 感谢您的回答!我的过滤器在我的应用程序子类中注册。问题是 jboss 安装中 resteasy 的模块。
    【解决方案3】:

    我还不能发表评论,所以这是一个答案:

    我不明白@Secured 机制是如何工作的。您是否尝试删除所有 @Secured 注释?然后过滤器应该对所有端点都处于活动状态。

    如果它仍然无法正常工作,很可能您将不得不在您的应用程序中手动注册它。

    如果它在之后确实有效,您至少可以提示在哪里寻找问题...

    【讨论】:

    • @Securedname binding annotation。它将过滤器绑定到一个或多个资源类和/或方法。
    • 酷,我忽略了@NameBinding 注释。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2017-06-18
    • 1970-01-01
    • 2014-02-03
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    相关资源
    最近更新 更多