【问题标题】:How to get the authorization code from query params // Amazon Cognito authorization endpoint如何从查询参数中获取授权代码 // Amazon Cognito 授权端点
【发布时间】:2021-09-23 06:19:39
【问题描述】:

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

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.getParameter("code"); 中获取它,但没有成功。

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

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

【问题讨论】:

    标签: jsp servlets oauth-2.0 oauth amazon-cognito


    【解决方案1】:

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

    作为示例,请参阅this code snippet,它在浏览器中运行并将代码放入urlData.query.code - 它使用url-parse library

    【讨论】:

    • 我正在用 Java 编写代码。当我打印request.getParameterMap() 时,我的日志中根本看不到代码。
    • 我认为你可以做 request.getParameter("code")
    • 我尝试了 request.getParameter("code"),但得到了 null。我检查了 tomcat 日志,确实看到代码被附加到末尾。 user [14/Jul/2021:15:35:43 -0400] "GET /app?code=fdsfs-fsd-4324-4324-fdsfsdfjh5u34 HTTP/1.1" 302 - 6553
    • 刚刚用更多信息更新了我的问题。
    猜你喜欢
    • 2020-09-03
    • 2020-02-26
    • 2021-03-24
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    • 1970-01-01
    • 2017-05-19
    相关资源
    最近更新 更多