Discord OAuth2 重定向 URL 无法使用 JavaFX WebView JDK12 显示
Posted
技术标签:
【中文标题】Discord OAuth2 重定向 URL 无法使用 JavaFX WebView JDK12 显示【英文标题】:Discord OAuth2 redirect url fails to display with JavaFX WebView JDK12 【发布时间】:2020-01-17 19:41:15 【问题描述】:我正在尝试获取以下站点:link to discord OAuth2 URL (hover me to see link) 以在 JDK12 中加载 JavaFX WebView。当我尝试加载该站点时,显示的只是一个白页。以下代码用于显示和处理 WebView。我正在使用库 OAuth2Discord 来处理代码和授权。DiscordAuth.java:DiscordAuth()
此方法初始化下面其余代码中使用的 OAuthBuilder。
public DiscordAuth()
CLIENT_SECRET = [REDACTED];
CLIENT_ID = [REDACTED];
REDIRECT_URL = "https://temperlesergal.github.io/NuBot/discordWebPage/success.html";
builder = new OAuthBuilder(CLIENT_ID, CLIENT_SECRET)
.setScopes(new String[]"connections", "guilds", "email")
.setRedirectURI(REDIRECT_URL);
getUser(WebView webView, Thread 线程)
这是为了在从 Auth url 重定向时开始检索应用于 URL 的代码的过程。
public void getUser(WebView webView, Thread thread)
startOAuth2Flow(webView, thread);
startOAuth2Flow(WebView webView, Thread thread)
此方法处理站点重定向以及分解 URL 以检索代码。 附言https://temperlesergal.github.io/NuBot/discordWebPage/index.html 重定向到上面列出的 Auth URL
private void startOAuth2Flow(WebView webView, Thread thread)
String authURL = builder.getAuthorizationUrl(null);
WebEngine webEngine = webView.getEngine();
webEngine.setjavascriptEnabled(true);
webEngine.setUserAgent("Cotton Le Sergal's OAuth2 grant for app.");
webEngine.getLoadWorker().stateProperty().addListener(
(ov, oldState, newState) ->
if (webEngine.getLoadWorker().getException() != null || newState == Worker.State.FAILED)
System.err.println(webEngine.getLoadWorker().getException().toString());
else
System.out.println(webEngine.getLoadWorker().getState());
);
webEngine.locationProperty().addListener((observableValue, oldLocation, newLocation) ->
System.out.println("newLocation = " + newLocation);
if (newLocation.startsWith(REDIRECT_URL) && newLocation.contains("code"))
try
URL url = new URL(newLocation);
String[] params = url.getQuery().split("&");
Map<String, String> map = new HashMap<>();
for (String param : params)
String name = param.split("=")[0];
String value = param.split("=")[1];
map.put(name, value);
code = map.get("code");
gotTheAccessCode(code, thread);
System.out.println("Notifying thread");
synchronized (thread)
thread.notify();
thread.start();
System.out.println("Thread Notified!");
catch (MalformedURLException e)
e.printStackTrace();
);
String url = "https://temperlesergal.github.io/NuBot/discordWebPage/index.html";
webEngine.load(url);
gotTheAccessCode(字符串代码,线程线程)
此方法交换代码并初始化以下内容: -builder.getUser(); -builder.getGuilds(); -builder.getConnections()
private void gotTheAccessCode(String code, Thread thread)
System.out.println("access code: " + code);
Response response = builder.exchange(code);
if (response == Response.ERROR)
// AN ERROR HAPPENED WHILE EXCHANGING THE CODE
else
// EVERYTHING WORKED AS EXPECTED
System.out.println("Testing by printing guild info!");
getGuildInfo().forEach(guild -> System.out.println(guild.getName()));
System.out.println("DONE!");
user = builder.getUser();
guilds = builder.getGuilds();
connections = builder.getConnections();
FXMLController.java:signIn()
这个方法是当用户在 FXML 场景中按下登录按钮时触发的。这意味着允许他们通过 JavaFX 应用程序登录不和谐。
@FXML
void signIn()
Thread thread = new Thread(() ->
System.out.println("Thread waking up!");
Platform.runLater(() ->
usernameLabel.setText(discordAuth.getUserNameWithDiscriminator());
setUserAvatar(discordAuth.getUserAvatarURL());
);
);
try
if(thread.isAlive())
thread.wait();
catch (InterruptedException e)
e.printStackTrace();
discordSigninWebView.setVisible(true);
discordSigninWebView.setOpacity(0.0);
FadeIn signInFade = new FadeIn(discordSigninWebView);
signInFade.play();
discordAuth.getUser(discordSigninWebView, thread);
这是我在加载到应用程序时看到的:机器人: URL:没有 URL 加载到 WebEngine 描述:这是运行应用程序时的默认外观。 网址:https://temperlesergal.github.io/NuBot/discordWebPage/index.html 描述:这是我点击注销时 WebView 的样子(应该是登录,只是忘记更改了)。 [ 网址:https://discordapp.com/oauth2/authorize?client_id=569662931990478857&redirect_uri=https%3A%2F%2Ftemperlesergal.github.io%2FNuBot%2FdiscordWebPage%2Fsuccess.html&response_type=code&scope=identify%20email%20connections%20guilds 说明:这是按下“授权”后显示的白屏。 这是我加载到 Chrome 时看到的内容:网络: 网址:https://discordapp.com/oauth2/authorize?client_id=569662931990478857&redirect_uri=https%3A%2F%2Ftemperlesergal.github.io%2FNuBot%2FdiscordWebPage%2Fsuccess.html&response_type=code&scope=identify%20email%20connections%20guilds 描述:这是机器人应该显示的内容。
这是打印到控制台的信息
Image size is: 800.0x800.0 with the width being the largest at: 800.0px.
Image is 8.0 times the size it should be... resizing
Image has been resized to the following: 100.0x100.0px.
Actual image size is: 100.0x100.0px.
Test
0 [OkHttp https://discordapp.com/...] DEBUG net.dv8tion.jda.internal.requests.Requester - Received response with following cf-rays: [5176ddc69aaae202-ORD]
317 [OkHttp https://discordapp.com/...] DEBUG net.dv8tion.jda.internal.requests.Requester - Received response with following cf-rays: [5176ddc83f13e202-ORD]
320 [Thread-3] INFO net.dv8tion.jda.api.JDA - Login Successful!
[0 / 3]
578 [OkHttp https://discordapp.com/...] DEBUG net.dv8tion.jda.internal.requests.Requester - Received response with following cf-rays: [5176ddca3e3ac510-ORD]
Host Name: [REDACTED]
Host Address: [REDACTED]
693 [JDA [0 / 3] MainWS-ReadThread] INFO net.dv8tion.jda.internal.requests.WebSocketClient - Connected to WebSocket
693 [JDA [0 / 3] MainWS-ReadThread] DEBUG net.dv8tion.jda.internal.requests.WebSocketClient - Sending Identify-packet...
734 [JDA [0 / 3] MainWS-ReadThread] DEBUG net.dv8tion.jda.internal.requests.WebSocketClient - Got HELLO packet (OP 10). Initializing keep-alive.
754 [OkHttp https://discordapp.com/...] DEBUG net.dv8tion.jda.internal.requests.Requester - Received response with following cf-rays: [5176ddcaaf3dc510-ORD]
754 [Thread-3] INFO net.dv8tion.jda.api.JDA - Login Successful!
[1 / 3]
832 [JDA [0 / 3] MainWS-ReadThread] DEBUG net.dv8tion.jda.internal.handle.GuildSetupController - Setting incomplete count to 0
832 [JDA [0 / 3] MainWS-ReadThread] INFO net.dv8tion.jda.api.JDA - Finished Loading!
Shard: 1 out of: 3 is ready.
842 [JDA [0 / 3] Gateway-Worker 1] DEBUG net.dv8tion.jda.internal.requests.WebSocketClient - Sending normal message "op":3,"d":"game":"name":"Type owo.help","type":0,"afk":false,"status":"online","since":1568679123052
883 [OkHttp https://discordapp.com/...] DEBUG net.dv8tion.jda.internal.requests.Requester - Received response with following cf-rays: [5176ddcc2a44c56c-ORD]
998 [OkHttp https://discordapp.com/...] DEBUG net.dv8tion.jda.internal.requests.Requester - Received response with following cf-rays: [5176ddccab8dc56c-ORD]
998 [Thread-3] INFO net.dv8tion.jda.api.JDA - Login Successful!
[2 / 3]
5952 [JDA [1 / 3] MainWS-ReadThread] INFO net.dv8tion.jda.internal.requests.WebSocketClient - Connected to WebSocket
5953 [JDA [1 / 3] MainWS-ReadThread] DEBUG net.dv8tion.jda.internal.requests.WebSocketClient - Sending Identify-packet...
5955 [JDA [1 / 3] MainWS-ReadThread] DEBUG net.dv8tion.jda.internal.requests.WebSocketClient - Got HELLO packet (OP 10). Initializing keep-alive.
6047 [JDA [1 / 3] MainWS-ReadThread] DEBUG net.dv8tion.jda.internal.handle.GuildSetupController - Setting incomplete count to 1
6082 [JDA [1 / 3] MainWS-ReadThread] INFO net.dv8tion.jda.api.JDA - Finished Loading!
Shard: 2 out of: 3 is ready.
6084 [JDA [1 / 3] MainWS-ReadThread] DEBUG net.dv8tion.jda.internal.handle.GuildSetupController - Finished setup for guild 530802775530012672 firing cached events 0
6246 [JDA [1 / 3] Gateway-Worker 1] DEBUG net.dv8tion.jda.internal.requests.WebSocketClient - Sending normal message "op":3,"d":"game":"name":"Type owo.help","type":0,"afk":false,"status":"online","since":1568679128295
11239 [JDA [2 / 3] MainWS-WriteThread] INFO net.dv8tion.jda.internal.requests.WebSocketClient - Connected to WebSocket
11240 [JDA [2 / 3] MainWS-WriteThread] DEBUG net.dv8tion.jda.internal.requests.WebSocketClient - Sending Identify-packet...
11241 [JDA [2 / 3] MainWS-ReadThread] DEBUG net.dv8tion.jda.internal.requests.WebSocketClient - Got HELLO packet (OP 10). Initializing keep-alive.
11368 [JDA [2 / 3] MainWS-ReadThread] DEBUG net.dv8tion.jda.internal.handle.GuildSetupController - Setting incomplete count to 0
11368 [JDA [2 / 3] MainWS-ReadThread] INFO net.dv8tion.jda.api.JDA - Finished Loading!
Shard: 3 out of: 3 is ready.
11504 [JDA [2 / 3] Gateway-Worker 1] DEBUG net.dv8tion.jda.internal.requests.WebSocketClient - Sending normal message "op":3,"d":"game":"name":"Type owo.help","type":0,"afk":false,"status":"online","since":1568679133581
newLocation = https://temperlesergal.github.io/NuBot/discordWebPage/index.html
SCHEDULED
RUNNING
SUCCEEDED
newLocation = https://discordapp.com/api/oauth2/authorize?client_id=569662931990478857&redirect_uri=https%3A%2F%2Ftemperlesergal.github.io%2FNuBot%2FdiscordWebPage%2Fsuccess.html&response_type=code&scope=identify%20email%20connections%20guilds
SCHEDULED
RUNNING
newLocation = https://discordapp.com/oauth2/authorize?client_id=569662931990478857&redirect_uri=https%3A%2F%2Ftemperlesergal.github.io%2FNuBot%2FdiscordWebPage%2Fsuccess.html&response_type=code&scope=identify%20email%20connections%20guilds
SUCCEEDED
再现性:100% 只需尝试在 WebView 中加载 Auth url 就会导致此问题。 非常感谢您对此事的任何和所有想法,谢谢。
【问题讨论】:
【参考方案1】:我发现问题是 [WebView 子资源完整性检查在 Windows 和 Linux 上失败。
这可以在以下链接中看到和证明:
-
https://bugs.openjdk.java.net/browse/JDK-8219917
https://github.com/javafxports/openjdk-jfx/issues/230
如何解决手头的问题
Please view link no.1 for more in depth details
简而言之,问题在于用于 JavaFX 的 OpenJDK 12 存在一个错误,该错误导致 Windows 和 Linux 平台上的子资源完整性检查失败,当它没有提供与获取的资源中的哈希匹配的加密哈希时导致问题。因此,网页的内容将无法正确显示。解决方法列于link no.1。该错误已在 JavaFX 13 中修复,因此解决此问题所需要做的就是更新我的 maven pom 文件中的 JavaFX 模块。
【讨论】:
以上是关于Discord OAuth2 重定向 URL 无法使用 JavaFX WebView JDK12 显示的主要内容,如果未能解决你的问题,请参考以下文章
Spring Security Oauth2:无效的重定向 url
Spring boot,Security,OAuth2:是不是可以使用自定义 AuthorizationCodeResourceDetails?身份验证服务器需要重定向 url 中的特定参数