I'm looking through examples and code but I don't see anything implemented. Is this possible at this stage?
Yes it's possible. You need to override Request.getHeaders(). I'm lazy and I used HttpHeaders and HttpAuthentication from Spring for Android but you can just build the auth header and return it from the method. From getHeaders() you can return the auth header for basic auth. This is a sample request with basic auth.
public class GetUser extends Request<User> {
private static final String TAG = GetUser.class.getName();
private Response.Listener<User> mListener;
private ObjectMapper mMapper = new ObjectMapper();
public GetUser(Response.ErrorListener errorListener, Response.Listener<User> listener){
super(Method.GET, PoisUtils.BASE_URL + "/users", errorListener);
mListener = listener;
}
@Override
protected Response<User> parseNetworkResponse(NetworkResponse response) {
String jsonString = new String(response.data);
try {
User result = mMapper.readValue(jsonString, User.class);
return Response.success(result, getCacheEntry());
} catch (IOException e) {
Log.d(TAG, e.getMessage());
}
return null;
}
@Override
protected void deliverResponse(User response) {
mListener.onResponse(response);
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return AuthUtils.buildAuthHeaders().toSingleValueMap();
}
}
And here is how I build the auth headers
public static HttpHeaders buildAuthHeaders(){
if(UserUtils.isUserLogged()){
HttpHeaders requestHeaders = new HttpHeaders();
User user = PoisApplication.get().getUser();
HttpAuthentication auth = new HttpBasicAuthentication(
user.getUsername(), user.getPassword());
requestHeaders.setAuthorization(auth);
return requestHeaders;
}
return null;
}
For those who don't want to use Spring for Android just for that, here's how to do it.
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> params = new HashMap<String, String>();
String creds = String.format("%s:%s","USERNAME","PASSWORD");
String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
params.put("Authorization", auth);
return params;
}
Note that you may have to use Base64.NO_WRAP
instead of Base64.DEFAULT
for this to work. As pointed in the comments.
API 8+
Base64.NO_WRAP
instead of Base64.DEFAULT
for it to work. I am using Play and it gave exception p.nettyException - Exception caught in Netty java.lang.IllegalArgumentException: Header name cannot contain the following prohibited characters: =,;: \t\r\n\v\f
when i used Base64.DEFAULT
. Any idea why? Also please edit your post with this change. –
Pyroxene Yes it's possible. You need to override Request.getHeaders(). I'm lazy and I used HttpHeaders and HttpAuthentication from Spring for Android but you can just build the auth header and return it from the method. From getHeaders() you can return the auth header for basic auth. This is a sample request with basic auth.
public class GetUser extends Request<User> {
private static final String TAG = GetUser.class.getName();
private Response.Listener<User> mListener;
private ObjectMapper mMapper = new ObjectMapper();
public GetUser(Response.ErrorListener errorListener, Response.Listener<User> listener){
super(Method.GET, PoisUtils.BASE_URL + "/users", errorListener);
mListener = listener;
}
@Override
protected Response<User> parseNetworkResponse(NetworkResponse response) {
String jsonString = new String(response.data);
try {
User result = mMapper.readValue(jsonString, User.class);
return Response.success(result, getCacheEntry());
} catch (IOException e) {
Log.d(TAG, e.getMessage());
}
return null;
}
@Override
protected void deliverResponse(User response) {
mListener.onResponse(response);
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return AuthUtils.buildAuthHeaders().toSingleValueMap();
}
}
And here is how I build the auth headers
public static HttpHeaders buildAuthHeaders(){
if(UserUtils.isUserLogged()){
HttpHeaders requestHeaders = new HttpHeaders();
User user = PoisApplication.get().getUser();
HttpAuthentication auth = new HttpBasicAuthentication(
user.getUsername(), user.getPassword());
requestHeaders.setAuthorization(auth);
return requestHeaders;
}
return null;
}
For a proxy authorization (like squid) use this header :
String credentials = proxyUsername + ":" + proxyPassword;
String auth = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
headers.put("Proxy-Authorization", auth);
© 2022 - 2024 — McMap. All rights reserved.