[英]How to refactor chain of asynchronous calls in vertx to avoid the callback hell
[英]In Vertx - how to make a callback?
我對Vertx oauth2有問題。
我遵循了本教程http://vertx.io/docs/vertx-web/java/#_oauth2authhandler_handler :
OAuth2Auth authProvider = OAuth2Auth.create(vertx, OAuth2FlowType.AUTH_CODE, new OAuth2ClientOptions()
.setClientID("CLIENT_ID")
.setClientSecret("CLIENT_SECRET")
.setSite("https://github.com/login")
.setTokenPath("/oauth/access_token")
.setAuthorizationPath("/oauth/authorize"));
// create a oauth2 handler on our domain: "http://localhost:8080"
OAuth2AuthHandler oauth2 = OAuth2AuthHandler.create(authProvider, "http://localhost:8080");
// setup the callback handler for receiving the GitHub callback
oauth2.setupCallback(router.get("/callback"));
// protect everything under /protected
router.route("/protected/*").handler(oauth2);
// mount some handler under the protected zone
router.route("/protected/somepage").handler(rc -> {
rc.response().end("Welcome to the protected resource!");
});
// welcome page
router.get("/").handler(ctx -> {
ctx.response().putHeader("content-type", "text/html").end("Hello<br><a href=\"/protected/somepage\">Protected by Github</a>");
});
想法是在受保護的文件夾中包含所有需要身份驗證的網頁。
當我想訪問受保護的網頁時,我被重定向到Microsoft登錄站點,登錄后我被重定向到我的回調。
我不明白的是現在如何處理回調? 我得到這樣的回應:
https://localhost:8080/callback?code=AAABAAA...km1IgAA&session_state=....
我的理解方式( https://blog.mastykarz.nl/building-applications-office-365-apis-any-platform/ )我需要以某種方式提取代碼和會話狀態,然后將其發回:
https://login.microsoftonline.com/common/oauth2/token
為了獲得令牌。
但是我不明白如何用Vertx做到這一點。 有什么幫助嗎? 如何提取代碼和會話並發送回Microsoft?
我在這里找到了一些教程: https : //github.com/vert-x3/vertx-auth/blob/master/vertx-auth-oauth2/src/main/java/examples/AuthOAuth2Examples.java但對我沒有幫助。
我正在使用Azure身份驗證來完成此工作(在教程中編寫為Github,但我將所有這些更改為Microsoft)。
您是代理人嗎? 回調處理程序從應用程序而非瀏覽器向提供者發送請求。 對我來說,這凍結了整個應用程序。 您可以使用提供給OAuth2Auth.create的OAuth2ClientOptions設置代理。
如官方vert.x-web文檔中所述,auth流的處理(包括對Microsoft的訪問令牌請求)由OAuth2AuthHandler處理:
OAuth2AuthHandler將設置適當的回調OAuth2處理程序,因此用戶不需要處理授權服務器響應的驗證。
這就是說,不需要應用程序手動處理它。 而不是使用vertx-auth的例子,試試這一個 ,而不是其實際使用OAuth2AuthHandler。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.