【发布时间】:2015-05-09 03:13:40
【问题描述】:
我有一个要求以编程方式添加授权(权限)约束(此处不是身份验证)。我有一个应用程序范围的 CDI 托管 bean,如下所示。
@Named
@ApplicationScoped
public class Bean {
@Inject
private Service service;
private List<Entity>list;
public Bean() {}
@PostConstruct
private void init() {
initialize();
}
private void initialize() {
// Initialize the list on application start up.
// The service.getList() method in an EJB is authenticated anonymously
// for the first time on application start up.
list=service.getList();
// Do something programmatically to enforce the authority ROLE_ADMIN afterwords.
}
// This method is only invoked by an admin (ROLE_ADMIN) as and when required.
// The @PostConstruct method may however be invoked by an anonymous user on start up.
public void action() {
initialize();
}
}
是否可以在用@PostConstruct 装饰的方法完成之前以编程方式强制执行权限/角色,以便service.getList() EJB 方法仅由具有所述ROLE_ADMIN 权限的用户调用一次该方法用@PostConstruct装饰完成它的工作了吗?
换句话说,它的行为完全如下所示 - 一旦@PostConstruct 完成它的工作?
@Stateless
@DeclareRoles(value = {"ROLE_ADMIN", "ROLE_USER"})
@RolesAllowed(value = {"ROLE_ADMIN"})
public class Skeleton implements Service {
@Override
public List<Entity> getList() {
return entityManager.createQuery("SELECT e FROM Entity e").getResultList();
}
}
我目前使用的是 GlassFish Server 4.1,但如果答案与容器无关,那就更好了。
【问题讨论】:
-
我认为这在
@ApplicationScopedbean 的上下文中没有多大意义。 -
@peeskillet :整个页面仅描述了
@DeclareRoles、@RolesAllowed和@RunAs的用法。它没有说明在运行时动态分配权限/角色,即“programmatically”。 -
@SteveC:我完全没能找到你。
-
这个 bean 是
@ApplicationScoped。所有用户都将访问同一个实例,并且它的@PostConstruct方法只会被调用一次(每个应用程序节点)。您要求根据第一个访问它的用户所拥有的角色对其进行初始化。
标签: jakarta-ee glassfish jaas java-ee-7 glassfish-4.1