是否可以使用对象的属性值来决定访问权限?
我是Spring Security的新手。我一直致力于创建一个自定义选举器,根据对象的属性值决定是否授予权限。也就是说,如果对象实例A具有值为i的属性X,则具有ROLE_MGR的用户具有访问权限。如果对象实例B在X属性中具有值j,则ROLE_MGR不具有访问权限。是否有可能这样做,如果是这样,我需要做什么?如果这是不可能的,我们可能决定不使用Spring Security。是否可以使用对象的属性值来决定访问权限?
我想通了。我需要使用自定义权限评估程序。从我的代码摘录以下提供的人可能会试图做同样的事情:
的security.xml
<security:global-method-security
pre-post-annotations="enabled">
<security:expression-handler ref="expressionHandler" />
</security:global-method-security>
<bean id="expressionHandler"
class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler">
<property name="permissionEvaluator">
<bean id="permissionEvaluator"
class="org.krams.tutorial.infrastructure.SomePermissionsEvaluator" />
</property>
</bean>
服务接口 @PostFilter( “调用hasPermission(filterObject, '读')”) public List getAll();
自定义权限计算器
@Override
public boolean hasPermission(Authentication authorities,
Object targetDomainObject, Object permission) {
boolean Decision = false;
System.out.println("Initial Decision: " + Decision);
Date cutoffDate = null;
try {
cutoffDate = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH)
.parse("January 1, 2012");
System.out.println("Cutoff Date: " + cutoffDate.toString());
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("Domain Object Date: "
+ Post.class.cast(targetDomainObject).getDate());
if (Post.class.cast(targetDomainObject).getDate().before(cutoffDate)) {
Decision = false;
System.out.println("In before");
} else {
Decision = true;
System.out.println("In after");
}
System.out.println("Final Decision: " + Decision);
System.out.println("--------");
return Decision;
}
这可能,但首先看看Spring Security的域对象安全。这是用来授予细粒度访问你的对象,看到这里:http://static.springsource.org/spring-security/site/docs/3.0.x/reference/domain-acls.html
我已经阅读了acl的文档,并没有指定我如何根据域对象的属性的值作出决定。 – chanakya2 2012-03-21 16:42:15
我同意,OP希望基于域对象属性的值而不是域对象本身进行保护。 – 2016-05-27 04:31:48
的确是这样,但你到底是在实施它有什么样的问题?这看起来非常简单。 – Simeon 2012-03-21 12:28:40
我正在使用自定义选举器(实现AccessDecisionVoter),但是没有办法获取域对象。我如何检查自定义选举器中域对象的属性值,还是需要查看另一个接口/类来自定义? – chanakya2 2012-03-21 16:45:40
我认为你需要更多地解释“对象”。它在哪里以及如何确定要访问哪个实例?这似乎基本上就是答案所在。如果它无法从投票人那里获得(例如通过注入一个DAO),那么你需要解释为什么。 – 2012-03-21 17:32:41