我正在使用本教程中描述的身份验证/授权机制构建休息服务: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<Object> singletons = new LinkedHashSet<Object>();
public ServerApplication() {
singletons.add(new SecurityInterceptor());
singletons.add( ... ); //add each of my rest resources
}
@Override
public Set<Class<?>> getClasses() {
HashSet<Class<?>> set = new HashSet<Class<?>>();
return set;
}
@Override
public Set<Object> getSingletons() {
return singletons;
}
}
Run Code Online (Sandbox Code Playgroud)
SecurityInterceptor:
@Provider
@ServerInterceptor
public class SecurityInterceptor implements javax.ws.rs.ext.ReaderInterceptor {
@Override
public Object aroundReadFrom(ReaderInterceptorContext context){
//code that is …Run Code Online (Sandbox Code Playgroud) 我正在使用WildFly 8在JavaEE 7中开发一个新的应用程序.我正在使用JAX-RS为远程应用程序提供RESTful服务接口.
HttpHeaders可以使用@Context注释在资源方法参数中注入类似对象的东西.由于该对象基于请求参数(当然,HTTP标头),我提出了创建自己的可注入User对象的想法,该对象是根据请求中存在的有效令牌创建的(类似于OAuth访问令牌) ).
所以,我希望实现这样的目标:
@Path("/resources")
public class MyResource {
@Path("/{id}")
@GET
public Response getById(@Context User user, @PathParam("id") long id) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
其中User是基于请求参数创建的可注入对象,例如可通过HttpHeaders对象访问的对象.当然,如果由于任何原因无法创建User对象,则提供程序也可以抛出异常并返回HTTP错误响应.
现在,我的问题是:
谢谢