具有路径变量和蚂蚁匹配器的Spring Security自定义方法


问题内容

我正在使用带有Java配置的Spring Boot,Web MVC和Spring Security。我的网址是“
RESTful”,并且想添加自定义授权方法。

例如:

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

我想实现以下目标:

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

我了解我可以传入HttpServletRequest和手动删除路径,但想避免这种情况!同样也不太热衷于方法级别的安全性,因为我有许多控制器,所以宁愿将配置保留在一个位置。

谢谢!


问题答案:

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

.antMatchers("/path/{token}/**").access("@myBean.authorise(#token)")
我不确定何时引入此功能,但我知道现在支持它。参考:[https]( https://docs.spring.io/spring-
security/site/docs/current/reference/htmlsingle/#el-access-web-path-variables)
//docs.spring.io/spring-
security/site/docs/current/reference/htmlsingle/#el-access-web-path-
variables