网站导航网学 原创论文 原创专题 网站设计 最新系统 原创论文 论文降重 发表论文 论文发表 UI设计定制 论文答辩PPT格式排版 期刊发表 论文专题
返回网学首页
网学原创论文
最新论文 推荐专题 热门论文 论文专题
当前位置: 网学 > 交易代码 > Java精品代码 > 正文

java单点登录

论文降重修改服务、格式排版等 获取论文 论文降重及排版 论文发表 相关服务
跨服务器登录验证(单点登录SSO)的过程和Java实现 收藏
如果我们的网站需要和另一个域名做统一认证,也就是在我们网站登录,但真正的功能却在另一个网站来提供。许多都以 passport 的方式。
整个认证可以分三步完成
第一步:本地验证
这个很简单,输入本地的用户名和密码,然后服务器认证通过,并返回正确的Cookie;
第二步:做远程认证,并返回远程连接
通过本地Cookie,确认用户合法性,然后服务器端调用远程的登录程序,返回一个远程认证号的URL,这个URL里面包含了一个唯一的认证码,使用Location的方式
第三步:远程登录
客户端使用前一步的URL,访问远程的服务器,服务器确认认证码的正确性,再返回正确的远程Cookie.
至此,本地认证,通过一个URL,实现了远程认证。
以下是示范代码,是Verycd的武林英雄的认证过程,其登录服务是verycd提供的,而真实服务时9wee提供的
package org.javaren.hero;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.URLEncoder;
import java.util.Scanner;
/*** 跨服务器登录验证(单点登录SSO)的过程和Java实现* * @author JAVA世纪网(java2000.net, laozizhu.com)*/
public class Login {
public static String getCookie() {   
System.out.print("输入用户名:");   
Scanner in = new Scanner(System.in);   
String username = in.next();   
System.out.print("输入密码:");   
String password = in.next();   
try {     
// 本地认证     
String cookie = postLogin(username, password);     
if (cookie == null) {       
return null;     
}     
// 服务器远程认证     
String url = getLogin2(cookie);     
if (url == null) {       
return null;     
}     
// 远程访问认证     
String cookie2 = getLogin3(url.substring("http://s4.verycd.9wee.com".length()));     
// System.out.println(cookie2);     
return cookie2;   
} catch (Exception ex)
{      ex.printStackTrace();     
return null;    }}
private static String postLogin(String username, String password) throws Exception {   
Socket socket = new Socket("secure.verycd.com", 80);   
try {     
StringBuilder sb = new StringBuilder();     
sb.append("POST /signin?f=out HTTP/1.1\r\n");     
sb.append("Host: secure.verycd.com\r\n");     
sb.append("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3\r\n");     
sb.append("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");     
sb.append("Accept-Language: zh-cn,zh;q=0.5\r\n");     
sb.append("Accept-Encoding: gzip,deflate\r\n");     
sb.append("Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7\r\n");      sb.append("Referer: http://secure.verycd.com/3rdServices/50hero\r\n");     
sb.append("Content-Type: application/x-www-form-urlencoded\r\n");     
String data = "ru=http%3A%2F%2Fsecure.verycd.com%2F3rdServices%2F50hero&login_submit=%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 + "\r\n");     
sb.append("\r\n");     
sb.append(data);     
OutputStream os = socket.getOutputStream();     
os.write(sb.toString().getBytes("UTF-8"));     
os.flush();     
InputStream is = socket.getInputStream();     
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
// 读取结果     
String line;     
StringBuilder cookieSb = new StringBuilder();     
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.contains("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("secure.verycd.com", 80);   
try {     
StringBuilder sb = new StringBuilder();     
sb.append("GET /signin?ak=50hero&ru=http%3A%2F%2Fs4.verycd.9wee.com%2Fpassport.php HTTP/1.1\r\n");     
sb.append("Host: secure.verycd.com\r\n");     
sb.append("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3\r\n");     
sb.append("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");     
sb.append("Accept-Language: zh-cn,zh;q=0.5\r\n");     
sb.append("Accept-Encoding: gzip,deflate\r\n");     
sb.append("Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7\r\n");      sb.append("Referer: http://secure.verycd.com/3rdServices/50hero\r\n");     
sb.append("Cookie: " + cookie + "\r\n");     
sb.append("\r\n");     
OutputStream os = socket.getOutputStream();     
os.write(sb.toString().getBytes("UTF-8"));     
os.flush();     
InputStream is = socket.getInputStream();     
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
// 读取结果     
String line;     
while ((line = reader.readLine()) != null) {       
if (line.startsWith("location:")) {
          return line.substring(10);       
}     
}     
is.close();     
reader.close();     
return null;   
} finally {     
socket.close();   
}}
private static String getLogin3(String url) throws Exception {    Socket socket = new Socket("s4.verycd.9wee.com", 80);   
try {     
StringBuilder sb = new StringBuilder();     
sb.append("GET " + url + " HTTP/1.1\r\n");     
sb.append("Host: s4.verycd.9wee.com\r\n");     
sb.append("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3\r\n");     
sb.append("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");     
sb.append("Accept-Language: zh-cn,zh;q=0.5\r\n");     
sb.append("Accept-Encoding: gzip,deflate\r\n");     
sb.append("Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7\r\n");      sb.append("Referer: http://secure.verycd.com/3rdServices/50hero\r\n");     
sb.append("\r\n\r\n");     
OutputStream os = socket.getOutputStream();     
os.write(sb.toString().getBytes("UTF-8"));     
os.flush();     
InputStream is = socket.getInputStream();     
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
// 读取结果     
String line;     
StringBuilder cookieSb = new StringBuilder();     
int index;     
while ((line = reader.readLine()) != null) {
        if (line.length() == 0) {
          break;       
}       
if (line.startsWith("Set-Cookie:")) {
          index = line.indexOf(";");         
if (index > 12) {           
cookieSb.append(line.substring(12, index + 1));          }        }      }     
is.close();     
reader.close();     
return cookieSb.toString();   
} finally {     
socket.close();   
}}}

TAG: JavaJAVAjava




点击下载系统:http://www.myeducs.cn/chaxun/index.html?go=java单点登录&aa=%CB%D1%CB%F7%C2%DB%CE%C4
设为首页 | 加入收藏 | 网学首页 | 原创论文 | 计算机原创
版权所有 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
Copyright 2008-2020 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号 常年法律顾问:王律师