我已经开始使用 Spring Security,但经过大量研究后,我无法找到答案:
如果我明确想要检查用户 A 是否可以访问 B 的内容。我可以使用 JSP 标签支持来检查 Spring Security - check if web url is secure / protected喜欢
<sec:authorize url="stuff/B">
但是如果我想在 Controller (java 类)中检查同样的事情怎么办。我在这里没有找到任何 Spring 功能来检查登录用户是否可以访问提到的 url(https://docs.spring.io/spring-security/site/docs/3.0.x/reference/el-access.html)
最佳答案
来自 javadoc 的提示:
to use this tag there must also be an instance of
WebInvocationPrivilegeEvaluator
in your application context. If you are using the namespace, one will automatically be registered. This is an instance ofDefaultWebInvocationPrivilegeEvaluator
,"
在 DefaultWebInvocationPrivilegeEvaluator
的 javadoc 中,我们可以看到一个isAllowed
应该完成这项工作的方法:
// privilegeEvaluator is a WebInvocationPrivilegeEvaluator "autowired"
boolean allowed = privilegeEvaluator.isAllowed("/stuff/B", yourAuthentication);
关于java - Spring安全检查用户是否可以访问提到的url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45276701/