使用 spring-authorization-server,如何从身份验证服务器框架之外的正在进行的身份验证中检索 context/request?

Using spring-authorization-server, how do you retrieve the context/request from an in-progress authentication outside of the auth server framework?

我试图解决的用例是当用户必须通过表单登录时,能够显示有关与 OAuth 请求关联的已注册客户端的一些详细信息。例如,如果能够为客户显示一个友好的名称,那就太好了。一旦拥有客户端 ID,我就可以处理映射此类数据,但我不确定如何在 Spring 启动应用程序的其他地方检索有关原始请求的客户端 ID(或其他识别信息)。

我在代码库中查找了任何类似于持有者的东西(例如 Spring 安全性 SecurityContextHolder)或任何在会话中存储数据的东西,但我找不到任何参考。此数据必须以某种方式在原始请求和登录过程之间保留。我如何找回它?

谢谢!

看起来这是通过会话中的 SavedRequest 在框架外处理的。如果您遇到类似的问题,这就是我从控制器获取登录页面上的 OAuth2 客户端的方式。

    /**
     * Retrieves a friendly OAuth2 client name for the given login request.
     * <p>
     * In order for this to work, a couple of conditions must be met:
     *
     * <ol>
     *     <li>The original request must have been a valid OAuth2 request.</li>
     *     <li>The client must be configured with a friendly name.</li>
     * </ol>
     * <p>
     * If a client name can not be found, {@code null} is returned.
     *
     * @param session HTTP session of the request.
     * @return The friendly client name, or {@code null}.
     */
    private String getClientName(HttpSession session) {
        if (session != null) {
            DefaultSavedRequest savedRequest = (DefaultSavedRequest) session.getAttribute("SPRING_SECURITY_SAVED_REQUEST");

            if (savedRequest != null && savedRequest.getParameterMap().containsKey("client_id")) {
                String[] values = savedRequest.getParameterMap().get("client_id");

                if (values.length > 0) {
                    String clientId = values[0];

                    RegisteredClient registeredClient = registeredClientRepository.findByClientId(clientId);

                    if (registeredClient != null && StringUtils.hasLength(registeredClient.getClientName())) {
                        return registeredClient.getClientName();
                    }
                }
            }
        }

        return null;
    }