Re: [Mifos-developer] Question : REST API and mifos authentication

326 views
Skip to first unread message

Udai Gupta

unread,
Nov 16, 2011, 9:30:23 AM11/16/11
to Satish, Developer
Hi Satish,

You are right, there should be a way to use RestTemplate, haven't had
time to explore about this but I will check once I get my hand free.

Udai

On Wed, Nov 16, 2011 at 7:57 PM, Satish <www.s...@gmail.com> wrote:
> Hi Udai,
> Thank you for the response. I did follow you mifos-rest.py example and it
> was helpful.  I was using  Spring's RestTemplate and was having problem in
> saving session for subsequent rest calls. However, I have managed to
> implement it using HttpClient. Solution is working but i feel it cannot be
> so crude and there must exist a better solution.
>
> This is what i have done:
> //This method prepare the connections and return the session with
> authentication.
> //class: RESTClient
>
> public HttpClient prepareConnection() throws HttpException, IOException{
>
>
>
>   logger.debug("Establishing http session with mifos");
>
>   httpConn = new HttpClient();
>
> Credentials credentials = new
> UsernamePasswordCredentials(username,password);
>
> httpConn.getParams().setAuthenticationPreemptive(true);
>
> httpConn.getState().setCredentials(AuthScope.ANY, credentials);
>
> HttpConnectionManagerParams conpar = new HttpConnectionManagerParams();
>
> conpar.setConnectionTimeout(5 * 60000); // 5 minutes
>
> conpar.setSoTimeout(5 * 60000); // 5 minutes
>
> httpConn.getHttpConnectionManager().setParams(conpar);
>
> GetMethod get = new GetMethod(URL);
>
> get.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
>
> get.setDoAuthentication(true);
>
> httpConn.executeMethod(get);
>
> String responseBody = get.getResponseBodyAsString();
>
> if (responseBody.contains("j_spring_security_check")) {
>
> String loginUrl = URL +
> "/j_spring_security_check?j_username="+username+"&j_password="+password;
>
> PostMethod authpost = new PostMethod(loginUrl);
>
> // Release Get Connection
>
> get.releaseConnection();
>
> httpRes = httpConn.executeMethod(authpost);
>
> responseBody = authpost.getResponseBodyAsString();
>
> if (httpRes == 301 || httpRes == 302 || httpRes == 307) {
>
> // redirected, get content page
>
> get = new GetMethod(authpost.getResponseHeader("Location")
>
> .getValue());
>
> System.out.println(authpost.getResponseHeader("Location").getValue());
>
> get.setRequestHeader("Content-Type","text/plain; charset=UTF-8");
>
> authpost.releaseConnection();
>
> httpRes = httpConn.executeMethod(get);
>
> responseBody = get.getResponseBodyAsString();
>
> get.releaseConnection();
>
> }
>
> } else {
>
> get.releaseConnection();
>
> }
>
> return httpConn;
>
>
>
>   }
>
> After i get this authenticated session, I create RestTemplate and invoke
> REST services.
>
> RESTClient rs = new RESTClient("mifos", "testmifos");
>
> HttpClient httpClient = rs.prepareConnection();
>
> CommonsClientHttpRequestFactory cf = new
> CommonsClientHttpRequestFactory(httpClient);
>
> RestTemplate restTemplate = new RestTemplate(cf);
>
> String url = "http://localhost:9081/mifos/personnel/id-current.json";
>
> HttpHeaders headers = new HttpHeaders();
>
> headers.setContentType(MediaType.APPLICATION_JSON);
>
> String result = restTemplate.getForObject(url, String.class);
>
> Could you please advise if there is a better way to achieve this? Thanks.
> Regards,
> Satish
>
>
> On 16 November 2011 21:51, Udai Gupta <mail...@gmail.com> wrote:
>>
>> I have an example how the authentication works using python.
>> https://github.com/mifos/head/blob/master/rest/scripts/mifos-rest.py#L47
>>
>> url = base_url + '/j_spring_security_check'
>> data = urllib.urlencode({'j_username' : username, 'j_password' :
>> password, 'spring-security-redirect' : '/status.json'})
>> headers =  {'User-agent' : 'Mifos REST Client',
>> 'Content-Type':'application/x-www-form-urlencoded'}
>> req = Request(url, data, headers)
>>
>> You have to submit these parameters.
>> j_usename = mifos
>> j_password = testmifos
>> spring-security-redirect: /status.json
>>
>> to URL like http://localhost:8080/mifos/j_spring_security_check
>> with
>> 'Content-Type':'application/x-www-form-urlencoded'
>>
>> The response on successful login will contain cookie with jsessionid
>> for authentication.
>> and a JSON response { status : 'Success' }
>>
>> If you are looking into a specific client/language let me know I will
>> try to create an example code for authentication and successful call.
>>
>> Udai
>> On Sun, Nov 13, 2011 at 6:00 PM, Satish <www.s...@gmail.com> wrote:
>> > Hi Udai/Binny,
>> > I have been following up recent development on the REST front in Mifos
>> > and
>> > trying to build a external client which will consume the web services.
>> > I have been trying this from a spring app ( I did try to follow up
>> > Udai's
>> > python client too) but couldnt  successfully authenticate mifos. As I
>> > understand, i will have to authenticate and then maintain cookie data
>> > for
>> >  following request, however It is just not working out. May I ask you to
>> > help me with a little code snippet in Java which can let me authenticate
>> > with mifos and save cookie to use for subsequest request?
>> > I tried various ways to create a http request and send it to mifos, but
>> > either it fails with "invalid username/password" (which is misleading)
>> > or
>> > does nothing.
>> > Also, I see in REST API for authentication at
>> >
>> >  https://docs.google.com/document/d/1dZWg7Dw6kGJbpowjHJRCgdX3r9EI3p2K2yCGcC0s6XM/edit?hl=en_US
>> > 1)Authentication
>> > Content-Type:application/json
>> > url:http://localhost:8080/mifoslive/authenticate
>> > method:post
>> > Body:
>> > {
>> >     PersonnelDetails: {
>> >         userName: "support",
>> >         password: "password"
>> >     }
>> > }
>> > Is this really there in place? I couldnt find it. Could any one of you
>> > help
>> > me sort this authentication with mifos please?
>> > Sorry about sending a direct email, My group access is not working yet.
>> > I
>> > will appreciate your help. thanks.
>> > Regards,
>> > Satish
>
>

