我在@Query中使用Spring Security Expressions就像这个例子:
@Query("select o from Pet o where o.owner.name like ?#{hasRole('ROLE_ADMIN') ? '%' : principal.username}")
Run Code Online (Sandbox Code Playgroud)
如果您具有ADMIN角色,则查询将返回所有宠物.但是,如果您没有此角色,则查询仅返回所有者名称与用户身份验证名称相同的Pet对象.
这很好用,但是当我尝试使用hasAnyRole('ROLE_ADMIN','ROLE_OWNER')时,系统会返回异常......
org.springframework.expression.spel.SpelEvaluationException: EL1004E:(pos 0): Method call: Method hasAnyRole(java.lang.String,java.lang.String) cannot be found on java.lang.Object[] type
at org.springframework.expression.spel.ast.MethodReference.findAccessorForMetho
...
Run Code Online (Sandbox Code Playgroud)
在SecurityExpressionRoot中定义方法hasAnyRole:
public final boolean hasAnyRole(String... roles) {
return hasAnyAuthorityName(defaultRolePrefix, roles);
}
Run Code Online (Sandbox Code Playgroud) 我尝试审核一个实体,但是我不想审核其关系。如果我将@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)放在@ManyToOne关系中,则此方法有效,我也没有任何例外,但是当我尝试在@onetomany中使用定义了mapbyby参数的相同注释时,我有一个我必须审核另一个实体的例外情况。
例:
@Table(name = "OWNERS")
@Entity
@EntityListeners(AuditingEntityListener.class)
@Audited
public class Owner {
...
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
@ManyToOne(fetch=FetchType.LAZY)
private User user;
...
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
@OneToMany(cascade = CascadeType.ALL, mappedBy = "owner" )
private Set<Pet> pets = new HashSet<Pet>();
...
}
Run Code Online (Sandbox Code Playgroud)