在spring security中使用hasPermission时,权限参数区分大小写吗?

时间:2015-06-26 19:43:09

标签: java spring security spring-security permissions

以下两行的行为是否相同?

@PreAuthorize("hasPermission(#task, 'MANAGER')

@PreAuthorize("hasPermission(#task, 'manager')

此外,权限常量在CustomPermission.java

中定义如下
 public static final Permission MANAGER    = new CustomPermission(1<<7,'M');

1 个答案:

答案 0 :(得分:1)

权限参数是否区分大小写?嗯,是的,不。如果权限是大写String并且您输入了小写权限,则它将起作用。然而,相反的结果却不胜一筹。

我检查了Spring的源代码。 AclPermissionEvaluatorPermissionEvaluator的默认实现,它是处理hasPermission()例程的接口。它首先尝试从原始给定的String中查找权限。如果找不到,则会再次尝试拨打toUpperCase()

亲自看看:

if (permission instanceof String) {
    String permString = (String) permission;
    Permission p;

    try {
        p = permissionFactory.buildFromName(permString);
    }
    catch (IllegalArgumentException notfound) {
        p = permissionFactory.buildFromName(permString.toUpperCase());
    }

    if (p != null) {
        return Arrays.asList(p);
    }

}

参考:

https://github.com/spring-projects/spring-security/blob/7b4a37f27e4ba7045bd63656e49ee0d5ee381ce5/acl/src/main/java/org/springframework/security/acls/AclPermissionEvaluator.java