我正在尝试使用AndroidAnnotations @Rest RestClient
接口通过令牌使用REST API进行身份验证。
但是由于缺少文档,我无法使用setBearerAuth(String token);
我已经拥有界面和class HeadersRequestInterceptor implements ClientHttpRequestInterceptor
,但我不知道在哪里拨打setBearerAuth(myCustomToken);
如果有人能给我一个提示,我会非常优雅。
干杯
答案 0 :(得分:1)
MyRestClient.java:
@RequiresAuthentication
@Rest(rootUrl = "your_url", converters = {...})
public interface MyRestClient extends RestClientHeaders {
@Post("/somecall")
public void someApiCall();
}
MyActivity.java:
@EActivity
public class MyActivity extends Activity {
@RestService
MyRestClient client;
@Background
void callSomeApi() {
String accessToken = ... ; // was previously returned from server
client.setBearerAuth(accessToken);
client.someApiCall(); // it will add header: Authorization: Bearer accessToken
}
}
答案 1 :(得分:0)
我找到了一个更好的方法,让我解释一下: 如果您需要在所有Android REST调用中应用它,那么我们需要创建一个Interceptor来获取应用程序发出的所有REST请求,如下所示:
import android.content.Context;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;
import org.androidannotations.rest.spring.annotations.Rest;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import java.io.IOException;
/**
* Created by Rafael Rocha on 21/05/2018.
*/
@EBean
public class HttpBasicAuthenticatorInterceptor implements ClientHttpRequestInterceptor {
@RootContext
Context context;
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
request.getHeaders().add("Authorization",AUTHENTICATION_SCHEME + "your_token_goes_here");
return execution.execute(request, body);
}
}
用您的身份验证方法替换AUTHENTICATION_SCHEME,在我的情况下是“Bearer”,并且不要忘记在它们之间留一个空格“Bearer YOUR_TOKEN”。 之后只是在你的REST服务类中对@Rest注释填充属性“拦截器”
import org.androidannotations.rest.spring.annotations.Accept;
import org.androidannotations.rest.spring.annotations.Get;
import org.androidannotations.rest.spring.annotations.Header;
import org.androidannotations.rest.spring.annotations.Path;
import org.androidannotations.rest.spring.annotations.Rest;
import org.androidannotations.rest.spring.api.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import java.util.List;
/**
* Created by Rafael Rocha on 13/03/2018.
*/
@Rest(rootUrl = RestUtil.ROOT_URL, converters = {MappingJackson2HttpMessageConverter.class}, interceptors = {HttpBasicAuthenticatorInterceptor.class})
@Accept(MediaType.APPLICATION_JSON)
public interface NotificationRest {
//@Header(name = "server_version", value = SERVER_VERSION)
@Get("notificacao/profissional/recentes/{idProfessional}")
List<ResponseNotification> getNotifications(@Path Long idProfessional);
}
我唯一做的就是在请求标头中添加一个Header值。你可以通过我的应用程序来传递其他东西例如这里需要将我的应用程序android版本传递给服务器端处理旧应用程序或者只是阻止访问,所以我只需要增加代码中的更多行
request.getHeaders().add(RestUtil.SERVER_VERSION_HEADER,RestUtil.SERVER_VERSION);