是否可以使用注释实现方法级访问检查?
考虑一些带有User
和Group
的基本授权框架,其中对方法的访问应该通过检查来保护,以确保用户或组具有必要的PrivilegeLevel
执行该方法,否则失败。
我的想象是这样的:
@AccessCheck(PriviledgeLevel.ADMINISTRATOR)
public static void secureMethod(){ ... }
代码检查基本上是这样的吗?
if(currentUser.getPriviledgeLevel >= PriviledgeLevel.ADMINISTRATOR ||
currentUser.getGroup.priviledgeLevel >= PriviledgeLevel.ADMINISTRATOR)
// Allow access
else
// Deny access
可以这样实现吗?
我做了一些研究,指出了一些基于 AspectJ 的现有内容,主要是在 安全注释框架 (SAF) 和 春天安全。
我有点担心,因为 SAF 似乎不再很活跃,而且文档也不是那么好。
我不确定 Spring Security,但它似乎更关注与 Web 相关主题的安全问题。
Java 身份验证和授权服务 似乎相关,但不使用注释方法。
尝试使用这种声明性方法定义这些安全要求是否有意义?
是否有另一个我缺少的库/框架,它已经实现了我想要的或一些与这里相关的技术?
或者是否有一个完全不同的解决方案(例如实现我自己的ClassLoader
,...),它优于我的想象(就库用户的简洁性和可读性而言)?
Consider some basic authorization framework with User
s and Group
s where access to methods should be guarded by checks which make sure that the user or the group have the necessary PriviledgeLevel
to execute the method and fails otherwise.
What I imagine is something like this:
@AccessCheck(PriviledgeLevel.ADMINISTRATOR)
public static void secureMethod(){ ... }
Where the code checking basically does
if(currentUser.getPriviledgeLevel >= PriviledgeLevel.ADMINISTRATOR ||
currentUser.getGroup.priviledgeLevel >= PriviledgeLevel.ADMINISTRATOR)
// Allow access
else
// Deny access
Would it be possible to implement it this way?
I did a bit of research which points to some existing things based on AspectJ, mostly on the Security Annotation Framework (SAF) and on Spring Security.
I'm a bit concerned because SAF doesn't seem very active anymore and the documentation isn't that great.
I'm not sure about Spring Security but it seems to be more focused on security problems in web-related topics.
The Java Authentication and Authorization Service seems to be related, but doesn't use the annotation approach.
Does it make sense trying to define these security requirements with this declarative approach?
Is there another library/framework I'm missing, which already implements what I want or some techonology which would be relevant here?
Or is there a completely different solution (like implementing my own ClassLoader
, ...) which is superior to what I imagine (in terms of conciseness and readability for the library user)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为 AspectJ 会做你想做的事。我们有一大堆方法,您需要某些访问权限,并且我们创建了一个 AspectJ 方面,它将检查并在用户没有这些权限时出错。
另外,由于 AspectJ 在编译时“编织”到类中,因此无法通过配置禁用它。
我们也使用Spring Security,两者可以和谐使用!
I think AspectJ will do what you want it to do. We have a whole bunch of methods which you need certain access rights for and we've created an AspectJ aspect which will check that and error out if the user does not have those permissions.
As a plus, because AspectJ is "woven" into the classes at compile time it cannot be disabled by configuration.
We also use Spring Security, it is possible to use both in harmony!
您可以通过使用动态代理自己轻松地完成此操作。
代理将在实现您的接口的类上创建,并且您将使用自定义注释来注释您的接口。
在代理的 invoke() 方法中,您可以检查您的方法是否具有注释,如果不满足权限则抛出 SecurityException。
You could do this fairly trivially yourself by using dynamic proxies.
The proxy would be created on the class that implements your interface and you would annotate your interface with your custom annotation.
In the invoke() method of your proxy, you can check if your method has the annotation and throw a SecurityException if the privileges are not met.
使用 Spring Security,您只需添加:
并正确配置它,方法是:
UserDetailsService
< /一>如果您没有使用数据库凭据存储,只需配置您的首选
UserDetailsService
< /a> 将用户和组凭据添加到生成的用户详细信息
。当然,如果不检查 文档,但是 Spring Security 完全可以实现方法级别的访问检查,这是我最喜欢的技术。
With Spring Security, you just have to add:
And configure it properly, by:
JdbcDaoImpl
as yourUserDetailsService
If you are not using database credential storage, just configure your preferred
UserDetailsService
to add both user and group credentials to the authorities of the generatedUserDetails
.Of couse, it is hard to understand it without checking the concepts at the documentation, but method level access checks is perfectly possible with spring security and it's my prefered technology for it.
在 Spring Security 中,如 文档指出,
@Secured
和@PreAuthorize
都可以在方法级别使用。要启用@PreAuthorize(如果您还没有...),您需要输入< /代码>
在您的配置 XML 中;
对于
@Secured
,请使用
。有关更多详细信息,请参阅
In Spring Security, as the docs state, both
@Secured
and@PreAuthorize
can be used at method level.To enable
@PreAuthorize
(if you haven't already...), you need to put<global-method-security pre-post-annotations="enabled" />
in your configuration XML;
For
@Secured
use<global-method-security secured-annotations="enabled" />
.For more details, refer to this article.