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

Dai*_*Lea 9 java spring spring-mvc spring-security

我使用java配置的spring boot,web MVC和spring security.我的网址是"RESTful",并希望添加自定义授权方法.

例如:

.antMatchers("/path/*/**").access("@myBean.authorise()")
Run Code Online (Sandbox Code Playgroud)

我希望实现这样的目标:

.antMatchers("/path/{token}/**").access("@myBean.authorise(token)")
Run Code Online (Sandbox Code Playgroud)

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

谢谢!

The*_*ger 7

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

.antMatchers("/path/{token}/**").access("@myBean.authorise(#token)")
Run Code Online (Sandbox Code Playgroud)

我不确定这是什么时候引入的,但我知道它现在得到支持。参考:https : //docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#el-access-web-path-variables