JAAS中的程序化授权

时间:2015-03-07 16:21:55

标签: java-ee glassfish jaas java-ee-7 glassfish-4.1

我有一个要求以编程方式添加授权(授权)约束(因此不进行身份验证)。我有一个应用程序范围的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方法仅由具有所述{{1}的用户调用用ROLE_ADMIN装饰的方法完成其工作后,权限?

换句话说,它的行为完全如下图所示 - 一旦@PostConstruct完成其工作?

@PostConstruct

我目前正在使用GlassFish Server 4.1,但如果答案与容器无关,那会更好。

1 个答案:

答案 0 :(得分:0)

我不确定是否会奏效,但您可以尝试以下方法。创建一个接收SessionContext的EJB Bean,然后使用方法isCallerInRole(role)。

@Stateless
public class AuthorizationBean {
    @Resource
    private SessionContext sessionContext;

    public Boolean isCallerInRole(String role){
     return this.sessionContext.isCallerInRole(role);
    }
}

在您的类中注入此bean,然后检查角色。这里的问题是它的类(ApplicationScoped)的范围,因为这不确定它是否会起作用。

相关问题