2
我使用的是自定义访问检查与@PreAuthorize:是否可以在自定义PreAuthorize方法中获取RequestMethod-verb?
@RestController
@RequestMapping("/users")
public class Users {
@PreAuthorize("@customAccessChecker.hasAccessToMethod('USERS', 'GET')")
@RequestMapping(method = RequestMethod.GET)
User getUsers() {
...
}
@PreAuthorize("@customAccessChecker.hasAccessToMethod('USERS', 'POST')")
@RequestMapping(method = RequestMethod.POST)
User addUser() {
...
}
}
我想摆脱字符串“GET”和“POST”在@PreAuthorize注释。是否有可能在@RequestMapping中使用RequestMethod作为hasAccessToMethod的变量输入?
这是一个理智的答案。由于注释值需要一个常量Spring,因此不能将它与某些枚举值连接起来。 – Andrew
太棒了!它工作完美。 – maxo
从请求我也可以使用#request.servletPath获取资源名称,而不是使用硬编码的'USERS'字符串。 – maxo