首页
学习
活动
专区
圈层
工具
发布

for oAuth2
EN

Stack Overflow用户
提问于 2018-03-16 02:18:23
回答 1查看 226关注 0票数 0

为了oAuth2身份验证流的目的,我试图在本地Java应用程序中编写web服务器。( web服务器是客户端,而不是流中的服务器。)

我尝试过使用HTTP启动web服务器

代码语言:javascript
复制
public static void startServer() throws IOException {
    int port = 13370;
    HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
    server.createContext("/test", new MyHandler());
    server.setExecutor(null);
    server.start();
}

static class MyHandler implements HttpHandler {
    @Override
    public void handle(HttpExchange t) throws IOException {
        String response = "This is the response";
        t.sendResponseHeaders(200, response.length());
        OutputStream os = t.getResponseBody();
        os.write(response.getBytes());
        os.close();
    }
}

然而,它所做的就是在http://127.0.0.1:13370/test上托管一个网页,并在那里显示响应。在oAuth流中,服务器将发送上述web服务器不期望的授权代码(如http://127.0.0.1:13370/?code=[code]&state=[state] )。

是否有一种方法可以将服务器配置为期望响应?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-16 03:18:21

作为标准oAuth的一部分,您可以在访问authorization-url时指定return_url

例如,您是这样告诉服务提供者的,授权后应该在哪里重定向。

代码语言:javascript
复制
https://github.com/login/oauth/authorize?response_type=code&client_id=156d37xxxxxxxxx&redirect_uri=http%3A%2F%2Flocalhost%3A13370%2Ftest&state=secret846593

在上面的url中,redirect_urihttp://localhost:13370/test

在进行此更改之后,您应该能够在code方法中捕获MyHandler#handle

有关整个my this answer流,请参考oAuth。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49312216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档