如何在Spring应用程序中应用GitHub OAuth时修复“Whitelabel Error Page”错误?

huangapple go评论82阅读模式
英文:

How to fix 'Whitelabel Error Page' when applying GitHub OAuth in a Spring application?

问题

在我的Spring应用程序中尝试应用GitHub OAuth时出现Whitelabel Error Page错误。
这个应用程序没有明确的映射路径/error,所以你看到它作为一个后备页面。

2023年5月28日20:58:39 IRDT
发生了意外错误(类型=内部服务器错误,状态=500)。
由于输入结束,没有内容可映射到[来源:(字符串)"";行:1,列:0]。

这是我尝试打印或返回userDataResult.body后登录后出现错误的Java代码:

  1. @RestController
  2. public class LoginController {
  3. private String clientSecret = "541a3c6bdca41a233d593ed263f628915317c434";
  4. private String clientId = "f3cc203f5df4b2e30d5c";
  5. @GetMapping("/auth")
  6. public ResponseEntity<String> auth(@RequestBody String code) {
  7. String accessTokenString = String.format(
  8. "https://github.com/login/oauth/access_token?client_id=%s&client_secret=%s&code=%s", clientId,
  9. clientSecret, code);
  10. HttpClient client = HttpClient.newHttpClient();
  11. URI accessTokenUri = URI.create(accessTokenString);
  12. HttpRequest.Builder accessTokenBuilder = HttpRequest.newBuilder().uri(accessTokenUri);
  13. HttpRequest accessTokenRequest = accessTokenBuilder.POST(HttpRequest.BodyPublishers.noBody())
  14. .header("Accept", "Application/json").build();
  15. HttpResponse<String> accessTokenResult = null;
  16. try {
  17. accessTokenResult = client.send(accessTokenRequest, HttpResponse.BodyHandlers.ofString());
  18. } catch (IOException | InterruptedException e) {
  19. e.printStackTrace();
  20. }
  21. ObjectMapper mapper = new ObjectMapper();
  22. HashMap<String, Object> resultBody = new HashMap<>();
  23. try {
  24. resultBody = mapper.readValue(accessTokenResult.body(), HashMap.class);
  25. } catch (JsonProcessingException e1) {
  26. e1.printStackTrace();
  27. }
  28. String accessToken = (String) resultBody.get("access_token");
  29. URI userDataUri = URI.create("https://api.github.com/user");
  30. HttpRequest.Builder userDataBuilder = HttpRequest.newBuilder().uri(userDataUri);
  31. HttpRequest req = userDataBuilder.GET().header("Authorization", String.format("token %s", accessToken)).build();
  32. try {
  33. HttpResponse<String> userDataResult = client.send(req, HttpResponse.BodyHandlers.ofString());
  34. GithubUser user = mapper.readValue(userDataResult.body(), GithubUser.class);
  35. System.out.println(userDataResult.body());
  36. } catch (Exception e) {
  37. e.printStackTrace();
  38. }
  39. return null;
  40. }
  41. }

还编写了一个HTML来检查:

  1. <a href="https://github.com/login/oauth/authorize?client_id=f3cc203f5df4b2e30d5c&amp;scope=user"><h1>Click to login using github</h1></a>

已经尝试检查令牌是否为空,但我不知道为什么会为空,还尝试打印accessTokenResult.body()

英文:

Getting Whitelabel Error Page when try to apply Github OAuth in my spring application
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun May 28 20:58:39 IRDT 2023
There was an unexpected error (type=Internal Server Error, status=500).
No content to map due to end-of-input at [Source: (String)""; line: 1, column: 0]

