使用JBoss jBPM实现流程访问和执行的授权
2010-05-11 00:00:00 来源:WEB开发网图 4 有用户授权信息的流程的ACL
这些ACL的生成是通过引入额外的部署器完成的,它将在“标准”jBPM部署器之后运行,抽取上面描述的授权属性,为给定流程构建ACL。
保护jBPM命令
我们采用了一种通用的方法来保护jBPM命令,包括实现用于定义命令所需授权信息的自定义的注解,以及处理这个注解的自定义的授权会话(命令拦截器)实现。
授权注解(清单3)可以指定所需的用户角色和表示某个流程的方法。
@Retention(value=RetentionPolicy.RUNTIME)
@Target(value=ElementType.METHOD)
public @interface AuthorizedCommand {
/** Access type */
public String role();
String key();
}
清单 3 授权注解
对于某个流程,用户角色——“starter”或“user”——指向某个用户应该拥有的角色。由于不同命令既可以引用部署ID,也可以引用流程ID或者流程键值,因此注解支持多种指定键值的方式,允许将合适的引用指定为键值。
清单4的授权拦截器检查是否有命令的方法被授权注解修饰。如果有,则执行适当的查询,确定出哪些用户和用户组集合被授权给了这个命令,然后检查当前用户是 否属于他们。
…………..
@SuppressWarnings("unchecked")
public void checkPermission(Command<?> command, EnvironmentImpl environment) {
environment.setAuthenticatedUserId(environment.get(AuthorizationIdentitySession.class).getAuthenticatedUserId());
for( Method method : command.getClass().getMethods()) {
AuthorizedCommand sc = method.getAnnotation(AuthorizedCommand.class);
if(sc != null){
log.debug("Checking Class based Secured Function");
String ID = environment.get(AuthorizationIdentitySession.class).getAuthenticatedUserId();
Object value = null;
try {
log.debug("Checking authorization: " + command.getClass().getName());
Session session = environment.get(SessionImpl.class);
value = method.invoke(command, (Object[])null);
Query uQ = session.createQuery(userQuery.get(sc.key())).
setString("role", sc.role()).setString("value",(String) value);
Query gQ = session.createQuery(groupQuery.get(sc.key())).
setString("role", sc.role()).setString("value", (String) value);
List<String> userIds = (List<String>)uQ.list();
List<String> groups = (List<String>)gQ.list();
if(!isAuthorized(environment, userIds, groups))
throw new AccessControlException(ID+" attempted access to ProcessDefinition #"+value);
} catch (IllegalArgumentException e) {
log.error("Caught " + IllegalArgumentException.class, e);
throw new AccessControlException(ID+" attempted access to ProcessDefinition #"+value);
} catch (IllegalAccessException e) {
log.error("Caught " + IllegalAccessException.class, e);
throw new AccessControlException(ID+" attempted access to ProcessDefinition #"+value);
} catch (InvocationTargetException e) {
log.error("Caught " + InvocationTargetException.class, e);
throw new AccessControlException(ID+" attempted access to ProcessDefinition #"+value);
}
}
}
return;
}
……………………..
public boolean isAuthorized(EnvironmentImpl env, List<String> authorizedUserIds, List<String> authorizedGroupIds) {
AuthorizationIdentitySession identitySession = env.get(AuthorizationIdentitySession.class);
if (authorizedUserIds.contains(AuthorizationIdentitySession.ANONYMOUS_USER_ID))
return true;
if (authorizedUserIds.contains(identitySession.getAuthenticatedUserId()) )
return true;
//check if any of userGroups is an authorized group. if so then return true
List<Group> groups = identitySession.findGroupsByUser(identitySession.getAuthenticatedUserId());
for(Group group : groups){
String g = group.getId();
// admin is allowed to execute any command
if(g.equals(AuthorizationIdentitySession.ADMINISTRATORS_GROUP))
return true;
if(authorizedGroupIds.contains(g))
return true;
}
return false;
}
更多精彩
赞助商链接