参考文档:https://apereo.github.io/cas/5.0.x/installation/OAuth-OpenId-Authentication.html
重新编译打包
<dependency>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-support-oauth-webflow</artifactId>
<version>${cas.version}</version>
</dependency>
配置客户端
每个OAuth客户端必须被定义为一个CAS service。下面是在json文件中配置的一个例子,可以直接拿来测试:
{
"@class" : "org.apereo.cas.support.oauth.services.OAuthRegisteredService",
"clientId": "clientid",
"clientSecret": "clientSecret",
"bypassApprovalPrompt": false,
"serviceId" : "^(https|imaps)://hello.*",
"name" : "HTTPS and IMAPS",
"id" : 10000001
}
cas-server-support-oauth-core 模块里实现了两种service,OAuthRegisteredService和OAuthCallbackAuthorizeService。很明显是根据oauth2协议中的四种方式中是否有回调URL地址来区分的
。上边是配置了一个OAuthRegisteredService的例子,下面再来一个OAuthCallbackAuthorizeService的例子。
{
@class: org.apereo.cas.support.oauth.services.OAuthCallbackAuthorizeService
serviceId: https://asdf:8443/cas/oauth2.0/callbackAuthorize.*
name: HTTP
id: 21626504634929
description: HTTP
evaluationOrder: -2147483648
logoutType: BACK_CHANNEL
attributeReleasePolicy:
{
@class: org.apereo.cas.services.ReturnAllAttributeReleasePolicy
principalAttributesRepository:
{
@class: org.apereo.cas.authentication.principal.DefaultPrincipalAttributesRepository
expiration: 2
timeUnit: HOURS
}
authorizedToReleaseCredentialPassword: false
authorizedToReleaseProxyGrantingTicket: false
}
multifactorPolicy:
{
@class: org.apereo.cas.services.DefaultRegisteredServiceMultifactorPolicy
failureMode: CLOSED
}
accessStrategy:
{
@class: org.apereo.cas.services.DefaultRegisteredServiceAccessStrategy
enabled: true
ssoEnabled: true
requireAllAttributes: true
caseInsensitive: false
}
}
这里需要注意,上边的配置里的name为oauth2协议中的client_id,description为oauth2协议中的client_secret,serviceId为oauth2协议中的回调地址,也就是跳转到cas的登录界面的链接(注意这个表述,https://localhost:8443/cas/login这个也是跳转到登录界面的链接,可以比对一下,加深理解)。
重启服务器验证
授权码模式
https://asdf.com:8443/cas/oauth2.0/authorize?response_type=code&client_id=HTTP&redirect_uri=http://the/url/receive/code
输入上边的链接,会跳转到cas的登录页面,输入用户名、密码,认证成功之后,会跳转到指定的redirect_uri,并携带着下一步用来获取access_token的code。
https://asdf.com:8443/cas/oauth2.0/accessToken?grant_type=authorization_code&client_id=HTTP&client_secret=HTTP&code=xxxx&redirect_uri=http://the/url/receive/authcode
上边的链接用code交换access_token。
然后就可以获取用户的资料了。
https://asdf.com:8443/cas/oauth2.0/profile?access_token=AT-1-ZPyTKobq43uSDp4Vxz6HcdK54fdINfNXDOi
返回值,此处可以扩展返回其他的相关资料。
{
"attributes": {},
"id": "admin"
}
隐形模式
操作跟授权码模式类似。
用户名密码模式
用户名密码模式,没有回调,使用的是OAuthRegisteredService。
https://asdf.com:8443/cas/oauth2.0/accessToken?grant_type=password&client_id=clientid&username=admin&password=admin
CAS Server返回
access_token=AT-1-mDtnfSKENpNoPxmzJUzBZk3u34xkL920K56&expires_in=7200
客户端模式
呵呵,CAS没有实现这种模式,大概是觉得这种太不安全了吧。