Vim*_*Joe 4 authentication graphql apollo-client graphql-java
我正在开发一个使用 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;
}
}
Run Code Online (Sandbox Code Playgroud)
请注意 :
如果你投反对票,那么请为自己辩解。
您需要在 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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1228 次 |
| 最近记录: |