我目前正在开发一个用例,在该用例中,我需要使用下面的来显示(或不显示)基于用户权限的链接。
<spring:url var="createUserURL" value="/security/user/create" />
<sec:authorize url="${createUserURL}">
<a href="${createUserURL}">
<spring:message code="button.createUser" />
</a>
</sec:authorize>在查看日志时,我发现没有发生任何事情
<sec:authorize url="${createUserURL}">不管我的角色是什么,我总是看到按钮。当我使用hasRole时,它可以工作,所以我肯定遗漏了一些<sec:authorize url="..." />工作的东西。
在阅读Spring安全参考的以下部分时:
要使用这个标记,应用程序上下文中还必须有一个
WebInvocationPrivilegeEvaluator实例。如果您正在使用命名空间,则将自动注册一个名称空间。
…问题是,引用没有涵盖Java。因此,我想我只需要在我的@Bean类中定义一个SecurityConfig,如下所示:
@Bean
public WebInvocationPrivilegeEvaluator webInvocationPrivilegeEvaluator() {
return new DefaultWebInvocationPrivilegeEvaluator();
}…但是,它需要一个FilterSecurityInterceptor实例,我已经在日志中看到了这个实例。事实上,它已经是我的过滤器链的一部分,所以我想知道如何在上面的DefaultWebInvocationPrivilegeEvaluator构造函数中引用它?
3.2.4. Reference:http://docs.spring.io/spring-security/site/docs/3.2.4.RELEASE/reference/htmlsingle/#the-authorize-tag
谢谢
发布于 2016-06-08 00:26:44
要获得FilterSecurityInterceptor的实例,您只需向@Bean-annotated方法添加一个参数,spring就会发挥神奇的作用(它将找到匹配的bean并将其作为参数传递给该方法):
@Bean
public WebInvocationPrivilegeEvaluator webInvocationPrivilegeEvaluator(FilterSecurityInterceptor filterSecurityInterceptor) {
return new DefaultWebInvocationPrivilegeEvaluator(filterSecurityInterceptor);
},但是,这不能解决你的问题。当使用java配置时,spring自动创建WebInvocationPrivilegeEvaluator bean,因此不需要手动创建它。实际上,如果上下文中没有WebInvocationPrivilegeEvaluator bean,那么sec:authorize标记就会抛出一个异常。
我相信您的问题是由于不正确地使用url属性造成的。它的值应该是一个内部url,如/security/user/create,但是您使用的是<spring:url>标记的结果,它将上下文路径附加到该url (如/context/security/user/create)。
另一种可能是您没有配置spring安全性来实际保护/security/user/create url,但我认为这里不是这样的。
https://stackoverflow.com/questions/37466594
复制相似问题