我正在使用本教程中描述的身份验证/授权机制来构建Rest服务:http :
//howtodoinjava.com/2013/06/26/jax-rs-resteasy-basic-authentication-and-
authorization-tutorial/
基本上,它使用PreProcessInterceptor接口扫描目标方法中的注释(来自javax.annotation.security包),这些注释描述访问该方法所需的角色。由于此处的身份验证器是拦截器,它可以取消目标方法的调用,并在需要时返回401(未授权)。
这里的问题是在当前的RestEasy版本(3.0.1)中不推荐使用org.jboss.resteasy.spi.interception.PreProcessInterceptor接口,而在使用标准JAX-
RS接口实现相同行为时遇到问题。
我正在使用javax.ws.rs.ext.ReaderInterceptor接口来拦截呼叫。但是服务器以某种方式从未调用它:拦截器只是被忽略。
我以与以前的PreProcessInterceptor相同的方式注册拦截器/资源,并使用相同的@Provider和@ServerInterceptor批注:
ServerApplication:
public class ServerApplication extends javax.ws.rs.core.Application {
private final HashSet singletons = new LinkedHashSet();
public ServerApplication() {
singletons.add(new SecurityInterceptor());
singletons.add( ... ); //add each of my rest resources
}
@Override
public Set> getClasses() {
HashSet> set = new HashSet>();
return set;
}
@Override
public Set getSingletons() {
return singletons;
}
}
安全拦截器:
@Provider
@ServerInterceptor
public class SecurityInterceptor implements javax.ws.rs.ext.ReaderInterceptor {
@Override
public Object aroundReadFrom(ReaderInterceptorContext context){
//code that is never called... so lonely here...
}
}
关于如何解决此问题的任何见解?
谢谢。