跨服務(wù)器登錄驗證(單點登錄SSO)的過程和Java實現(xiàn)_第1頁
跨服務(wù)器登錄驗證(單點登錄SSO)的過程和Java實現(xiàn)_第2頁
跨服務(wù)器登錄驗證(單點登錄SSO)的過程和Java實現(xiàn)_第3頁
跨服務(wù)器登錄驗證(單點登錄SSO)的過程和Java實現(xiàn)_第4頁
跨服務(wù)器登錄驗證(單點登錄SSO)的過程和Java實現(xiàn)_第5頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

1、跨服務(wù)器登錄驗證(單點登錄SSO的過 程和Java實現(xiàn)收藏如果我們的網(wǎng)站需要和另一個域名做統(tǒng)一認(rèn)證,也就是在我們網(wǎng)站登錄,但真正 的功能卻在另一個網(wǎng)站來提供。許多都以passport的方式。整個認(rèn)證可以分三步完成 第一步:本地驗證這個很簡單,輸入本地的用戶名和密碼,然后服務(wù)器認(rèn)證通過,并返回正確的Cookie;第二步:做遠程認(rèn)證,并返回遠程連接通過本地Cookie,確認(rèn)用戶合法性,然后服務(wù)器端調(diào)用遠程的登錄程序,返回一個遠程認(rèn)證號的URL這個URL里面包含了一個唯一的認(rèn)證碼,使用Location的方式第三步:遠程登錄客戶端使用前一步的URL,訪問遠程的服務(wù)器,服務(wù)器確認(rèn)認(rèn)證碼的正確性,再返回正

2、確的遠程Cookie.至此,本地認(rèn)證,通過一個 URL,實現(xiàn)了遠程認(rèn)證。以下是示范代碼,是Verycd的武林英雄的認(rèn)證過程,其登錄服務(wù)是verycd提供 的,而真實服務(wù)時9wee提供的package org.javare n.hero;import java.io.BufferedReader;import java.io .In putStream;import java.i o.ln putStreamReader;import java.io.OutputStream;import java .n et.Socket;import java .n et.URLE ncoder;impor

3、t java.util.Sca nner;/*跨服務(wù)器登錄驗證(單點登錄SSO的過程和Java實現(xiàn)* author JAVA 世紀(jì)網(wǎng)(, )*/public class Logi n public static String getCookie() System.out.print(”輸入用戶名:");Scanner in = new Scann er(System.i n);Stri ng user name = in.n ext();System.out.print("輸入密碼:");Stri ng password = in.n ext();try / 本地

4、認(rèn)證String cookie = postLog in( user name, password);if (cookie = n ull) return n ull;/服務(wù)器遠程認(rèn)證String url = getLog in 2(cookie);if (url = n ull) return n ull;/遠程訪問認(rèn)證String cookie2 =getLog in 3(url.substri ng(”" .len gth();/ System.out.pri ntl n( cookie2);retur n cookie2; catch (Excepti on ex) ex.p

5、ri ntStackTrace();return nu II;private static String postLogi n( Stri nguser name, String password) throwsExcepti on Socket socket = new Socket("", 80);try Stri ngBuilder sb = new Stri ngBuilder();sb.appe nd("POST /sig nin? f=out HTTP/1.1rn");sb.appe nd("Host: r n");sb.

6、appe nd("User-Age nt: Mozilla/5.0 (Wi ndows; U; Win dows NT 5.1;zh-CN; rv:) Gecko/20090824 Firefox/3.5.3rn");sb.appe nd("Accept:text/html,applicati on /xhtml+xml,applicatio n/xml;q=0.9,*/*;q=0.8rn");sb.appe nd("Accept-La nguage: zh-c n, zh;q=0.5r n");sb.appe nd(&

7、quot;Accept-E ncodi ng: gzip,deflater n");sb.append("Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7rn");sb.appe nd("Referer: n”);sb.appe nd("C onten t-Type: application/x-www-form-urlencodedrn");String data = "ru=http%3A%2F%2F%2F3rdServices%2F50hero&login_submi t=%

8、E7%99%BB%E5%BD%95&username="+ URLEncoder.encode(username, "UTF-8") + "&password=" + URLEncoder.encode(password, "UTF-8");sb.append("Content-Length: " + data.getBytes("UTF-8").length + "rn");sb.append("rn");sb.append(d

9、ata);OutputStream os = socket.getOutputStream();os.write(sb.toString().getBytes("UTF-8");os.flush();InputStream is = socket.getInputStream();BufferedReader reader = new BufferedReader(newInputStreamReader(is, "UTF-8"); / 讀取結(jié)果String line;StringBuilder cookieSb = new StringBuilder(

10、);int index;while (line = reader.readLine() != null) if (line.startsWith("Set-Cookie:") if (!line.contains("=deleted;") index = line.indexOf("");if (index > 12) cookieSb.append(line.substring(12, index + 1); else if (line.startsWith("location:") if (line.co

11、ntains("error_code") return null;is.close();reader.close();return cookieSb.toString(); finally socket.close();private static String getLogin2(String cookie) throws Exception Socket socket = new Socket("", 80);try StringBuilder sb = new StringBuilder();sb.appe nd("GET/sig nin

12、 ?ak=50hero&ru=http%3A%2F%2F%2Fpassport.php HTTP/1.1rn");sb.appe nd("Host: r n");sb.appe nd("User-Age nt: Mozilla/5.0 (Wi ndows; U; Win dows NT 5.1;zh-CN; rv:) Gecko/20090824 Firefox/3.5.3rn");sb.appe nd("Accept:text/html,applicati on /xhtml+xml,applicatio n/

13、xml;q=0.9,*/*;q=0.8rn");sb.appe nd("Accept-La nguage: zh-c n, zh;q=0.5r n");sb.appe nd("Accept-E ncodi ng: gzip,deflater n");sb.append("Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7rn");sb.appe nd("Referer: n”);sb.ap pe nd("Cookie: " + cookie + "rn

14、");sb.appe nd("r n");OutputStream os = socket.getOutputStream();os.write(sb.toStri ng().getBytes("UTF-8");os.flush();In putStream is = socket.getI nputStream();BufferedReader reader = new BufferedReader( newIn putStreamReader(is, "UTF-8"); /讀取結(jié)果Stri ng line;while (

15、line = reader.readLine() != null) if (li ne.startsWith("locatio n:") retur n lin e.substri ng(10);is.close();reader.close();return nu II; fin ally socket.close();private static String getLogin3(String url) throws Exception Socket socket = new Socket("", 80);try Stri ngBuilder sb

16、= new Stri ngBuilder();sb.appe nd("GET " + url + " HTTP/1.1rn");sb.appe nd("Host: r n");sb.appe nd("User-Age nt: Mozilla/5.0 (Wi ndows; U; Win dows NT 5.1;zh-CN; rv:) Gecko/20090824 Firefox/3.5.3rn");sb.appe nd("Accept:text/html,applicati on /xhtml

17、+xml,applicatio n/xml;q=0.9,*/*;q=0.8r n");sb.appe nd("Accept-La nguage: zh-c n, zh;q=0.5r n");sb.appe nd("Accept-E ncodi ng: gzip,deflater n");sb.append("Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7rn");sb.appe nd("Referer: n”);sb.appe nd("r nrn");Out

18、putstream os = socket.getOutputStream();os.write(sb.toStri ng().getBytes("UTF-8");os.flush();In putStream is = socket.getI nputStream();BufferedReader reader = new BufferedReader( newIn putStreamReader(is, "UTF-8"); /讀取結(jié)果Stri ng line;Stri ngBuilder cookieSb = new Stri ngBuilder();int in dex;while (line = reade

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論