Vue前端 php 后端通过cas6.5认证

2 篇文章 0 订阅

Vue前端 php 后端 通过CAS登录

Vue前端:http://localhost:8080/
PHP后端:http://localhost/restful_cas/
CAS服务器地址:https://localhost:8443/cas/login

流程:
1、用户访问vue前端,点击cas登录后跳转到认证页面

if (!this.$cookies.isKey('PHPSESSID') ) {
  //跳转的时候会自动生成一个ssid,这个id是phpcas生成的,不是后台生成的.
  //PHPSESSID 是后台生成的
  window.location.href = "http://localhost/restful_cas/cas_login.php";

  console.log("没有登录");
    }

关键点:跳转到phpcas的客户端会生成PhpCas_SESSID(初始化也会生成),登录成功后访问前端的http://localhost:8080/list,会生成新的PHPSESSID cookie。
如果这个PHPSESSID的话就没有登录,跳转到认证后台。

2、后端cas_login.php,通过phpcas的模板修改得到。
判断是否认证,如果没有认证自动跳转到cas后台认证页面https://localhost:8443/cas/login,
通过认证后,返回到cas_login.php,再跳转到前端的http://localhost:8080/list 页面。http://localhost:8080/list这个页面会请求php后端的,这个php后端生成新的PHPSESSID保存到cookie中。这个cookie就是判断是否登录的cookie。

$auth = phpCAS::forceAuthentication();
if ($auth) {
    $url='http://localhost:8080/list';
    header("Location: $url");
    exit();

} 

3、退出操作
前端点击退出,执行如下操作:

logout() {
this. c o o k i e s . r e m o v e ( ′ P h p C a s S E S S I D ′ ) ; t h i s . cookies.remove('PhpCas_SESSID'); this. cookies.remove(PhpCasSESSID);this.cookies.remove(‘PHPSESSID’);

window.location.href = “http://localhost/restful_cas/cas_login.php?logout=”;

},

1)删除本地cookies
2)跳转到后端cas_login.php上
3)cas_login.php上执行cas退出并返回到登录页面。

if (isset($_REQUEST['logout'])) {
   $param = array("service" => "http://localhost/restful_cas/cas_login.php");
   
    

    phpCAS::logout($param);
}

注意点:
1)以cas_login.php为中心代理相关请求。
2)Vue到cas_login.php因为涉及到跳转302请求,只能用跳转方式,不能用axios方式。
3)认证成功后,跳转到vue前端的首页(比如List页面),不要跳转到vue前端cas_login登录页面,避免循环跳转。
4)vue需要开启发送携带cookie的配置,同时后端php需要开始接收cookie的请求
Vue配置

axios.defaults.withCredentials = true

Vue.use(VueCookies);

Php关键配置

header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: http://localhost:8080");
if (isset($_COOKIE['PhpCas_SESSID'])) {
    try {

        session_id($_COOKIE['PhpCas_SESSID']);
        session_start();

        if (isset($_SESSION['phpCAS'] ['attributes']['username'])) {

}}}

总流程内容

在这里插入图片描述

CasloginView.vue

<template>
  <div class="cas_login">
    
    您已登录,欢迎用户{{ this.$store.state.userName }}登录使用。
  </div>
</template>
<script>
export default {
  data() {
    return {
    };
  },
  mounted() {

    console.log("登录状态:"+this.$cookies.isKey('PHPSESSID'));
    if (!this.$cookies.isKey('PHPSESSID') ) {
      //跳转的时候会自动生成一个ssid,这个id是phpcas生成的,不是后台生成的.
      //PHPSESSID 是后台生成的
      window.location.href = "http://localhost/restful_cas/cas_login.php";

      console.log("没有登录");
    }


      console.log("已经登录");


  
    




    
  },
};
</script>

cas_login.php代码

<?php
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: http://localhost:8080");

header("Access-Control-Allow-Headers: access");
header("Access-Control-Allow-Methods: POST");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");




// Load the settings from the central config file
require_once './phpCAS/config.php';
// Load the CAS lib
require_once  './phpCAS/CAS.php';



// Initialize phpCAS
phpCAS::client(CAS_VERSION_2_0, $cas_host, $cas_port, $cas_context);


phpCAS::setNoCasServerValidation();


if (isset($_REQUEST['logout'])) {
   $param = array("service" => "http://localhost/restful_cas/cas_login.php");
   
    

	phpCAS::logout($param);
}




$auth = phpCAS::forceAuthentication();




if ($auth) {

    $user_name = phpCAS::getUser();
  
    $attr = phpCAS::getAttributes();



  
    $url='http://localhost:8080/list';
    header("Location: $url");
    exit();


    

} else {

   
    
}



?>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值