【发布时间】:2020-12-19 17:16:22
【问题描述】:
所以我用两个简单的公共字符串创建了一个类
public final class Right {
private Right() {
super();
}
public static final String AUTH = "hasAuthority('admin') or hasAuthority('mod')";
}
当我将它与控制器上的 @PreAuthorize 注释一起使用时,它就像一个魅力。我不喜欢它是硬编码的。出于这个原因,我将角色放在属性中,并尝试将其用作组件:
@Component("authRule")
public class AuthRule {
@Value("${role.administrator}")
private String roleAdmin;
@Value("${role.moderator}")
private String roleMod;
public String getRightAccess() {
return "hasAuthority('" + roleAdmin+ "')" + " or hasAuthority('" + roleMod+ "')";
}
}
当我在我的 PreAuthorize 中使用它时:
@PreAuthorize("@authRule.getRightAccess()")
我正在返回一个异常,即 Failed to convert from type [java.lang.String] to type [java.lang.Boolean] for value 'hasAuthority('admin') or hasAuthority('mod')
如果我在 PreAuthorize 中硬编码。我对此感到很困惑。有人有什么想法吗?
提前感谢所有回复。
【问题讨论】:
标签: java spring-boot security components authorization