Laravel5 integrates the Jasig cas unified authentication system, laravel5jasig
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:
1 <?php 2 3 namespace cas; 4 5 use Illuminate\Contracts\Auth\UserProvider; 6 use Illuminate\Contracts\Hashing\Hasher; 7 use Illuminate\Contracts\Auth\Authenticatable; 8 use Illuminate\Auth\GenericUser; 9 10 class CasAuthProvider implements UserProvider {11 12 /**13 * Retrieve a user by their unique identifier.14 *15 * @param mixed $id16 * @return \Illuminate\Auth\UserInterface|null17 */18 public function retrieveById($id) {19 return $this->casUser();20 }21 22 /**23 * Retrieve a user by the given credentials.24 *25 * @param array $credentials26 * @return \Illuminate\Auth\UserInterface|null27 */28 public function retrieveByCredentials(array $credentials) {29 return $this->casUser();30 }31 32 /**33 * Validate a user against the given credentials.34 *35 * @param \Illuminate\Auth\UserInterface $user36 * @param array $credentials37 * @return bool38 */39 public function validateCredentials(Authenticatable $user, array $credentials) {40 return true;41 }42 43 protected function casUser() {44 $cas_host = \Config::get('app.cas_host');45 //dump($cas_host);46 $cas_context = \Config::get('app.cas_context');47 $cas_port = \Config::get('app.cas_port');48 \phpCAS::setDebug();49 \phpCAS::client(CAS_VERSION_2_0, $cas_host, $cas_port, $cas_context);50 \phpCAS::setNoCasServerValidation();51 52 if (\phpCAS::isAuthenticated()) {53 $attributes = array(54 'id' => \phpCAS::getUser(),55 'name' => \phpCAS::getUser()56 );57 return new GenericUser($attributes);58 } else {59 //\phpCAS::setServerURL(\Config::get('app.url'));60 \phpCAS::forceAuthentication();61 }62 return null;63 }64 65 /**66 * Needed by Laravel 4.1.26 and above67 */68 public function retrieveByToken($identifier, $token) {69 return new \Exception('not implemented');70 }71 72 /**73 * Needed by Laravel 4.1.26 and above74 */75 public function updateRememberToken(Authenticatable $user, $token) {76 return new \Exception('not implemented');77 }78 79 }80 81 ?>
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:
1 public function login() { 2 3 $message_error = ""; 4 if (Auth::check()) { 5 6 } else { 7 if (Auth::attempt(array())) { 8 // Redirect to link after login 9 }10 // Redirect to un-logged in page11 }12 dump(\phpCAS::getUser());13 dump(Auth::user());14 }15 16 public function logout() {17 18 $cas_host = \Config::get('app.cas_host');19 //dump($cas_host);20 $cas_context = \Config::get('app.cas_context');21 $cas_port = \Config::get('app.cas_port');22 \phpCAS::setDebug();23 \phpCAS::client(CAS_VERSION_2_0, $cas_host, $cas_port, $cas_context);24 \phpCAS::setNoCasServerValidation();25 \phpCAS::logoutWithRedirectService(\Config::get('app.url'));26 }
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!
Reference: https://sonnguyen.ws/how-to-integrate-phpcas-in-laravel/