Discord OAuth2 重定向 url 无法使用 JavaFX WebView JDK12 显示
Discord OAuth2 redirect url fails to display with JavaFX WebView JDK12
我正在尝试获取以下站点:link to discord OAuth2 URL (hover me to see link) to load with JavaFX WebView in JDK12. When I try loading that site, all that is shown is a white page. The following code is what is used to display, and handle the WebView. I am using the library OAuth2Discord 来处理代码和授权。
DiscordAuth.java:
DiscordAuth()
This method initializes the OAuthBuilder used in the rest of the code below.
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线程)
This is meant to start the process of retrieving the code applied to the URL when redirected from the Auth url.
public void getUser(WebView webView, Thread thread) {
startOAuth2Flow(webView, thread);
}
startOAuth2Flow(WebView webView, Thread线程)
This method handles the site redirection as well as breaking up the URL to retrieve the code.
P.S. https://temperlesergal.github.io/NuBot/discordWebPage/index.html redirects to the Auth URL listed above
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(String code, Thread线程)
This method exchanges the code and initializes the following:
-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()
This method is what is triggered when the user presses login button on the FXML scene. It is meant to allow them to sign in to discord through the JavaFX application.
@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
说明:这是 运行 应用程序时的默认外观。
URL: https://temperlesergal.github.io/NuBot/discordWebPage/index.html
说明:这是我点击注销时WebView的样子(应该是登录,只是忘了改)。
[
URL: 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
描述:这是在按下 "Authorize" 后显示的白屏。
这是我加载到Chrome时看到的:
网址:
URL: 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
描述:这是机器人应该显示的内容。
This is the information printed to the console
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 将导致此问题。
对此事的任何和所有想法将不胜感激,谢谢。
我发现问题是 [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 模块。
我正在尝试获取以下站点:link to discord OAuth2 URL (hover me to see link) to load with JavaFX WebView in JDK12. When I try loading that site, all that is shown is a white page. The following code is what is used to display, and handle the WebView. I am using the library OAuth2Discord 来处理代码和授权。
DiscordAuth.java:
DiscordAuth()
This method initializes the OAuthBuilder used in the rest of the code below.
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线程)
This is meant to start the process of retrieving the code applied to the URL when redirected from the Auth url.
public void getUser(WebView webView, Thread thread) {
startOAuth2Flow(webView, thread);
}
startOAuth2Flow(WebView webView, Thread线程)
This method handles the site redirection as well as breaking up the URL to retrieve the code.
P.S. https://temperlesergal.github.io/NuBot/discordWebPage/index.html redirects to the Auth URL listed above
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(String code, Thread线程)
This method exchanges the code and initializes the following:
-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()
This method is what is triggered when the user presses login button on the FXML scene. It is meant to allow them to sign in to discord through the JavaFX application.
@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
说明:这是 运行 应用程序时的默认外观。
说明:这是我点击注销时WebView的样子(应该是登录,只是忘了改)。
[
描述:这是在按下 "Authorize" 后显示的白屏。
这是我加载到Chrome时看到的:
网址:
URL: 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
描述:这是机器人应该显示的内容。
This is the information printed to the console
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 将导致此问题。
对此事的任何和所有想法将不胜感激,谢谢。
我发现问题是 [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 模块。