【发布时间】: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 搜索!)。 以下是我的问题...
- 这里使用的是什么类型的授权?
- 此授权码是否授予?我在这里没有看到授权码。
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