cas-overlay-template-5.3 集成Oauth2.0

第一步:在pom.xml 文件添加oauth2.0协议支持:

	        <!-- 开启oauth支持 -->
				<dependency>
					<groupId>org.apereo.cas</groupId>
					<artifactId>cas-server-support-oauth-webflow</artifactId>
					<version>${cas.version}</version>
				</dependency>

第二步:添加oauth2.0 服务相关配置

第一种:在application.properties 配置文件中oauth2.0 服务地址:

##
# CAS OAuth2.0 support
#
cas.server.name=http://cas.example.org:8099/cas
cas.server.prefix=http://cas.example.org:8099/cas

第二种:在application.properties 配置文件中,不仅仅添加oauth2.0 服务地址,还包含:access_token 存活时间和最高存活时间,refer_token 刷新token 的存活时间,授权码可以使用的次数等等相关配置属性:

##
# CAS OAuth2.0 support
#
cas.server.name=http://cas.example.org:8099/cas
cas.server.prefix=http://cas.example.org:8099/cas
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

 我这里使用的是第一种配置方式。

第三步:注册oauth 服务

在/src/main/resources/services 文件夹 里面 加入一个json格式的文本文件,根据文档要求,配置属性和文件名。

示例如下:

{
  "@class" : "org.jasig.cas.support.oauth.services.OAuthRegisteredService",
  "clientId": "key",                 
  "clientSecret": "secret",
  "bypassApprovalPrompt": false,
  "serviceId" : "^http://www.baidu.com",
  "generateRefreshToken" : true,
  "name" : "keyName",
  "id" : 100004
}

然后保存成 key-100004.json 格式是:

JSON fileName = name+ "-" + id+ ".json"

下面重点讲解:(官方文档地址:https://apereo.github.io/cas/5.2.x/installation/OAuth-OpenId-Authentication.html

注册服务配置文件说明:

@class oauth 服务实现类
@clientId 注册服务id全局唯一
@clientSecret 注册服务secret全局唯一
@supportedGrantTypes 注册服务支持授权方式,默认全部支持(建议:使用默认配置)
@supportedResponseTypes 注册服务支持响应类型,默认全部支持(建是:使用默认配置 )
@bypassApprovalPrompt 注册服务通过认证时,是否提示对话框,默认不提示(建议是:使用默认配置)
@generateRefreshToken 注册服务认证时,是否生成刷新tokem,默认(false) [建议是:开启生成刷新token 值修改为true]
@serviceId 注册服务认证成功时,配置的回调地址
@id 服务Id
@name 服务名称
@jsonFormat 注册服务认证成功时,响应的数据结构类型是json,默认(false)[建议:使用默认配置]

启动Cas5 Server 服务,总是提示

解决办法:

1、修改 \WEB-INF\classes\services\HTTPSandIMAPS-10000001.json

"serviceId" : "^(https|imaps)://.*" 
改为==>
 "serviceId" : "^(https|http|imaps)://.*",

2、修改项目配置文件\WEB-INF\classes\application.properties

cas.tgc.secure=false
cas.serviceRegistry.initFromJson=true #这一点真是太重要了!!!!!!!!

第四步:OAuth2.0 服务接口说明

启用OAuth支持后,将提供以下端点:

/oauth2.0/authorize方法介绍

response_type参数有两种:一种是code,另一种是token
code模式:response_type=code也就是授权码模式。返回authCode。
token模式:另一种是 是获取token,该token类似于accessToken,可以根据这个token获取用户信息。H5页面或者app等应用可以使用token模式。
 

/oauth2.0/accessToken方法介绍

grant_type参数有以下几种类型:
authorization_code
grant_type=authorization_code使用最多的一种,根据code 获取accessToken。常用于第三方登录,用户输入账号密码,同意并授权,第三方不知道用户的账号和密码,获取code之后,再根据code获取accessToken,使用accessToken获取用户同意获取的信息。
password
grant_type=password 允许Oauth客户端直接发送用户的账号和密码到Oauth服务端,常用于web服务,和受信任的客户端(一般是内部服务)。
client_credentials
grant_type=client_credentials采用Client Credentials方式,即应用公钥、密钥方式获取Access Token,适用于任何类型应用,但通过它所获取的Access Token只能用于访问与用户无关的Open API,并且需要开发者提前向开放平台申请,成功对接后方能使用。认证服务器不提供像用户数据这样的重要资源,仅仅是有限的只读资源或者一些开放的 API。比如获取App首页最新闻列表,由于这个数据与用户无关,所以不涉及用户登录与授权,但又不想任何人都可以调用这个WebAPI,这样场景就适用。
refresh_token
grant_type=refresh_token 上一次的令牌过期时,根据上一次请求返回的refresh_token来刷新accessToken.
 

发布了1266 篇原创文章 · 获赞 275 · 访问量 290万+

猜你喜欢

转载自blog.csdn.net/zhouzhiwengang/article/details/103879874