Here is my java code which I try to get userDataResult.body printed or even returned but getting the error after log-in:

  1. @RestController
  2. public class LoginController {
  3. private String clientSecret = &quot;541a3c6bdca41a233d593ed263f628915317c434&quot;;
  4. private String clientId = &quot;f3cc203f5df4b2e30d5c&quot;;
  5. @GetMapping(&quot;/auth&quot;)
  6. public ResponseEntity&lt;String&gt; auth(@RequestBody String code) {
  7. String accessTokenString = String.format(
  8. &quot;https://github.com/login/oauth/access_token?client_id=%s&amp;client_secret=%s&amp;code=%s&quot;, clientId,
  9. clientSecret, code);
  10. HttpClient client = HttpClient.newHttpClient();
  11. URI accessTokenUri = URI.create(accessTokenString);
  12. HttpRequest.Builder accessTokenBuilder = HttpRequest.newBuilder().uri(accessTokenUri);
  13. HttpRequest accessTokenRequest = accessTokenBuilder.POST(HttpRequest.BodyPublishers.noBody())
  14. .header(&quot;Accept&quot;, &quot;Application/json&quot;).build();
  15. HttpResponse&lt;String&gt; accessTokenResult = null;
  16. try {
  17. accessTokenResult = client.send(accessTokenRequest, HttpResponse.BodyHandlers.ofString());
  18. } catch (IOException | InterruptedException e) {
  19. e.printStackTrace();
  20. }
  21. ObjectMapper mapper = new ObjectMapper();
  22. HashMap&lt;String, Object&gt; resultBody = new HashMap&lt;&gt;();
  23. try {
  24. resultBody = mapper.readValue(accessTokenResult.body(), HashMap.class);
  25. } catch (JsonProcessingException e1) {
  26. e1.printStackTrace();
  27. }
  28. String accessToken = (String) resultBody.get(&quot;access_token&quot;);
  29. URI userDataUri = URI.create(&quot;https://api.github.com/user&quot;);
  30. HttpRequest.Builder userDataBuilder = HttpRequest.newBuilder().uri(userDataUri);
  31. HttpRequest req = userDataBuilder.GET().header(&quot;Authorization&quot;, String.format(&quot;token %s&quot;, accessToken)).build();
  32. try {
  33. HttpResponse&lt;String&gt; userDataResult = client.send(req, HttpResponse.BodyHandlers.ofString());
  34. GithubUser user = mapper.readValue(userDataResult.body(), GithubUser.class);
  35. System.out.println(userDataResult.body());
  36. } catch (Exception e) {
  37. e.printStackTrace();
  38. }
  39. return null;
  40. }

Also wrote a html to check that out:

  1. &lt;a href=&quot;https://github.com/login/oauth/authorize?client_id=f3cc203f5df4b2e30d5c&amp;scope=user&quot;&gt;&lt;h1&gt;Click to login using github&lt;/h1&gt;&lt;/a&gt;

Already tried checking if the token is empty and I don't know why it should be, also tried to print accessTokenResult.body()

答案1

得分: 0

以下是您要求的翻译代码部分:

  1. @GetMapping(value = "/auth", produces = MediaType.APPLICATION_JSON_VALUE)
  2. public String auth(@RequestParam(value = "code") String code) throws Exception {
  3. String accessTokenURL = String.format(
  4. "https://github.com/login/oauth/access_token?client_id=%s&client_secret=%s&code=%s",
  5. clientId, clientSecret, code
  6. );
  7. HttpClient client = HttpClient.newHttpClient();
  8. URI accessTokenURI = URI.create(accessTokenURL);
  9. HttpRequest.Builder accessTokenBuilder = HttpRequest.newBuilder().uri(accessTokenURI);
  10. HttpRequest accessTokenRequest = accessTokenBuilder.POST(HttpRequest.BodyPublishers.noBody())
  11. .header("Accept", "application/json").build();
  12. HttpResponse<String> accessTokenResult = client.send(accessTokenRequest, HttpResponse.BodyHandlers.ofString());
  13. ObjectMapper mapper = new ObjectMapper();
  14. HashMap<String, Object> resultBody = mapper.readValue(accessTokenResult.body(), HashMap.class);
  15. String accessToken = (String) resultBody.get("access_token");
  16. URI userDateURI = URI.create("https://api.github.com/user");
  17. HttpRequest.Builder userDataBuilder = HttpRequest.newBuilder().uri(userDateURI);
  18. HttpRequest req = userDataBuilder.GET().header("Authorization", String.format("token %s", accessToken)).build();
  19. HttpResponse<String> userDataResult = client.send(req, HttpResponse.BodyHandlers.ofString());
  20. HashMap<String, Object> userData = mapper.readValue(userDataResult.body(), HashMap.class);
  21. return userDataResult.body();
  22. }
英文:

Changed the code and this worked:

  1. @GetMapping(value = &quot;/auth&quot;, produces = MediaType.APPLICATION_JSON_VALUE)
  2. public String auth(@RequestParam(value = &quot;code&quot;) String code) throws Exception {
  3. String accessTokenURL = String.format(
  4. &quot;https://github.com/login/oauth/access_token?client_id=%s&amp;client_secret=%s&amp;code=%s&quot;,
  5. clientId, clientSecret, code
  6. );
  7. HttpClient client = HttpClient.newHttpClient();
  8. URI accessTokenURI = URI.create(accessTokenURL);
  9. HttpRequest.Builder accessTokenBuilder = HttpRequest.newBuilder().uri(accessTokenURI);
  10. HttpRequest accessTokenRequest = accessTokenBuilder.POST(HttpRequest.BodyPublishers.noBody())
  11. .header(&quot;Accept&quot;, &quot;application/json&quot;).build();
  12. HttpResponse&lt;String&gt; accessTokenResult = client.send(accessTokenRequest, HttpResponse.BodyHandlers.ofString());
  13. ObjectMapper mapper = new ObjectMapper();
  14. HashMap&lt;String, Object&gt; resultBody = mapper.readValue(accessTokenResult.body(), HashMap.class);
  15. String accessToken = (String) resultBody.get(&quot;access_token&quot;);
  16. URI userDateURI = URI.create(&quot;https://api.github.com/user&quot;);
  17. HttpRequest.Builder userDataBuilder = HttpRequest.newBuilder().uri(userDateURI);
  18. HttpRequest req = userDataBuilder.GET().header(&quot;Authorization&quot;, String.format(&quot;token %s&quot;, accessToken)).build();
  19. HttpResponse&lt;String&gt; userDataResult = client.send(req, HttpResponse.BodyHandlers.ofString());
  20. HashMap&lt;String, Object&gt; userData = mapper.readValue(userDataResult.body(), HashMap.class);
  21. return userDataResult.body();
  22. }

huangapple
  • 本文由 发表于 2023年5月29日 00:40:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定