获取 ContainerRequestFilter 中的资源类注释值
2022-09-03 04:57:46
我正在努力理解休息拦截器注释如何添加稍后在过滤器中可见的不同值。给定下面的代码,我希望一旦进入过滤器,权限值就会有 foo 和 bar,但是它们是空的。任何帮助将不胜感激。
注解
package edu.psu.swe.fortress.poc.interceptor;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.enterprise.util.Nonbinding;
import javax.ws.rs.NameBinding;
@NameBinding
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(value=RetentionPolicy.RUNTIME)
public @interface FortressProtected
{
@Nonbinding String[] permissions() default {};
}
滤波器
package edu.psu.swe.fortress.poc.interceptor;
import java.io.IOException;
import java.lang.annotation.Annotation;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.ext.Provider;
@Provider
@FortressProtected
public class FortressAuthorizer implements ContainerRequestFilter
{
@Override
public void filter(ContainerRequestContext requestContext) throws IOException
{
System.out.println("In the interceptor");
Class<?> clazz = this.getClass();
FortressProtected annotation = clazz.getAnnotation(edu.psu.swe.fortress.poc.interceptor.FortressProtected.class);
System.out.println("Annotation? " + clazz.isAnnotation());
for (Annotation a : clazz.getAnnotations())
{
System.out.println(a);
}
for (String s : annotation.permissions())
{
System.out.println(s);
}
}
}
应用配置
package edu.psu.swe.fortress.poc.rest;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import edu.psu.swe.fortress.poc.interceptor.FortressAuthorizer;
import edu.psu.swe.fortress.poc.interceptor.FortressProtected;
@ApplicationPath("")
public class FortressTestApp extends Application
{
private Set<Class<?>> clazzez_ = new HashSet<>();
{
clazzez_.add(ResourceImpl.class);
clazzez_.add(FortressProtected.class);
clazzez_.add(FortressAuthorizer.class);
}
public Set<Class<?>> getClasses()
{
return clazzez_;
}
}
资源类
package edu.psu.swe.fortress.poc.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import edu.psu.swe.fortress.poc.interceptor.FortressProtected;
@FortressProtected(permissions={"foo", "bar"})
@Path("tests")
public class ResourceImpl
{
@GET
@Produces("application/text")
public String getHello()
{
FortressProtected annotation = this.getClass().getAnnotation(edu.psu.swe.fortress.poc.interceptor.FortressProtected.class);
System.out.println(annotation.toString());
return "hello";
}
}
日志输出如下所示:
15:59:55,223 INFO [stdout] (default task-9) @edu.psu.swe.fortress.poc.interceptor.FortressProtected(permissions=[]) 15:59:55,229 INFO [stdout] (default task-9) @edu.psu.swe.fortress.poc.interceptor.FortressProtected(permissions=[foo, bar])
提前致谢。