如何访问SpringBoot的AccessDecisionVoter中的request body?

How to access the request body in SpringBoot's AccessDecisionVoter?

所以我们有一个授权服务器,我们用它来创建 OAuth2 访问令牌。所有子系统都会验证访问令牌并可能会检查权限的请求路径,但是,在其中一个子系统中,我们需要查看请求正文并提取 'id' 以检查用户是否具有适当的权限提交请求。请求消息采用 JSON 格式,这是一个带有客户端提供的 ID 的 POST 请求。

请求中的id为进程id,部分用户可能对某些进程没有权限,需要id进行验证。

因此,在 AccessDecisionVoter 中,我们只能获取请求 URI,但无法获取 HttpServletRequest 来读取消息。 (注意:我们有一个请求包装器,允许我们多次读取请求体)

我尝试自动连接 HttpServletRequest,但没有成功。出现没有线程绑定请求的错误

我也在考虑实现 UserDetailService,但还是没有成功,因为 Spring 引导程序没有调用它。请记住,我们使用的是自定义 AuthorizationServerTokenServices,它位于公共库中。

如何在 AccessDecisionVoter 中获取 Http servlet 请求或请求主体?

您应该能够实现一个 AccessDecisionVoter<FilterInvocation>,您可以在其中获取请求。这行不行:

public class MyAccessDecisionVoter implements AccessDecisionVoter<FilterInvocation> {
    @Override
    public boolean supports(ConfigAttribute attribute) {
        return false;
    }

    @Override
    public boolean supports(Class<?> clazz) {
        return true;
    }

    @Override
    public int vote(Authentication authentication, FilterInvocation fi, Collection<ConfigAttribute> attributes) {
        int result = ACCESS_ABSTAIN;
        fi.getRequest() // this is the request
        // decide the outcome and set result

        return result;
    }
}