Search code examples
androidsymfonyfirewallsymfony-2.1androidhttpclient

Symfony2 firewall issue Android


Let me explain my situation, I developed a complete backend for an Android application in Symfony2.1 that works perfectly, now I'm trying to create the Android app part, for that I created a firewall with http_basic authentication that ensures that my users are correctly authenticated and authorized, I actually can use my app and be logged, but if I try to retrieve any page behind the firewall a get a 404 error.

I don't want to use any external bundle, I just want to send my user/pass on every request since my app makes just three httpclient calls but I don't know hoy to get access granted on every request.

Here is part of my code, feel free to ask :) Thanks in advance!

My Android http call:

@Override protected String doInBackground(Void... arg0) {

        HttpClient httpClient = new DefaultHttpClient();

        // construir peticion get
        HttpGet httpGet = new HttpGet("http://www.somewebsite.com/api/login/");

        httpGet.addHeader(BasicScheme.authenticate(
                new UsernamePasswordCredentials(loguser, passw), "UTF-8",
                false));

         try {
            response = httpClient.execute(httpGet);

            reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
            builder = new StringBuilder();
            for(String line = null; (line = reader.readLine()) != null;){
                builder.append(line).append("\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
            alert("Error de protocolo", "Lo sentimos, ha ocurrido un error");
        }

        return builder.toString();
    }

My firewall

api:  
        pattern: ^/api/.*  
        provider: app_user
        http_basic:  
            realm: "API"

access_control:
    - { path: ^/api-registro/, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/api/.*, role: ROLE_API }
providers:
    app_user:
        entity: { class: Alood\BackBundle\Entity\Usuario, property: user }
encoders:
    Alood\BackBundle\Entity\Usuario: plaintext

My Controller

public function apiLoginAction()  
 {  
    $peticion = $this->getRequest();
    $sesion = $peticion->getSession();
    $usuario = $this->get('security.context')->getToken()->getUser();
    $error = $peticion->attributes->get(SecurityContext::AUTHENTICATION_ERROR, 
    $sesion->get(SecurityContext::AUTHENTICATION_ERROR));

    $securityContext = $this->container->get('security.context');
    if( $securityContext->isGranted('IS_AUTHENTICATED_FULLY')){
        $texto["user"] = $usuario->getUser();

        return new JsonResponse($texto);
    }
 }

Note: If I repeat the same steps in a different function of my controller I get a problem in my android app and I don't know how to solve this.


Solution

  • It happened to be a typo issue in my routing.yml so if its useful for somebody I must say this works properly.