好的,这里有很多内容需要介绍,还有一些我没有做的工作。但基本思想是,在服务器端(我们使用.Net),我们使用“kentor”创建了一个 SAML 通信层。我没有使用这个,但想法是软件与客户端的身份提供商 (IDP) 进行 SSO 通信(例如 Okta)。 IDP 客户端通常必须提供包含安全信息和最终 URL 的 XML 元数据,然后您向它们提供 SSO xml 元数据(抱歉,我没有参与这部分工作!)。
基本上从这里开始,Android 方面就非常简单了。最重要的是,上述交互会产生 SSO 客户端提供的 URL,您将在 Android 端使用该 URL 创建 Web 视图,这将允许他们输入登录信息进行验证。
我们对 URL 进行了硬编码,因为我们专门为客户创建了白标产品(您将看到它Constants.SINGLE_SIGNON_URL
如下),但在客户传递 SSO 的组织代码后,没有什么可以阻止您将 URL 传回(我们现在正在努力)。换句话说,您存储 URL 或根据客户生成 URL,然后在设备向您传递组织代码时返回该 URL。该 URL 实际上指向您的服务器,该服务器重定向到 SSO 的 IDP (Okta) 登录页面。这是因为 OKTA 的响应需要发送到您的服务器,最终它将通过重定向到您的 Web 视图发回。然后,我们使用 cookie 来存储生成的用户名,以允许正常的登录过程。可能有很多不同的方法可以做到这一点,Okta 甚至提供了本机移动设备功能,但客户必须支持这一点。
这是一个图表,希望能够阐明其中的一些高级部分:
代码只涵盖了上图中的1)、2)和5)。 1)对WebView的调用非常明显。 2)实际上是对Constants.SINGLE_SIGNON_URL
它会访问您的服务器,该服务器应重定向到 IDP 页面。当用户登录时,它会被发送回您的服务 (SP) 并重定向回您的 WebView。同样,我们在 cookie 中存储了一些内容,以便将其拉出以继续正常登录。
关键是要意识到 WebView 的shouldOverrideUrlLoading()
被调用多次。忽略所有这些,除了发送回服务器 URL 的那一个,此时您将提取所需的数据(在我们的示例中是服务器已验证的登录信息)。这是在通话中看到的GlobalState.getInstance().currentUserName = getCookieValue("_username" ,cookies);
可能没有很好地解释这一点(已经过去一个月左右了!)。以下是 SSOActivity 的示例,其中完成了大部分工作:
public class SSOActivity extends Activity {
WebView webView;
private Button mCancel;
private Button mReset;
/**
* Grabs the specified variables out of the list of cookies
*
* @param fieldName
* @param cookies
* @return
*/
public String getCookieValue(String fieldName, final String cookies){
String CookieValue = null;
String[] cookiessplit = cookies.split(";");
for (String str : cookiessplit ) {
if(str.contains(fieldName)) {
String[] value=str.split("=");
CookieValue = value[1];
break;
}
}
return CookieValue;
}
public void clearCookies() {
try {
android.webkit.CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
}
catch (Exception ex)
{
Utilities.logException(ex);
Utilities.logError("SSOActivity", "clearCookies() : " + ex.getMessage() );
}
}
/**
* Cancels the SSO request in Webview
*
* @param view
*/
public void cancelSSOClick (View view) {
Utilities.logInfo("cancelSSOClick", "Cancel SSO click");
setResult(Activity.RESULT_CANCELED, null);
SSOActivity.this.finish();
}
/**
* Resets and deletes cookies and SSOUrl if one exists
*
* @param view
*/
public void resetSSOClick (View view) {
Utilities.logInfo("resetSSOClick", "Cancel SSO click");
setResult(Activity.RESULT_CANCELED, null);
clearCookies();
SSOActivity.this.finish();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setResult(Activity.RESULT_OK, null);
// Setup the web view. It will redirect to SSO site for login
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_sso);
mCancel = (Button)findViewById(R.id.cancelSSO);
mCancel.setTextColor(Color.WHITE);
mReset = (Button)findViewById(R.id.resetSSO);
mReset.setTextColor(Color.WHITE);
webView = (WebView) findViewById(R.id.ssoViewer);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(false);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading (WebView view, String url) {
try {
// If coming from our system, then we need to check the cookie for username password, for
// some SSO this might be different than the base url. Check for both
if (url.equals(Constants.getBaseUrl()) || url.equals(Constants.SSO_RETURN_URL)) {
CookieManager cookieManager = CookieManager.getInstance();
final String cookies = cookieManager.getCookie(url);
GlobalState.getInstance().currentUserName = getCookieValue("_username" ,cookies);
SSOActivity.this.finish();
return true;
}
}
catch (Exception ex) {
GlobalState.getInstance().currentUserName = "";
GlobalState.getInstance().currentPassword = "";
setResult(Activity.RESULT_CANCELED, null);
SSOActivity.this.finish();
}
return false;
}
});
try {
webView.loadUrl(Constants.SINGLE_SIGNON_URL);
}
catch (Exception ex) {
Utilities.logException(ex);
Utilities.logError("SSOActivity", "onCreate(), webView.loadUrl(ssoUrl) : " + ex.getMessage() );
}
}
}
以下是支持 Activity 的 XML 示例:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ssoViewerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/button_layout"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:gravity="center|bottom"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/cancelSSO"
android:layout_marginTop="16dp"
android:layout_width="125dp"
android:layout_height="55dp"
android:layout_margin="5dp"
android:onClick="cancelSSOClick"
android:text="Cancel Login"
android:background="@drawable/button_login" />
<Button
android:id="@+id/resetSSO"
android:layout_marginTop="16dp"
android:layout_width="125dp"
android:layout_height="55dp"
android:layout_margin="5dp"
android:onClick="resetSSOClick"
android:text="Reset SSO"
android:background="@drawable/button_login"/>
</LinearLayout>
<WebView
android:id="@+id/ssoViewer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/button_layout" />
</RelativeLayout>
在代码中的其他位置调用它如下所示:
Intent viewIntent = new Intent(getActivity(), SSOActivity.class);
(getActivity()).startActivityForResult(viewIntent, Constants.SINGLE_SIGN_ON);
最后你应该看到什么:
希望这可以帮助!