我目前正在开发一个用例,我需要使用以下 Spring Security 标记库来根据用户权限显示(或不显示)链接。
<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 Security 引用的以下部分时:
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.
…问题是该引用文献不涵盖 Java Config。所以我想我只需要定义一个 @Bean
在我的 SecurityConfig 类中,如下所示:
@Bean
public WebInvocationPrivilegeEvaluator webInvocationPrivilegeEvaluator() {
return new DefaultWebInvocationPrivilegeEvaluator();
}
…但是,它需要一个 FilterSecurityInterceptor 的实例,我已经在日志中看到了它。事实上,它已经是我的过滤器链的一部分,所以我想知道如何将它的引用放入 DefaultWebInvocationPrivilegeEvaluator
中。上面的构造函数?
Spring Security 3.2.4.RELEASE 引用: http://docs.spring.io/spring-security/site/docs/3.2.4.RELEASE/reference/htmlsingle/#the-authorize-tag
谢谢
最佳答案
获取 FilterSecurityInterceptor
的实例您只需向 @Bean
添加一个参数即可- 带注释的方法,spring 将发挥作用(它将找到匹配的 bean 并将其作为参数传递给方法):
@Bean
public WebInvocationPrivilegeEvaluator webInvocationPrivilegeEvaluator(FilterSecurityInterceptor filterSecurityInterceptor) {
return new DefaultWebInvocationPrivilegeEvaluator(filterSecurityInterceptor);
}
但是这并不能解决您的问题。当使用java配置时,spring自动创建WebInvocationPrivilegeEvaluator
bean,因此不需要手动创建它。事实上,如果没有WebInvocationPrivilegeEvaluator
上下文中的bean,sec:authorize
标签会抛出 exception .
我相信您的问题是由 url
的错误使用引起的属性。它的值应该是一个内部 url,例如 /security/user/create
,但您正在使用 <spring:url>
的结果标签,它将上下文路径附加到该 url(如 /context/security/user/create
)。
另一种可能性是您没有配置 spring-security 来实际保护 /security/user/create
url,但我认为这里的情况并非如此。
关于java - Spring 安全秒 :authorize tag using Java Config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37466594/