素材巴巴 > 程序开发 >

CAS统一登录认证(11): 提供oauth2.0认证服务器

程序开发 2023-09-20 15:33:27

    CAS可以提供Oauth2.0的第三方认证服务,这个服务,cas可以是作为服务客户端,如通过qq,微信,csdn,github等认证后登录cas,这时,qq互联等是oauth服务的提供者,cas是oauth客户端。另外一种功能是CAS作为第三方认证提供者,即Oauth服务器,为单位的各应用提供第三方Ouath认证服务,这时对提供oauth服务来说,cas是服务端,接入应用软件是Oauth服务的客户端。

   CAS作为Oauth客户端,另文再介绍,本文中,CAS是Oauth2.0服务提供者。

1.基本原理,不多着解析,借用一张网络图

2.cas服务器配置

(1)修改pom.xml,编译让cas包含Oauth插件

   org.apereo.cascas-server-support-oauth${cas.version}

(2) 编译部署后,修改application.properties 增加

cas.server.name=https://author.linbsoft.com
cas.server.prefix=https://author.linbsoft.com/cas

######### oauth2.0 ###########
cas.authn.oauth.refreshToken.timeToKillInSeconds=2592000
cas.authn.oauth.code.timeToKillInSeconds=30
cas.authn.oauth.code.numberOfUses=1
cas.authn.oauth.accessToken.releaseProtocolAttributes=true
cas.authn.oauth.accessToken.timeToKillInSeconds=7200
cas.authn.oauth.accessToken.maxTimeToLiveInSeconds=28800
cas.authn.oauth.grants.resourceOwner.requireServiceHeader=true
cas.authn.oauth.userProfileViewType=NESTED

地址改为你自己的cas服务器URL

(3)在services 增加一个json文件,允许向特定应用提供oauth服务

{"@class" : "org.apereo.cas.support.oauth.services.OAuthRegisteredService","clientId": "123666888","clientSecret": "b7cb58ffeba34a68bed632f8f2d55d2f","bypassApprovalPrompt": false,"generateRefreshToken": false,"serviceId" : "^(http|https)://.*","name" : "Linbsoft OAuth service ","id" : 101,"attributeReleasePolicy" : {"@class" : "org.apereo.cas.services.ReturnAllAttributeReleasePolicy"}
 }
 

重启tomcat服务器即可,在实际应用中可以单个应用授权。

3.CAS Oauth2.0客户端应用demo

以下是我的一个测试demo

点击发起第三方认证后,弹出窗口跳转到

https://author.linbsoft.com/cas/oauth2.0/authorize?response_type=code&client_id=123666888&redirect_uri=http://aaa.linbsoft.com/casoauth/gettoken.aspx 

第二步会出现cas的登录界面(如果未登录),已登录直接跳过。

第三步cas服务器请求授权

点允许后会返回code

http://aaa.linbsoft.com/casoauth/gettoken.aspx?code=OC-13-McilkUuZCyaCars1jC5RzXUtbokuPY2C

我把返回的code和其它请求信息构建了一个form表单,提交换取Token

点击提交post表单到  https://author.linbsoft.com/cas/oauth2.0/accessToken

获取Token

把token 提交给cas换取用户信息

实际是向cas发送get请求

https://author.linbsoft.com/cas/oauth2.0/profile?access_token=AT-9-abQcKy4-xEa7SgPfBKJHDO1pdYjF2eAX&expires_in=28800

cas服务器返回用户信息

客户端可以根据这个信息登录客户端系统了。

 

 


标签:

素材巴巴 Copyright © 2013-2021 http://www.sucaibaba.com/. Some Rights Reserved. 备案号:备案中。