Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android client for REST web service with basic security

I have write a simple RESTful web service using basic authentication.I use tips from this page secureRestWS. They have also created a video and posted on youtube with more details. It works just fine when you open it in browser. You need to write User Name and Password in authorization window.

I have also find a page with android client that call rest web service using httpclient and if the web service is without authentication it works. I am using this example androidRestWSClient. But I don`t know how to add User Name and Password in this scenario, I tried like :

client.AddParam("User Name", "myusername");
client.AddParam("Password", "mypassword");

or in header :

client.AddHeader("User Name", "myusername");
client.AddHeader("Password", "mypassword");

but nothing works. I also try to create url that tell the web service username and password like :

http://192.168.1.42/RestWS/resources/helloWorld?username=myusername&password=mypassword

I really don`t have a solution for this so if somebody have an example client I would appreciate that.

In webservice I have simple GET method

@GET
@Path("/text")
public String getText() {
    return "Hello World!";
}

Do I need to use SecurityContext for username and password? Is it better to create authentication manualy like in this example :

http://aruld.info/accessing-restful-services-configured-with-ssl-using-resttemplate/

Thanks for your help

like image 769
janilemy Avatar asked Dec 06 '11 14:12

janilemy


1 Answers

If you are using just basic http authentication, then the URL should look like this:

http://username:[email protected]/RestWS/resources/helloWorld

Well, here you go:

HttpGet httpget;
try{
    httpget = new HttpGet(url); 

    String auth =new String(Base64.encode(( username + ":" + password).getBytes(),Base64.URL_SAFE|Base64.NO_WRAP));
    httpget.addHeader("Authorization", "Basic " + auth);
    }
like image 174
Kaediil Avatar answered Nov 19 '22 02:11

Kaediil