【问题标题】:Authentication Apollo Graphql for android身份验证 Apollo Graphql for android
【发布时间】:2019-02-25 17:50:42
【问题描述】:

我正在开发一个使用 GraphQL 作为后端的 Android 应用程序。我的查询和变异部分工作得很好。但我找不到任何用于身份验证的文件。

那么如何将用户名和密码传递给服务器并进行身份验证呢?

LocalApolloClient.java:

public class LocalApolloClient {
    private static final String URL = "http://192.168.1.100/graphql/";
    private static ApolloClient apolloClient;
    private static String authHeader = "";


    public static ApolloClient getApolloClient(){
        //HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        //loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(chain -> {
                    Request original = chain.request();
                    Request.Builder builder = original.newBuilder().method(original.method(), original.body());
                    builder.header("Authorization", authHeader);
                    return chain.proceed(builder.build());
                })
                .build();

        apolloClient = ApolloClient.builder()
                .serverUrl(URL)
                .okHttpClient(okHttpClient)
                .build();

        return apolloClient;
    }
}

请注意:

  • 这不是重复的问题
  • 没有合适的 graphQl 文档

如果您投反对票,请说明理由。

【问题讨论】:

  • 您找到解决方案了吗?我面临同样的身份验证问题。

标签: authentication graphql apollo-client graphql-java


【解决方案1】:

您需要在 ApolloClient 上设置标头,而不是在 OkHttpClient 上。 像这样的:

ApolloClient.builder()
            .serverUrl(context.getString(R.string.graphql_base_url))
            .addApplicationInterceptor(object: ApolloInterceptor {
                override fun interceptAsync(
                    request: ApolloInterceptor.InterceptorRequest,
                    chain: ApolloInterceptorChain,
                    dispatcher: Executor,
                    callBack: ApolloInterceptor.CallBack
                ) {
                    val newRequest = request.toBuilder().requestHeaders(RequestHeaders.builder().addHeader("Authorization", "Basic ...").build()).build()
                    chain.proceedAsync(newRequest, dispatcher, callBack)
                }

                override fun dispose() {
                }
            }).build()

【讨论】:

  • ApolloClient和OkHttp拦截器有什么区别?
  • @DuncanLuk 我看到的一大区别是 OkHttp 拦截器在底层线程池中执行,而 apollo 拦截器在执行请求的同一线程中执行。因此,apollo 拦截器允许从 Threadlocal 中获取值,例如
【解决方案2】:

如果要使用用户名和密码进行身份验证,则必须对用户名和密码进行 base64 编码并将其传递给变量authHeader。例如,对于用户名 Aladdin 和密码 OpenSesame,您会得到:

private static String authHeader = "Basic QWxhZGRpbjpPcGVuU2VzYW1l";

见:https://en.wikipedia.org/wiki/Basic_access_authentication

【讨论】:

    【解决方案3】:

    试试这个

    1. HTTP服务器可以设置为HTTPS服务器
    2. 服务器还有用户名/密码数据库(密码可能用 bcrypt 保存)
    3. 客户端打开 HTTPS 连接,对服务器进行身份验证(因此需要服务器证书)并在交换主服务器后 密钥,连接应该被加密。
    4. 客户端将用户名/密码明文发送给服务器
    5. 服务器对密码运行 bcrypt 并将其与存储在数据库中的密码进行比较

    如果您有任何疑问,请参考此original post.

    注意:我个人从未尝试过。

    【讨论】:

      猜你喜欢
      • 2018-05-23
      • 2020-08-17
      • 1970-01-01
      • 2018-06-22
      • 2018-08-18
      • 2021-03-31
      • 2018-12-03
      • 2020-10-22
      • 2018-11-09
      相关资源
      最近更新 更多