具有路径变量参数的Spring Security

时间:2012-08-01 10:10:18

标签: java spring java-ee spring-mvc spring-security

我想基于自定义url参数(路径变量)创建安全规则。 在例子中。假设我想拥有对名为Brand1和Brand2的资源具有管理员访问权限的用户,但无法访问名为Brand3的资源。我们可以使用以下链接编辑资源。

http://myapp/brand/edit/1
http://myapp/brand/edit/2
http://myapp/brand/edit/3

现在在安全环境中我想做类似的事情

 <security:intercept-url pattern="/brand/edit/{brandId}" 
           access="hasRole('ROLE_ADMIN') or
                   @authorizationService.hasBrandPermission(
                   #brandId, principal.username)"/>

我唯一得到的是用户名。 BrandId始终为null。 我曾经用@PreAuthorize做到这一点并且它有效但现在我想将安全配置集中在单个xml文件中,而不是将其传播到所有控制器类。此外,当我使用@PreAuthorize时,我的访问被拒绝处理程序没有将我重定向到被拒绝的页面,但显示丑陋的AccessDeniedException insead。

我真的会想到任何想法。

2 个答案:

答案 0 :(得分:2)

pom.xml中的Spring Security版本更改为4.1.0.RELEASE

<spring-security.version>4.1.0.RELEASE</spring-security.version>

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-web</artifactId>
    <version>${spring-security.version}</version>
</dependency>

之后你可能需要清理你的maven项目。

(我知道,这是一个老问题。尽管如此,我在3年后面临同样的问题。)

答案 1 :(得分:1)

您可以尝试使用正则表达式。

如果使用spring security 3.1,则需要在http元素中添加 path-type =“regex”属性或 request-matcher =“regex” < / p>

有关详细信息,请参阅文档