【问题标题】:Spring Security custom method with path variable and ant matcher带有路径变量和蚂蚁匹配器的 Spring Security 自定义方法
【发布时间】:2014-12-12 19:42:09
【问题描述】:

我正在使用带有 java 配置的 Spring Boot、Web MVC 和 Spring Security。我的 URL 是 'RESTful' 并且想要添加自定义授权方法。

例如:

.antMatchers("/path/*/**").access("@myBean.authorise()")

我想实现这样的目标:

.antMatchers("/path/{token}/**").access("@myBean.authorise(token)")

我知道我可以传入HttpServletRequest 并手动剥离路径,但想避免这种情况!也不太热衷于方法级别的安全性,宁愿将配置保存在一个地方,因为我有很多控制器。

谢谢!

【问题讨论】:

    标签: java spring spring-mvc spring-security


    【解决方案1】:

    您可以访问路径变量,只需在它们前面加上#。在您的情况下,正确的语法是:

    .antMatchers("/path/{token}/**").access("@myBean.authorise(#token)")
    

    我不确定这是什么时候引入的,但我知道它现在已受支持。参考:https://docs.spring.io/spring-security/site/docs/4.2.x/reference/html/el-access.html

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 2012-07-30
    • 2011-02-26
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多