[英]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.