Integrating the jasig cas unified authentication system in Laravel5

Source: Internet
Author: User
Tags autoload

Integrating the jasig cas unified authentication system in Laravel5

CAS: CAS (Central Authentication Service) is a good single sign-on Framework for Web applications. Here we will introduce the cas that I just set up on laravel5. Preparations in advance: the available laravel5 project, cas server already exists.

Environment: Linux (Ubuntu)

1. Download the source code of phpcas.

Create the library directory under the project app directory of laravel5 and download the phpcas library. git clone https://github.com/jasig/phpcas.git. clonethe file directory is a phpcasfile.

2. Create a provider

Create the cas directory under the app and create CasAuthProvider. php with the following content:

<? Php

Namespace cas;

Use Illuminate \ Contracts \ Auth \ UserProvider;
Use Illuminate \ Contracts \ Hashing \ Hasher;
Use Illuminate \ Contracts \ Auth \ Authenticatable;
Use Illuminate \ Auth \ GenericUser;

Class CasAuthProvider implements UserProvider {

/**
* Retrieve a user by their unique identifier.
*
* @ Param mixed $ id
* @ Return \ Illuminate \ Auth \ UserInterface | null
*/
Public function retrieveById ($ id ){
Return $ this-> caSUSEr ();
}

/**
* Retrieve a user by the given credentials.
*
* @ Param array $ credentials
* @ Return \ Illuminate \ Auth \ UserInterface | null
*/
Public function retrieveByCredentials (array $ credentials ){
Return $ this-> casUser ();
}

/**
* Validate a user against the given credentials.
*
* @ Param \ Illuminate \ Auth \ UserInterface $ user
* @ Param array $ credentials
* @ Return bool
*/
Public function validateCredentials (Authenticatable $ user, array $ credentials ){
Return true;
}

Protected function casUser (){
$ Cas_host = \ Config: get ('app. cas_host ');
// Dump ($ cas_host );
$ Cas_context = \ Config: get ('app. cas_context ');
$ Cas_port = \ Config: get ('app. cas_port ');
\ PhpCAS: setDebug ();
\ PhpCAS: client (CAS_VERSION_2_0, $ cas_host, $ cas_port, $ cas_context );
\ PhpCAS: setNoCasServerValidation ();

If (\ phpCAS: isAuthenticated ()){
$ Attributes = array (
'Id' => \ phpCAS: getUser (),
'Name' => \ phpCAS: getUser ()
);
Return new GenericUser ($ attributes );
} Else {
// \ PhpCAS: setServerURL (\ Config: get ('app. url '));
\ PhpCAS: forceAuthentication ();
}
Return null;
}

/**
* Needed by Laravel 4.1.26 and above
*/
Public function retrieveByToken ($ identifier, $ token ){
Return new \ Exception ('not implemented ');
}

/**
* Needed by Laravel 4.1.26 and above
*/
Public function updateRememberToken (Authenticatable $ user, $ token ){
Return new \ Exception ('not implemented ');
}

}

?>

3. Modify config

Add the following three configuration items to config/app. php:

'Cas _ host' => '***', // authenticate the server
'Cas _ context' => '', // you have not figured out what it is.
'Cas _ port' => 000, // authenticate the service port
'Url' => 'HTTP: // localhost /',

4. Load the Authentication database

In app/providers/AppServiceProvider. php, add the authentication method in the register function of AppServiceProvider:

Auth: extend ('cas ', function ($ app ){
Return new CasAuthProvider;
});

Modify app/config/auth. php authentication driver: 'driver '=> 'cas ',

Configure the add-on in composer. json and add the following path to classmap in autoload:

"Autoload ":{
"Classmap ":[
**************
"App/library ",
"App/library/phpCAS ",
"App/cas"
]

}

Run composer dump-autoload in the root directory of the project.

5. Implementation

Create CasAuthController. php under app/http/controllers/and add the login and logout methods:

Public function login (){

$ Message_error = "";
If (Auth: check ()){

} Else {
If (Auth: attempt (array ())){
// Redirect to link after login
}
// Redirect to un-logged in page
}
Dump (\ phpCAS: getUser ());
Dump (Auth: user ());
}

Public function logout (){

$ Cas_host = \ Config: get ('app. cas_host ');
// Dump ($ cas_host );
$ Cas_context = \ Config: get ('app. cas_context ');
$ Cas_port = \ Config: get ('app. cas_port ');
\ PhpCAS: setDebug ();
\ PhpCAS: client (CAS_VERSION_2_0, $ cas_host, $ cas_port, $ cas_context );
\ PhpCAS: setNoCasServerValidation ();
\ PhpCAS: logoutWithRedirectService (\ Config: get ('app. url '));
}

After you add a routing rule to routes. php, you can click here to display the default login and logout methods of the project. When login is enabled, the login page of the server appears.

There is a problem, that is, after this change, the page that I set can be viewed without logging in. Now, the login page will jump out and I don't know why, thank you for your guidance!

How to install the PHP framework Laravel on CentOS 7/Ubuntu 15.04

Deploy Laravel using Nginx in Ubuntu

Deploying Laravel 14.04 using Nginx on Ubuntu 5.0

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.