【问题标题】:Is Authorization code mandatory to grant resource access?授权代码是否必须授予资源访问权限?
【发布时间】:2020-01-31 00:14:30
【问题描述】:

我正在开发一个 Spring Boot Web 应用程序,该应用程序需要从 3rd 方 Web 应用程序访问资源。我试图了解 Oauth2 的工作原理。第 3 方 Web 应用程序使用 Oauth2 授予客户端资源访问权限。来自 3rd 方网络应用程序的文档说要发送带有请求参数的 POST,格式如下。

用户名=&password=&client_id=&client_secret=&grant_type=password&hcode=

hcode 值根据文档是固定的。 我能够编写一个成功获取我访问令牌的 java 代码(感谢 Google 搜索!)。 以下是我的问题...

  1. 这里使用的是什么类型的授权?
  2. 此授权码是否授予?我在这里没有看到授权码。

PS:我是 Web 应用程序开发的新手。我指的是下面的帖子来了解 Oauth2。 [https://www.javainuse.com/spring/spring-boot-oauth-introduction]

String content = "-----";
BufferedReader reader = null;
HttpsURLConnection connection = null;
String returnValue = "";

URL url = new URL(CredentialDto.getTockenurl());
connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Authorization", "Basic " + 
CredentialDto.getAuthentication());
connection.setRequestProperty("Content-Type", "application/x-www-form- 
urlencoded");
connection.setRequestProperty("Accept", "application/json");
PrintStream os = new PrintStream(connection.getOutputStream());

os.print(content);

os.close();
reader = new BufferedReader(new 
InputStreamReader(connection.getInputStream()));
String line = null;

StringWriter out = new StringWriter(connection.getContentLength() > 0 ? 
connection.getContentLength() : 2048);

while((line = reader.readLine()) != null) {
        out.append(line);
        }

accessToken = out.toString();       
Matcher matcher = PAT.matcher(retuenValue); if(matcher.matches() && 
matcher.groupCount() > 0) { 
              accessToken = matcher.group(1); 
            }

【问题讨论】:

    标签: oauth-2.0 spring-security-oauth2


    【解决方案1】:

    1 这里使用的是什么类型的授权?

    资源所有者密码凭证 (ROPC)。检查这个RFC

    2 这是授权码授予吗?我在这里没有看到授权码。

    没有。

    【讨论】:

    • 感谢您的回复。该参数具有 hcode,它是与资源相关的代码(在这种情况下为诊所代码)。为什么在访问令牌获取阶段会问这个问题?获得令牌后,我正在检索患者详细信息。
    • 我有点理解为什么在参数中发送 clientID、clientSecret、userID 和 userPassword。我不清楚为什么要发送 grantType 和 hcode。
    • OAuth 中有多种授权类型(隐式、授权代码、客户端凭据等和 ROPC)。因此,您需要指定您正在使用的授权类型,以便资源服务器可以启动该流程。 hcode 不是标准的 OAuth 参数,可能是诊所代码特有的。
    猜你喜欢
    • 1970-01-01
    • 2018-12-20
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多