php实现SSO单点登录实例

2020-04-24 1050 阅读 0评论

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

<?php

//index.php 应用程序页面
header('Content-Type:text/html; charset=utf-8');
$sso_address      = 'http://www.c.com/sso_login.php'; //你SSO所在的域名
$callback_address = 'http://' . $_SERVER['HTTP_HOST']
    . str_replace('index.php', '', $_SERVER['SCRIPT_NAME'])
    . 'callback.php'; //callback地址用于回调设置cookie

if (isset($_COOKIE['sign'])) {
    exit("欢迎您{$_COOKIE['sign']} <a href=\"login.php?logout\">退出</a>");
} else {
    echo '您还未登录 <a href="' . $sso_address . '?callback=' . $callback_address . '">点此登录</a>';
}
?>
<iframe src="<?php echo $sso_address ?>?callback=<?php echo $callback_address ?>" frameborder="0" width="0"
        height="0"></iframe>

<?php
//callback.php 回调页面用来设置跨域COOKIE
header('Content-Type:text/html; charset=utf-8');
if (empty($_GET)) {
    exit('您还未登录');
} else {
    foreach ($_GET as $key => $val) {
        setcookie($key, $val, 0, '');
    }
    header("location:index.php");
}
?>

<?php
//connect.php 用来检测登录状态的页面,内嵌在页面的iframe中
header('Content-Type:text/html; charset=utf-8');
if (isset($_COOKIE['sign'])) {
    $callback = urldecode($_GET['callback']);
    unset($_GET['callback']);
    $query    = http_build_query($_COOKIE);
    $callback = $callback . "?{$query}";
} else {
    exit;
}
?>
<html>
<script type="text/javascript">top.location.href = "<?php echo $callback; ?>";</script>
</html>


<?php

//login.php SSO登录页面
header('Content-Type:text/html; charset=utf-8');
if (isset($_GET['logout'])) {
    setcookie('sign', '', -300);
    unset($_GET['logout']);
    header('location:index.php');
}

if (isset($_POST['username']) && isset($_POST['password'])) {
    setcookie('sign', $_POST['username'], 0, '');
    header("location:" . $_POST['callback'] . "?sign={$_POST['username']}");
}

if (empty($_COOKIE['sign'])) {
    ?>

    <form method="post">
        <p>用户名:<input type="text" name="username"/></p>
        <p>密 码:<input type="password" name="password"/></p>
        <input type="hidden" name="callback" value="<?php echo $_GET['callback']; ?>"/>
        <input type="submit" value="登录"/>
    </form>


    <?php
} else {
    $query = http_build_query($_COOKIE);
    echo "系统检测到您已登录 {$_COOKIE['sign']} <a href=\"{$_GET['callback']}?{$query}\">授权</a> <a href=\"?logout\">退出</a>";
}
喜欢就支持以下吧
点赞 0

发表评论

快捷回复: 表情:
aoman baiyan bishi bizui cahan ciya dabing daku deyi doge fadai fanu fendou ganga guzhang haixiu hanxiao zuohengheng zhuakuang zhouma zhemo zhayanjian zaijian yun youhengheng yiwen yinxian xu xieyanxiao xiaoku xiaojiujie xia wunai wozuimei weixiao weiqu tuosai tu touxiao tiaopi shui se saorao qiudale qinqin qiaoda piezui penxue nanguo liulei liuhan lenghan leiben kun kuaikule ku koubi kelian keai jingya jingxi jingkong jie huaixiao haqian aini OK qiang quantou shengli woshou gouyin baoquan aixin bangbangtang xiaoyanger xigua hexie pijiu lanqiu juhua hecai haobang caidao baojin chi dan kulou shuai shouqiang yangtuo youling
提交
评论列表 (有 0 条评论, 1050人围观)