Symfony2防火墙问题Android

时间:2013-07-10 08:01:45

标签: android symfony httpclient symfony-2.1 firewall

让我解释一下我的情况,我为Symfony2.1中的Android应用程序开发了一个完整的后端,它运行得很好,现在我正在尝试创建Android应用程序部分,因为我创建了一个带有http_basic身份验证的防火墙,确保了我的用户经过正确的身份验证和授权,我实际上可以使用我的应用程序并进行记录,但如果我尝试检索防火墙后面的任何页面,则会出现404错误。

我不想使用任何外部捆绑包,我只是想在每个请求上发送我的用户/传递,因为我的应用程序只进行了三次httpclient调用,但我不知道是否可以在每次请求时获得访问权限。< / p>

这是我的代码的一部分,随时问:) 提前谢谢!

我的Android http电话:

@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();
    }

我的防火墙

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

我的控制器

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);
    }
 }

注意:如果我在我的控制器的不同功能中重复相同的步骤,我的Android应用程序出现问题,我不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

这恰好是我的routing.yml中的拼写错误问题,所以如果它对某人有用,我必须说这是正常的。