tôi giải quyết vấn đề này bằng PreProcessInterceptor
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Securable {
String header() default "session-token";
}
@Provider
@ServerInterceptor
public class ValidationInterceptor implements PreProcessInterceptor, AcceptedByMethod {
@Context
private HttpServletRequest servletRequest;
@Override
public boolean accept(Class clazz, Method method) {
return method.isAnnotationPresent(Securable.class);
}
@Override
public ServerResponse preProcess(HttpRequest httpRequest, ResourceMethod resourceMethod) throws Failure,
WebApplicationException {
Securable securable = resourceMethod.getMethod().getAnnotation(Securable.class);
String headerValue = servletRequest.getHeader(securable.header());
if (headerValue == null){
return (ServerResponse)Response.status(Status.BAD_REQUEST).entity("Invalid Session").build();
}else{
// Validatation logic goes here
}
return null;
}
}
Chú thích @Securable sẽ được sử dụng trên dịch vụ REST mà cần phải được xác nhận.
@Securable
@PUT
public Response updateUser(User user)
Nếu bạn đang sử dụng Jersey thì hãy tạo chú thích 'x' và chú thích đề cập đến @NameBinding. Tạo ContainerRequestFilter và thêm chú thích 'x' vào bộ lọc này. Bây giờ trang trí các dịch vụ của bạn với chú thích 'x' – jaks