[英]Name Binding(Jersey) in DropWizard
I want to create custom annotation with NameBinding of Jersey for Auth via JWT, I used this tech in another plain-java project and everything works perfect, but it's not working in DropWizard project. 我想通过JWT使用Jersey的NameBinding for Auth创建自定义批注,我在另一个纯Java项目中使用了此技术,并且一切正常,但是在DropWizard项目中不起作用。 As I know DW also use Jersey for REST. 据我所知,DW也将Jersey用于REST。 this is my test code, but it's not working. 这是我的测试代码,但是没有用。
Interface: 接口:
@NameBinding
@Retention(RUNTIME)
@Target({TYPE, METHOD})
public @interface Auth {}
Implementation: 执行:
@Auth
@Provider
public class AuthFilter implements ContainerRequestFilter {
private static final String SECRET = "SOME_SECRET_STRING";
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
try {
String userToken = requestContext.getHeaderString("token");
Claims body = Jwts.parser().setSigningKey(SECRET).parseClaimsJws(userToken).getBody();
if(body.getExpiration().before(new Date())) {
requestContext.abortWith(Response
.status(Response.Status.UNAUTHORIZED)
.entity("Token is expired")
.build());
}
} catch (Exception e) {
requestContext.abortWith(Response
.status(Response.Status.UNAUTHORIZED)
.entity("Error")
.build());
}
}
}
Rest: 休息:
@GET
@Path("/test")
@Auth
public String test() {
return "Hello, " + userName;
}
can anybody help with this issue? 有人可以帮助解决这个问题吗?
I found the solution and post the answer it would be helpful for somebody. 我找到了解决方案并发布了答案,它将对某人有所帮助。 I had to register the AuthFilter class in run method: 我必须在run方法中注册AuthFilter类:
environment.jersey().register(new AuthFilter());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.