【问题标题】:Spring Security with path variable parameters带有路径变量参数的 Spring Security
【发布时间】:2012-07-30 05:14:31
【问题描述】:

我想根据自定义 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 始终为空。 我曾经使用 @PreAuthorize 来做到这一点并且它有效,但现在我想将安全配置集中在单个 xml 文件中,而不是将其分布在所有控制器类中。此外,当我使用 @PreAuthorize 时,我的 access-denied-handler 并没有将我重定向到拒绝页面,而是显示丑陋的 AccessDeniedException insead。

我真的很欣赏任何想法。

【问题讨论】:

  • 我不明白这个问题。 BrandId 是否始终为空?还是它不保护资源?还是丑陋的 AccessDeniedException 页面?

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


【解决方案1】:

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 年后我也遇到了同样的问题)。

【讨论】:

  • 这与问题有什么关系?
【解决方案2】:

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

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

查看文档了解更多详情

【讨论】:

  • 我虽然该正则表达式仅用于定义 url 模式而不是用于引用路径变量。即,我可能会执行类似“/path/to/*.html”的操作,因此它会应用与模式匹配的所有 URL,但我感兴趣的是“/path/to/resource/{resourceId}”并在 ' 中获取 resourceId 值访问'部分,即 access="@myService.mySecurityMethod(#resourceId)"
猜你喜欢
  • 1970-01-01
  • 2015-01-25
  • 1970-01-01
  • 2013-06-09
  • 2016-01-11
  • 2014-12-12
  • 1970-01-01
  • 2017-01-17
  • 1970-01-01
相关资源
最近更新 更多