码迷,mamicode.com
首页 > Web开发 > 详细

php实现SSO单点登录实例

时间:2019-02-19 17:42:52      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:order   gif   一个   alt   form   img   oca   ext   授权   

1、点击登录跳转到SSO登录页面并带上当前应用的callback地址
2、登录成功后生成COOKIE并将COOKIE传给callback地址
3、callback地址接收SSO的COOKIE并设置在当前域下再跳回到应用1即完成登录
4、再在应用程序需要登录的地方嵌入一个iframe用来实时检测登录状态

 

技术图片
 1 <?php
 2 //index.php 应用程序页面
 3 header(‘Content-Type:text/html; charset=utf-8‘);
 4 $sso_address      = ‘http://www.c.com/sso_login.php‘; //你SSO所在的域名
 5 $callback_address = ‘http://‘ . $_SERVER[‘HTTP_HOST‘]
 6     . str_replace(‘index.php‘, ‘‘, $_SERVER[‘SCRIPT_NAME‘])
 7     . ‘callback.php‘; //callback地址用于回调设置cookie
 8 
 9 if (isset($_COOKIE[‘sign‘])) {
10     exit("欢迎您{$_COOKIE[‘sign‘]} <a href=\"login.php?logout\">退出</a>");
11 } else {
12     echo ‘您还未登录 <a href="‘ . $sso_address . ‘?callback=‘ . $callback_address . ‘">点此登录</a>‘;
13 }
14 ?>
15 <iframe src="<?php echo $sso_address ?>?callback=<?php echo $callback_address ?>" frameborder="0" width="0"
16         height="0"></iframe>
17 
18 <?php
19 //callback.php 回调页面用来设置跨域COOKIE
20 header(‘Content-Type:text/html; charset=utf-8‘);
21 if (empty($_GET)) {
22     exit(‘您还未登录‘);
23 } else {
24     foreach ($_GET as $key => $val) {
25         setcookie($key, $val, 0, ‘‘);
26     }
27     header("location:index.php");
28 }
29 ?>
30 
31 <?php
32 //connect.php 用来检测登录状态的页面,内嵌在页面的iframe中
33 header(‘Content-Type:text/html; charset=utf-8‘);
34 if (isset($_COOKIE[‘sign‘])) {
35     $callback = urldecode($_GET[‘callback‘]);
36     unset($_GET[‘callback‘]);
37     $query    = http_build_query($_COOKIE);
38     $callback = $callback . "?{$query}";
39 } else {
40     exit;
41 }
42 ?>
43 <html>
44 <script type="text/javascript">top.location.href = "<?php echo $callback; ?>";</script>
45 </html>
46 
47 
48 <?php
49 
50 //login.php SSO登录页面
51 header(‘Content-Type:text/html; charset=utf-8‘);
52 if (isset($_GET[‘logout‘])) {
53     setcookie(‘sign‘, ‘‘, -300);
54     unset($_GET[‘logout‘]);
55     header(‘location:index.php‘);
56 }
57 
58 if (isset($_POST[‘username‘]) && isset($_POST[‘password‘])) {
59     setcookie(‘sign‘, $_POST[‘username‘], 0, ‘‘);
60     header("location:" . $_POST[‘callback‘] . "?sign={$_POST[‘username‘]}");
61 }
62 
63 if (empty($_COOKIE[‘sign‘])) {
64     ?>
65 
66     <form method="post">
67         <p>用户名:<input type="text" name="username"/></p>
68         <p>密 码:<input type="password" name="password"/></p>
69         <input type="hidden" name="callback" value="<?php echo $_GET[‘callback‘]; ?>"/>
70         <input type="submit" value="登录"/>
71     </form>
72 
73 
74     <?php
75 } else {
76     $query = http_build_query($_COOKIE);
77     echo "系统检测到您已登录 {$_COOKIE[‘sign‘]} <a href=\"{$_GET[‘callback‘]}?{$query}\">授权</a> <a href=\"?logout\">退出</a>";
78 }
技术图片

 

done!

php实现SSO单点登录实例

标签:order   gif   一个   alt   form   img   oca   ext   授权   

原文地址:https://www.cnblogs.com/liliuguang/p/10402497.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!