具有路径变量参数的Spring Security

Mar*_*ski 5 java spring spring-mvc spring-security java-ee

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

http://myapp/brand/edit/1
http://myapp/brand/edit/2
http://myapp/brand/edit/3
Run Code Online (Sandbox Code Playgroud)

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

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

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

我真的会想到任何想法.

rpt*_*t57 1

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

如果您使用 spring security 3.1,则需要在 http 元素中添加属性path-type="regex"或request-matcher="regex"

请参阅文档了解更多详细信息