關於單點登入第一天接觸之我見

來源:互聯網
上載者:User

標籤:mon   ref   多個   請求   baidu   ase   row   als   null   

前幾天老大要搭好了項目架構,然後沒要我繼續跟進,而是做單點登入。我操,跨系統。有點緊張,禮拜天看了看單點登入。

理論梳理:

單點登入(Single Sign On),簡稱為 SSO,是目前比較流行的企業業務整合的解決方案之一。SSO的定義是在多個應用系統中,使用者只需要登入一次就可以訪問所有相互信任的應用系統。

當使用者第一次訪問應用系統的時候,因為還沒有登入,會被引導到認證系統中進行登入;根據使用者提供的登入資訊,認證系統進行身份校正,如果通過校正,應該返回給使用者一個認證的憑據--ticket;使用者再訪問別的應用的時候,就會將這個ticket帶上,作為自己認證的憑據,應用系統接受到請求之後會把ticket送到認證系統進行校正,檢查ticket的合法性。如果通過校正,使用者就可以在不用再次登入的情況下訪問應用系統2和應用系統3了。

目前相對來講,實在原來項目的基礎上加了一個加密層,就是把目前使用者名以一種加密的方式傳輸給另一個系統,另一個系統接收到加密串對他進行解析,

解析後如果匹配正確,直接登入(因為當時非常糾結為什麼加密傳輸過程中不把密碼一起加密傳輸過去,讓對方解密後取匹配?而是只給了使用者名稱,我嘗試過傳輸非使用者的假名字,

會報錯:解析失敗),如果使用者名稱匹配失敗則無法登陸。

在這裡主要將機密過程做一展示:

package com.zx.ps.aes;import java.net.URLEncoder;import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec;import org.apache.commons.codec.binary.Base64;/** * @author : ckx * @date : 2016-12-5 下午9:32:52 */ public class AESUtil {     // 加密    public static String Encrypt(String sSrc, String sKey) throws Exception {        if (sKey == null) {            System.out.print("Key為空白null");            return null;        }        // 判斷Key是否為16位        if (sKey.length() != 16) {            System.out.print("Key長度不是16位");            return null;        }        byte[] raw = sKey.getBytes("utf-8");        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"演算法/模式/補碼方式"        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));        return new Base64().encodeToString(encrypted);//此處使用BASE64做轉碼功能,同時能起到2次加密的作用。    }     // 解密    public static String Decrypt(String sSrc, String sKey) throws Exception {        try {            // 判斷Key是否正確            if (sKey == null) {                System.out.print("Key為空白null");                return null;            }            // 判斷Key是否為16位            if (sKey.length() != 16) {                System.out.print("Key長度不是16位");                return null;            }            byte[] raw = sKey.getBytes("utf-8");            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");            cipher.init(Cipher.DECRYPT_MODE, skeySpec);            byte[] encrypted1 = new Base64().decode(sSrc);//先用base64解密            try {                byte[] original = cipher.doFinal(encrypted1);                String originalString = new String(original,"utf-8");                return originalString;            } catch (Exception e) {                System.out.println(e.toString());                return null;            }        } catch (Exception ex) {            System.out.println(ex.toString());            return null;        }    }     public static void main(String[] args) throws Exception {        /*         * 此處使用AES-128-ECB加密模式,key需要為16位。         */       // String cKey = "abcdefg123456789";        // 需要加密的字串       // String cSrc = "www.FDSA.soEEEEE";        String cKey = "1234567891234567";        String cSrc = "20161201121212szzx";        String url=URLEncoder.encode("DTB90z5MWjtt7ojCU+zvJbqnuke2KPAmffGPW2q8kFA=","UTF-8");         System.out.println(url);        System.out.println(cSrc);        // 加密        String enString = AESUtil.Encrypt(cSrc, cKey);        System.out.println("加密後的字串是:" + enString);        // 解密        String DeString = AESUtil.Decrypt(enString, cKey);        System.out.println("解密後的字串是:" + DeString);    }}

在頁面中本系統對另一系統的跳轉超連結:

對使用者進行加密,並把加密串放入超連結中:

String cKey = "1234567891234567";            String cSrc = "20161205193612zhongjunhua";            String ticket=AESUtil.Encrypt(cSrc, cKey);//調用加密方法對使用者加密            System.err.println("###########"+ticket);            dmp.put("zxssoticket", ticket);

如此便可以實現簡單的單點登入。

 

關於單點登入第一天接觸之我見

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.