1. 程式人生 > >CAS不使用CAS伺服器登入介面(適用於前後臺完全分離的專案)

CAS不使用CAS伺服器登入介面(適用於前後臺完全分離的專案)

本文主要介紹如何使用各CAS-Client的登入介面,而不統一使用CAS伺服器的登入介面。
原理:很簡單,兩次請求,第一次請求CAS伺服器,拿到登入介面,通過JS解析execution,然後往Cas伺服器傳送登入資訊即可。CAS-Server使用4.2.7.

CAS不使用CAS伺服器登入介面只需要在各自的客戶端通過js解析登入票據就好
下面上程式碼
function login() {
    mui.ajax({
        url: 'https://localhost:8443/cas/login',
        type: 'GET',
        data: {
            Name: "keyun"
}, dataType: 'html', timeout: 1000, async: false, error: function() { alert('Error'); }, success: function(result) { //如果使用者本來就是登入著的,可以選擇讓使用者直接跳轉,還有個更妥當方法是登出使用者,然後再掉login重新獲取 if(result.indexOf("登入成功") != -1) { // window.location.href="http://192,168.2.118/test";
mui.toast('登入成功'); plus.storage.setItem("havelogin", "true"); plus.storage.setItem("userinfo", JSON.stringify("text")); //tourl('../../areas/Tab/Bar/Bar.html', 'Bar'); mui.openWindow('../../areas/Tab/Bar/Bar.html', 'Bar'); //logout();
return; } /* var ltsl=result.indexOf("\"LT"); //擷取LT位置 var lt=result.substring(ltsl+1); ltlt=lt.substring(0,lt.indexOf("\"")); */ var evsl = result.indexOf("execution"); //擷取execution位置 var ev = result.substring(evsl + 1 + 18); //alert(ev.indexOf("\"")); evev = ev.substring(1, ev.indexOf("\"")); alert(evev); post(evev); } }); } function logout() { mui.ajax({ url: 'https://localhost:8443/cas/logout', type: 'GET', dataType: 'html', timeout: 1000, async: false, error: function() {}, success: function(result) { login(); } }); } //提交請求 function post(ev) { mui.ajax({ url: 'https://localhost:8443/cas/login', type: 'POST', async: false, data: { "execution": ev, "password": "cj", "submit": "LOGIN", "username": "cj", "_eventId": "submit" }, //dataType: 'json', timeout: 1000, error: function(r) { //debugger; alert('Error'); }, success: function(result) { //如果登陸成功,則跳轉,否則提示錯誤 if(result.indexOf("登入成功") != -1) { //alert('ok lt:'+lt); mui.toast('登入成功'); /*mui.openWindow("https://localhost:8443/cas/login");*/ plus.storage.setItem("havelogin", "true"); plus.storage.setItem("userinfo", JSON.stringify("text")); //tourl('../../areas/Tab/Bar/Bar.html', 'Bar'); mui.openWindow('../../areas/Tab/Bar/Bar.html', 'Bar'); } } }); }