Jac*_*ack 5 java tomcat annotations interceptor cdi
我正在尝试实现@Restricted注释,以用户只能在登录并具有特定角色时才能访问它们的方式来保护控制器方法.我在使用JSF和CDI的Tomcat 7上,所以没有EJB.只要注释接口未指定任何参数,就会调用拦截器.只要我添加一个@Nonbinding Role value() default Role.ADMIN;参数,拦截器和控制器方法都不会执行.也没有错误或例外.这是我的代码,我真的不知道它有什么问题:
注解:
@InterceptorBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
public @interface Restricted {
@Nonbinding Role value() default Role.ADMIN; // ###
}
Run Code Online (Sandbox Code Playgroud)
拦截器:
@Interceptor
@Restricted
public class RoleBasedRestrictingInterceptor implements Serializable {
@Inject
ISecurityManager security;
@AroundInvoke
public Object intercept(final InvocationContext ctx) throws Exception {
final Restricted annotation = ctx.getClass().getAnnotation(Restricted.class);
log.info("Intercepted, required role is: {}", annotation.value()); // ###
log.info("User is logged in: {}", security.isLoggedIn());
return ctx.proceed();
}
}
Run Code Online (Sandbox Code Playgroud)
控制器:
@Named("manageUsers")
@SessionScoped
public class ManageUsersBacking extends implements Serializable {
@Restricted(Role.ADMIN) // ###
public void testRestricted() {
log.info("testRestricted()");
}
}
Run Code Online (Sandbox Code Playgroud)
该###事件标志着什么已被修改或删除,使其重新工作.拦截器已正确定义WEB-INF/beans.xml,因为它在我的注释中没有角色参数.
16:04:33.772 [http-apr-8080-exec-11] INFO c.m.s.RoleBasedRestrictingInterceptor - User is logged in: true
16:04:33.772 [http-apr-8080-exec-11] INFO c.m.c.admin.ManageUsersBacking - testRestricted()
Run Code Online (Sandbox Code Playgroud)
今天我重新审视了这个特定的问题,并注意到它与 CDI 无关:
ctx.getClass().getAnnotation(Restricted.class)
显然,我的示例中没有类级别的注释。所以 getAnnotation() 返回null。相反,我应该使用以下内容:
ctx.getMethod().getAnnotation(Restricted.class)
虽然我不知道为什么那里没有任何例外。也许其他一些事情正在发生,我无法再重现,因为我将我的应用程序迁移到了 TomEE。