【发布时间】:2014-03-29 11:24:56
【问题描述】:
我有一个 PHP Symfony Web 应用程序,它使用基于表单的身份验证,现在我尝试在 Android 本机应用程序中访问它的一些数据。首先,我需要从我的 android 本机应用程序进行身份验证,但我无法针对 Symfony 应用程序进行实用的身份验证。
经过大量调试后,我发现当我尝试从 Android 对 Symfony 进行身份验证时,它似乎总是重定向到我能够通过 Fiddler 代理捕获的登录页面。我还尝试更改 security.yml 以使用基本身份验证移动设备,但似乎没有任何效果。
下面是我的security.yml
security:
acl:
connection: default
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
encoders:
Docova\DocovaBundle\Entity\UserAccounts: plaintext
providers:
chain_provider:
chain:
providers: [docova_second, docova_main]
docova_main:
id: docova.security.user.provider
docova_second:
entity: { class: DocovaBundle:UserAccounts}
firewalls:
login:
pattern: ^/demo/secured/login$
security: false
docova:
pattern: /.*
form_login:
login_path: %d.login_path%
check_path: %d.check_path%
default_target_path: %d.default_target_path%
success_handler: docova.security.authentication.success_handler
logout:
path: /Docova/logout
target: /Docova
anonymous: true
security: true
docova: true
mobile:
pattern: /.*
http_basic: ~
access_control:
- { path: /Docova/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/Docova, roles: ROLE_USER }
这是android使用basic进行身份验证的java代码:
/*
* Get input stream from requested url
*/
public InputStream getInputStream(String urlPath){
InputStream is=null;
HttpURLConnection httpConn = null;
FileOutputStream fos=null;
try {
int responseCode;
String responseContentType;
urlPath = "http://linux.dlitools.com/Symfony/web/app.php/Docova/mobileAuthenticateUser.xml";
URL url = new URL(urlPath);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("GET");
httpConn.setDoInput(true);
//prepare login string for basic auth
String authString = username + ":" + password;
// encode base 64 for basic auth
String encoded=Base64.encodeToString(authString.getBytes(), Base64.NO_WRAP);
//set required properties for basic
httpConn.setRequestProperty("Authorization", "Basic " + encoded);
httpConn.setDoOutput(true);
httpConn.setRequestProperty("Content-type", "text/xml");
//open connection and get input stream
httpConn.connect();
is = httpConn.getInputStream();
int lenghtOfFile = httpConn.getContentLength();
responseContentType=httpConn.getContentType(); //***** need to find out more on this ******
responseCode= httpConn.getResponseCode();
if (!responseContentType.equals("text/xml") ){
is=null;
}
Log.d(TAG, "The response Content Type is: " + responseContentType);
Log.d(TAG, "The response code is: " + responseCode);
Log.d(TAG, "getXmlInputStream() : END");
}catch(Exception e){
errorMsg=errorMsg+"\n\n Exception happend: "+" " + e.toString() ;
}
return is;
}
如果有人能指出我正确的方向,我将不胜感激......是否需要在 Symfony 安全性或某些配置中打开一个设置,以允许对 Android 等移动应用程序进行基本身份验证。
谢谢。
【问题讨论】:
-
您正在使用 2 个防火墙,一个用于基本身份验证,一个用于表单登录。通过基本身份验证后,您还需要通过表单登录。使用 2 个防火墙时需要小心,因为在进行身份验证时都会检查它们。
-
我有 2 个防火墙的原因是因为 sympfony 出于某种原因不允许以编程方式登录表单,所以我添加了基本身份验证只是为了看看它是否可以工作。顺便说一句,我尝试发送基本授权和帖子,但也没有用。
-
最好的办法是实现自定义身份验证提供程序或类似 FOSOAuthServerBundle 的东西。这样您就可以使用相同的防火墙以编程方式登录
-
谢谢建议。您能否发布自定义身份验证提供程序的示例,thnx。
标签: php android symfony symfony1