简体   繁体   English

如何在没有 web.xml 的情况下应用 ContainerRequestFilter?

[英]How can I apply ContainerRequestFilter without web.xml?

I'm currently making a RESTapi using jersey 2.27 and jetty 9.4.我目前正在使用 jersey 2.27 和 jetty 9.4 制作 RESTapi。 In this server I'm trying to apply a filter:在此服务器中,我尝试应用过滤器:

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

    private static final String REALM = "example";
    private static final String AUTHENTICATION_SCHEME = "Bearer";

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {

        //Authentication code
    }

    private boolean isTokenBasedAuthentication(String authorizationHeader) {

    }

    private void abortWithUnauthorized(ContainerRequestContext requestContext) {


    }

    private void validateToken(String token) throws Exception {

    }
}

However, this filter isn't triggered.但是,不会触发此过滤器。

This is my endpoint:这是我的终点:

@Path("/authenticate")
public class AuthenticationEndpoint {

    Machine machine = Machine.getInstance();

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



    @POST
    @Path("/authenticate")
    @Secured
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response authenticateUser(
            AuthenticationRequest authenticationRequest){

    }

I don't have a web.xml and I wouldn't know how to actually get this filter to trigger.我没有 web.xml,我不知道如何真正触发这个过滤器。 Anyone have some advice for this?有人对此有什么建议吗? I'm having a hard time understanding this server filter configuration.我很难理解这个服务器过滤器配置。 PS: i left out the content of the methods since I thought it would be too chaotic, I will of course add it if it is deemed necessary. PS:方法的内容我觉得太乱就不说了,如果有需要我当然会补上。

You have to register the filter when you create the Application, something like您必须在创建应用程序时注册过滤器,例如

public class MyApplication extends ResourceConfig {
  register(AuthenticationFilter.class)
  // yada yada
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在没有web.xml的情况下在Jersey中使用ContainerRequestFilter - Use ContainerRequestFilter in Jersey without web.xml 启用没有web.xml的ContainerRequestFilter - Enable ContainerRequestFilter without web.xml 泽西岛1.x和Tomcat:如何以编程方式注册ContainerRequestFilter,即没有web.xml? - Jersey 1.x and Tomcat: how to register a ContainerRequestFilter programmatically, i.e. without web.xml? 将Jersey2 ContainerRequestFilter与web.xml中的过滤器标记一起使用 - Use Jersey2 ContainerRequestFilter with filter notation in web.xml 如何在没有web.xml的情况下将AJAX映射到servlet - How do I map AJAX to servlet without web.xml 如何绕过web.xml中的安全筛选器 - How can I bypass security filter in web.xml 如何将 UTF8 值放入 web.xml? - How can I put UTF8 value to web.xml? 我可以在组件上使用@Profile标记而不在Spring Boot或Web.xml中创建@Configuration类吗? - Can I use @Profile Tags on Components without creating an @Configuration class in Spring Boot or a Web.xml Wildfly web.xml安全性约束使用ContainerRequestFilter阻止JAX-RS方法的基本auth头 - Wildfly web.xml security constraint blocking basic auth header for JAX-RS methods using ContainerRequestFilter 如何在没有 web.xml 的情况下将 App Engine 项目更新到 Java 11? - How do I update an App Engine project to Java 11 without web.xml?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM