Apereo CAS 5.0.X 启用OAuth/OpenID认证

参考文档: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没有实现这种模式,大概是觉得这种太不安全了吧。

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈振阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值