0

我正在尝试在 jsp 加载时点击 cognito 授权 URL。当 JSP 被调用时,我在我的 jsp 中调用了一个 java 方法。以下是代码片段:

public static void getData(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setStatus(HttpServletResponse.SC_FOUND);
    response.setHeader("Location",
            "https://test.us-west-2.amazoncognito.com/oauth2/authorize?redirect_uri=https://test.com&client_id=123456789&response_type=code");
}

用户在点击 jsp 时成功被重定向。授权码code附加到 url(我可以在 tomcat 日志中看到)。

我的方法中需要授权码。我已经尝试从 request 获取它request.getParameter("code");,但这没有用。

如何获取代码?它作为查询参数附加到 url。

用户 [14/Jul/2021:15:35:43 -0400] “GET /app?code=fdsfs-fsd-4324-4324-fdsfsdfjh5u34 HTTP/1.1”302 - 6553

4

1 回答 1

0

通常,您将使用将响应 URL 解析为对象的库。这取决于您未指定的技术。

作为示例,请参见在浏览器中运行并将代码放入的代码片段urlData.query.code- 它使用url-parse 库

于 2021-07-14T19:23:53.780 回答