6.2. 迁移授权应用程序
授权是 Eclipse Vert.x 4 中的新功能。在之前的版本中,您只能检查用户是否有权在 User 对象上执行任务。这意味着该提供程序负责用户的身份验证和授权。
在 Eclipse Vert.x 4 中,User 对象实例不与特定的身份验证供应商相关联。因此,您可以使用不同的供应商验证并授权用户。例如,您可以使用 OAuth2 验证用户,并对 MongoDB 或 SQL 数据库执行授权检查。
以下示例演示了如何应用程序检查用户是否可以在 Eclipse Vert.x 3.x 版本中使用打印机 #1234。
// omitting the error handling for brevity
user.isAuthorized("printers:printer1234", res -> {
if (res.succeeded()) {
boolean hasAuthority = res.result();
if (hasAuthority) {
System.out.println("User can use the printer");
} else {
System.out.println("User cannot use the printer");
}
}
});
此授权适用于 JDBC 和 MongoDB。但是,它不适用于 OAuth2 等提供程序,因为该提供程序没有执行授权检查。从 Eclipse Vert.x 4 中,可以使用不同的提供程序执行此类授权检查。
// omitting the error handling for brevity
provider.getAuthorizations(user, res -> {
if (res.succeeded()) {
if (PermissionBasedAuthorization.create("printer1234").match(user)) {
System.out.println("User can use the printer");
} else {
System.out.println("User cannot use the printer");
}
}
});
您可以检查对添加的角色、权限、逻辑操作、通配符和其他任何实施的授权。