------------------------------------------------------------------------------
RSA(R) Conference 2012
Save $700 by Nov 18
Register now
http://p.sf.net/sfu/rsa-sfdev2dev1
Mifos-developer mailing list
mifos-d...@lists.sourceforge.net
Unsubscribe or change settings at:
https://lists.sourceforge.net/lists/listinfo/mifos-developer

pramod niralakeri

unread,
Jul 27, 2015, 2:14:08 AM7/27/15
to Mifos Developer, mifos-d...@lists.sourceforge.net, www.s...@gmail.com
can i have sample java code to login into Mifos?

Thanks in advance

Vishwas Babu A J

unread,
Aug 1, 2015, 1:20:20 PM8/1/15
to Mifos software development, Mifos Developer
Hey Pramod,

I’ll assume that you are trying to login to MifosX and not the older Mifos product.

Our API documentation is at https://demo.openmf.org/api-docs/apiLive.htm , the section https://demo.openmf.org/api-docs/apiLive.htm#authentication_overview  gives you an overview of our authentication mechanism along with code examples in javascript.

Given that the current release supports HTTP Basic Auth (to be changes in a  future release to Oauth2) , you should be able to interact with the API’s easily with any language of your choice.

If you want to use Java without any libraries (like httpclient) , sample code to connect to the API would be similar to https://gist.github.com/vishwasbabu/502aa28832274abf6f05

*This code snippet interacts specifically with the document management API’s (you would change the Content-Type headers as appropriate to the other API’s)

Regards,
Vishwas



------------------------------------------------------------------------------

pramod niralakeri

unread,
Aug 1, 2015, 10:50:52 PM8/1/15
to Mifos Developer, mifos-d...@lists.sourceforge.net, www.s...@gmail.com
ThankQ so much!!! cheers


On Wednesday, 16 November 2011 20:00:23 UTC+5:30, Udai Gupta wrote:
Reply all
Reply to author
Forward
0 new messages