【问题标题】:Obtaining Authorization Code from Spring OAuth 2.0 Authorization Server programmatically以编程方式从 Spring OAuth 2.0 授权服务器获取授权码
【发布时间】:2025-12-31 14:10:17
【问题描述】:

我正在尝试使用简单的 CURL 命令从 Spring OAuth 2.0 授权服务器获取授权代码。

curl -v --header "Authorization: Basic hasfhfashfakhsfakhf712641246" "http://0.0.0.0:0000/oauth-server/oauth2/authorize?response_type=code&client_id=dummyclient&client_secret=dummyclient&redirect_uri=http://oauth2server/oauth2callback/"

但是,我没有返回带有代码的重定向 URL,而是返回对登录 URL 的 302 重定向响应。由于我已经在 Authorization Header 中发送了用户名、密码,有没有办法跳过登录页面重定向并直接获取授权码?

【问题讨论】:

    标签: spring spring-security-oauth2 oauth2 spring-oauth2


    【解决方案1】:

    您可能需要告诉 curl 使用 -L 标志跟随重定向

    curl -L -v --header "Authorization: Basic hasfhfashfakhsfakhf712641246" "http://0.0.0.0:0000/oauth-server/oauth2/authorize?response_type=code&client_id=dummyclient&client_secret=dummyclient&redirect_uri=http://oauth2server/oauth2callback/"
    

    【讨论】:

    • 谢谢,但现在跟随重定向会给我一个登录页面。但是,关键是我已经在 header 中传递了授权信息,所以,我真的不希望这里有登录页面。