Heim  >  Artikel  >  Backend-Entwicklung  >  PHP编程 SSO详细介绍及实例

PHP编程 SSO详细介绍及实例

墨辰丷
墨辰丷Original
2018-05-26 15:27:421571Durchsuche

这篇文章主要介绍了PHP编程 SSO详细介绍及简单实例的相关资料,这里介绍了三种模式跨子域单点登陆、完全跨单点域登陆、站群共享身份认证,需要的朋友可以参考下

PHP SSO详解

SSO有三种模式:①跨子域单点登陆②完全跨单点域登陆③站群共享身份认证

第一种模式很简单,只需要将Cookie的域设置成多个应用的根域即可

第二种方式,也很简单,就是将所以应用的认证地址更换成同一个认证地址,每次查看是否在认证中心登陆,如果登陆了,给调用应用发放一个加密令牌即可

第三种跨域,就是来回跳转来回验证token略有麻烦

配置目录结构

在服务器根目录下,新建三个项目目录:

|–/网站根目录/
|–|–/oa/
|–|–/bbs/
|–|–/blog/

在根目录下新建functions.PHP脚本文件,具体内容如下:

<?php

/**
 * 获取登陆token
 * @param string $url 获取token的地址
 * 2017-01-03T13:08:43+0800
 */
function getToken($url)
{
  $bool = isLogin();
  if ($bool) {
    // 如果登陆了跳转到本站首页
    header(&#39;location: index.php&#39;);
    exit();
  }

  // 否则没有登陆,去另一个站点看是否登陆
  header(&#39;location: &#39;.$url);
}

// 校验令牌是否正确
function yzToken($domain)
{
  $url = isset($_GET[&#39;url&#39;]) ? $_GET[&#39;url&#39;] : &#39;&#39;;
  $username = isset($_GET[&#39;username&#39;]) ? $_GET[&#39;username&#39;] : &#39;&#39;;
  $token = isset($_GET[&#39;token&#39;]) ? $_GET[&#39;token&#39;] : &#39;&#39;;


  if (!empty($username) && !empty($token)) {
    $salt = &#39;taoip&#39;;
    $_token = md5($salt.$username);
    // 校验第三方站点过来时的token是否正确
    if ($_token == $token) {
      // 设置跳转过来的网站的Cookie
      setCook($username, $_token, $domain);
      header(&#39;location: index.php&#39;);
    }
  }

}

// 设置cookie
function setCook($username, $_password, $domain)
{
  // 校验成功,开始登陆
  setcookie(&#39;username&#39;, $username, time()+3600, &#39;/&#39;, $domain);
  setcookie(&#39;token&#39;, $_password, time()+3600, &#39;/&#39;, $domain);
  header(&#39;location: index.php&#39;);
}

// 判断是否登陆
function isLogin()
{
  $username = isset($_COOKIE[&#39;username&#39;]) ? $_COOKIE[&#39;username&#39;] : &#39;&#39;;
  $token = isset($_COOKIE[&#39;token&#39;]) ? $_COOKIE[&#39;token&#39;] : &#39;&#39;;
  $salt = &#39;taoip&#39;;

  $_token = md5($salt.$username);

  if ($token == $_token) {
    return true;
  } else {
    return false;
  }
}

?>

在oa项目目录下,新建index.php和login.php两个脚本文件

编辑index.php文件

<?php

// OA站点

// (1)开启Session会话
session_name(&#39;taoip&#39;);
session_start();
// (2)获取用户名和token进行校验
$username = isset($_COOKIE[&#39;username&#39;]) ? $_COOKIE[&#39;username&#39;] : &#39;&#39;;
$token = isset($_COOKIE[&#39;token&#39;]) ? $_COOKIE[&#39;token&#39;] : &#39;&#39;;

$salt = &#39;taoip&#39;;

$_token = md5($salt.$username);

if ($token != $_token) {
  header(&#39;location: login.php&#39;);
  exit();
}

echo "欢迎{$username}用户,访问OA站点";

?>

编辑login.php文件

<?php

// OA站点登陆系统
require &#39;../functions.php&#39;;

// (2)验证
yzToken(&#39;taoip.cn&#39;);

// (1)判断是否登陆,登陆则跳转首页,未登录则去其他站点获取token
$url = isset($_GET[&#39;url&#39;]) ? $_GET[&#39;url&#39;] : &#39;&#39;;
if (empty($url)) {
  getToken(&#39;http://dengpeng.cc/login.php?url=http://oa.taoip.cn/login.php&#39;);
}

// (1)判断用户是否登陆
$bool = isLogin();
$url = isset($_GET[&#39;url&#39;]) ? $_GET[&#39;url&#39;] : &#39;&#39;;
if ($bool) {
  if (empty($url)) {
    header(&#39;location: index.php&#39;);
  } else {
    $username = isset($_COOKIE[&#39;username&#39;]) ? $_COOKIE[&#39;username&#39;] : &#39;&#39;;
    $token = isset($_COOKIE[&#39;token&#39;]) ? $_COOKIE[&#39;token&#39;] : &#39;&#39;;
    $lurl = $url.&#39;?username=&#39;.$username.&#39;&token=&#39;.$token;
    header(&#39;location: &#39;.$lurl);
  }
}


if (!empty($_POST)) {
  $username = isset($_POST[&#39;username&#39;]) ? $_POST[&#39;username&#39;] : &#39;&#39;;
  $password = isset($_POST[&#39;password&#39;]) ? $_POST[&#39;password&#39;] : &#39;&#39;;

  // 从库中查询用户密码
  @$link = mysql_connect(&#39;localhost&#39;, &#39;root&#39;, &#39;&#39;);
  mysql_query(&#39;use sso&#39;, $link);
  mysql_query(&#39;set names utf8&#39;, $link);
  $sql = "select * from users where username = &#39;".$username."&#39;";
  $user = mysql_fetch_assoc(mysql_query($sql, $link));

  // 校验
  $salt = &#39;taoip&#39;;
  $_password = md5($salt.$username);

  // var_dump($user[&#39;password&#39;] == $_password);
  // print_r($user);exit();

  if ($user[&#39;password&#39;] == $_password) {
    // 校验成功,开始登陆
    setcookie(&#39;username&#39;, $username, time()+3600, &#39;/&#39;, &#39;taoip.cn&#39;);
    setcookie(&#39;token&#39;, $_password, time()+3600, &#39;/&#39;, &#39;taoip.cn&#39;);
    // 如果URL没有值重定向到首页,否则重定向到URL页面
    if (empty($url)) {
      header(&#39;location: index.php&#39;);
    } else {
      header(&#39;location: &#39;.$lurl);
    }
  }
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="generator" content="Sublime Text 3114">
  <meta name="author" content="3@dengpeng.cc">
  <meta name="keywords" content="">
  <meta name="description" content="">
  <title>OA站点登陆系统</title>
</head>
<body>
  <p class="container">
    <h2>oa.taoip.cn站点登陆系统</h2>
    <form action="" method="post">
      <label for="">用户名</label>
      <input type="text" name="username">
      <br>
      <label for="">密码</label>
      <input type="text" name="password">
      <hr>
      <button type="submit">提交</button>
    </form>
  </p>
</body>
</html>

在bbs项目目录下,新建index.php和login.php两个脚本文件

编辑index.php文件

<?php
/**
 * @author DengPeng <3@dengpeng.cc>
 * @since 2017/01/03
 * @copyright copyright (c) 2017 zixue.it GPL
 * @license http://www.zixue.it/
 */

// BBS站点

// (1)开启Session会话
session_name(&#39;taoip&#39;);
session_start();
// (2)获取用户名和token进行校验
$username = isset($_COOKIE[&#39;username&#39;]) ? $_COOKIE[&#39;username&#39;] : &#39;&#39;;
$token = isset($_COOKIE[&#39;token&#39;]) ? $_COOKIE[&#39;token&#39;] : &#39;&#39;;

$salt = &#39;taoip&#39;;

$_token = md5($salt.$username);

if ($token != $_token) {
  header(&#39;location: login.php&#39;);
  exit();
}

echo "欢迎{$username}用户,访问BBS站点";

?>

编辑login.php文件

<?php
/**
 * @author DengPeng <3@dengpeng.cc>
 * @since 2017/01/03
 * @copyright copyright (c) 2017 zixue.it GPL
 * @license http://www.zixue.it/
 */

// BBS站点登陆系统
require &#39;../functions.php&#39;;

// (2)验证
yzToken(&#39;taoip.cn&#39;);

// (1)判断是否登陆,登陆则跳转首页,未登录则去其他站点获取token
$url = isset($_GET[&#39;url&#39;]) ? $_GET[&#39;url&#39;] : &#39;&#39;;
if (empty($url)) {
  getToken(&#39;http://dengpeng.cc/login.php?url=http://bbs.taoip.cn/login.php&#39;);
}

// (1)判断用户是否登陆
$bool = isLogin();
$url = isset($_GET[&#39;url&#39;]) ? $_GET[&#39;url&#39;] : &#39;&#39;;
if ($bool) {
  if (empty($url)) {
    header(&#39;location: index.php&#39;);
  } else {
    $username = isset($_COOKIE[&#39;username&#39;]) ? $_COOKIE[&#39;username&#39;] : &#39;&#39;;
    $token = isset($_COOKIE[&#39;token&#39;]) ? $_COOKIE[&#39;token&#39;] : &#39;&#39;;
    $lurl = $url.&#39;?username=&#39;.$username.&#39;&token=&#39;.$token;
    header(&#39;location: &#39;.$lurl);
  }
}


if (!empty($_POST)) {
  $username = isset($_POST[&#39;username&#39;]) ? $_POST[&#39;username&#39;] : &#39;&#39;;
  $password = isset($_POST[&#39;password&#39;]) ? $_POST[&#39;password&#39;] : &#39;&#39;;

  // 从库中查询用户密码
  @$link = mysql_connect(&#39;localhost&#39;, &#39;root&#39;, &#39;&#39;);
  mysql_query(&#39;use sso&#39;, $link);
  mysql_query(&#39;set names utf8&#39;, $link);
  $sql = "select * from users where username = &#39;".$username."&#39;";
  $user = mysql_fetch_assoc(mysql_query($sql, $link));

  // 校验
  $salt = &#39;taoip&#39;;
  $_password = md5($salt.$username);

  // var_dump($user[&#39;password&#39;] == $_password);
  // print_r($user);exit();

  if ($user[&#39;password&#39;] == $_password) {
    // 校验成功,开始登陆
    setcookie(&#39;username&#39;, $username, time()+3600, &#39;/&#39;, &#39;taoip.cn&#39;);
    setcookie(&#39;token&#39;, $_password, time()+3600, &#39;/&#39;, &#39;taoip.cn&#39;);
    // 如果URL没有值重定向到首页,否则重定向到URL页面
    if (empty($url)) {
      header(&#39;location: index.php&#39;);
    } else {
      header(&#39;location: &#39;.$lurl);
    }
  }
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="generator" content="Sublime Text 3114">
  <meta name="author" content="3@dengpeng.cc">
  <meta name="keywords" content="">
  <meta name="description" content="">
  <title>BBS站点登陆系统</title>
</head>
<body>
  <p class="container">
    <h2>bbs.taoip.cn站点登陆系统</h2>
    <form action="" method="post">
      <label for="">用户名</label>
      <input type="text" name="username">
      <br>
      <label for="">密码</label>
      <input type="text" name="password">
      <hr>
      <button type="submit">提交</button>
    </form>
  </p>
</body>
</html>

在blog项目目录下,新建index.php和login.php两个脚本文件

编辑index.php文件

<?php
/**
 * @author DengPeng <3@dengpeng.cc>
 * @since 2017/01/03
 * @copyright copyright (c) 2017 zixue.it GPL
 * @license http://www.zixue.it/
 */

// blog站点

// (1)开启Session会话
session_name(&#39;taoip&#39;);
session_start();
// (2)获取用户名和token进行校验
$username = isset($_COOKIE[&#39;username&#39;]) ? $_COOKIE[&#39;username&#39;] : &#39;&#39;;
$token = isset($_COOKIE[&#39;token&#39;]) ? $_COOKIE[&#39;token&#39;] : &#39;&#39;;

$salt = &#39;taoip&#39;;

$_token = md5($salt.$username);

if ($token != $_token) {
  header(&#39;location: login.php&#39;);
  exit();
}

echo "欢迎{$username}用户,访问blog站点";

?>

<?php
/**
 * @author DengPeng <3@dengpeng.cc>
 * @since 2017/01/03
 * @copyright copyright (c) 2017 zixue.it GPL
 * @license http://www.zixue.it/
 */

// blog站点

// (1)开启Session会话
session_name(&#39;taoip&#39;);
session_start();
// (2)获取用户名和token进行校验
$username = isset($_COOKIE[&#39;username&#39;]) ? $_COOKIE[&#39;username&#39;] : &#39;&#39;;
$token = isset($_COOKIE[&#39;token&#39;]) ? $_COOKIE[&#39;token&#39;] : &#39;&#39;;

$salt = &#39;taoip&#39;;

$_token = md5($salt.$username);

if ($token != $_token) {
  header(&#39;location: login.php&#39;);
  exit();
}

echo "欢迎{$username}用户,访问blog站点";

?>

编辑login.php文件

<?php
/**
 * @author DengPeng <3@dengpeng.cc>
 * @since 2017/01/03
 * @copyright copyright (c) 2017 zixue.it GPL
 * @license http://www.zixue.it/
 */

// blog站点登陆系统
require &#39;../functions.php&#39;;

// (2)验证
yzToken(&#39;dengpeng.cc&#39;);

// (1)判断是否登陆,登陆则跳转首页,未登录则去其他站点获取token
$url = isset($_GET[&#39;url&#39;]) ? $_GET[&#39;url&#39;] : &#39;&#39;;
if (empty($url)) {
  getToken(&#39;http://oa.taoip.cn/login.php?url=http://dengpeng.cc/login.php&#39;);
}


// (1)判断用户是否登陆
$bool = isLogin();
$url = isset($_GET[&#39;url&#39;]) ? $_GET[&#39;url&#39;] : &#39;&#39;;
if ($bool) {
  if (empty($url)) {
    header(&#39;location: index.php&#39;);
  } else {
    $username = isset($_COOKIE[&#39;username&#39;]) ? $_COOKIE[&#39;username&#39;] : &#39;&#39;;
    $token = isset($_COOKIE[&#39;token&#39;]) ? $_COOKIE[&#39;token&#39;] : &#39;&#39;;
    $lurl = $url.&#39;?username=&#39;.$username.&#39;&token=&#39;.$token;
    header(&#39;location: &#39;.$lurl);
  }
}


// (3)判断用户是否提交数据
if (!empty($_POST)) {
  $username = isset($_POST[&#39;username&#39;]) ? $_POST[&#39;username&#39;] : &#39;&#39;;
  $password = isset($_POST[&#39;password&#39;]) ? $_POST[&#39;password&#39;] : &#39;&#39;;

  // 从库中查询用户密码
  @$link = mysql_connect(&#39;localhost&#39;, &#39;root&#39;, &#39;&#39;);
  mysql_query(&#39;use sso&#39;, $link);
  mysql_query(&#39;set names utf8&#39;, $link);
  $sql = "select * from users where username = &#39;".$username."&#39;";
  $user = mysql_fetch_assoc(mysql_query($sql, $link));

  // 校验
  $salt = &#39;taoip&#39;;
  $_password = md5($salt.$username);

  // var_dump($user[&#39;password&#39;] == $_password);
  // print_r($user);exit();

  if ($user[&#39;password&#39;] == $_password) {
    setCook($username, $_password, &#39;dengpeng.cc&#39;);
    if (empty($url)) {
      header(&#39;location: index.php&#39;);
    } else {
      header(&#39;location: &#39;.$lurl);
    }
  }
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="generator" content="Sublime Text 3114">
  <meta name="author" content="3@dengpeng.cc">
  <meta name="keywords" content="">
  <meta name="description" content="">
  <title>blog站点登陆系统</title>
</head>
<body>
  <p class="container">
    <h2>dengpeng.cc站点登陆系统</h2>
    <form action="" method="post">
      <label for="">用户名</label>
      <input type="text" name="username">
      <br>
      <label for="">密码</label>
      <input type="text" name="password">
      <hr>
      <button type="submit">提交</button>
    </form>
  </p>
</body>
</html>

配置本地虚拟主机

具体配置步骤,我想大家应该都会了,不需要我一一赘述.你只需要按照我给的参照,配置和不同域名对应目录的映射即可.

域名 /项目目录/
oa.taoip.cn /oa/
bbs.taoip.cn /bbs/
dengpeng.cc /blog/

恭喜您,已经完成了一个简单的SSO系统

配置完成后,记得重启Web服务器.然后你只需要访问这三个不同的站点,即可实现一个站点登陆,其他站点不再发送登陆请求.

以上就是本文的全部内容,希望对大家的学习有所帮助。


相关推荐:

PHP单点登录SSO实现方法

实例讲解SSO单点登录原理

php使用两个用户自定义的键名比较函数array_udiff_uassoc()

Das obige ist der detaillierte Inhalt vonPHP编程 SSO详细介绍及实例. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn