CAS3.5.x提供了oauth的支持,包括客户端和服务端,cas-server-support-oauth依赖架包web
scribe-1.3.5.jar
scribe-up-1.2.0.jar
jackson-core-2.3.0.jar,jackson-databind-2.3.0.jar。数据库
CAS默认提供了三个服务:
/oauth2.0/authorize
Input GET parameters required : client_id and redirect_uri.
/oauth2.0/accessToken
Input GET parameters required : client_id, redirect_uri, client_secret and code.
/oauth2.0/profile
Input GET parameter required : access_token.session
##关于接入的一些背景: 1.cas的web登陆访问路径为https://cas.sayi.com:8443/cas/login
2.回调地址为http://www.doubannote.org/(虚拟地址,实际不存在)
3.client_Id为key
4.client_secret为secret
5.应用名称为DoubanNote
6.核心类为org.jasig.cas.support.oauth.web.OAuth20WrapperControllerapp
下面配置cas server支持oauth2 server,咱们从oauth2 client向cas接入为步骤来分析每一步的配置:ui
在成熟的系统中,一般提供页面供用户申请应用,而后提供用户client_id和client_secret,并容许用户配置回调地址,那么oauthserver端(即CAS Server)首先考虑的就是须要持久化这些配置。默认在文件deployerConfigContext.xml的serviceRegistryDao中配置应用服务,实际使用中,咱们能够将申请的应用信息存储在数据库中:url
<bean id="serviceRegistryDao" class="org.jasig.cas.services.InMemoryServiceRegistryDaoImpl"> <property name="registeredServices"> <list> <bean class="org.jasig.cas.services.RegisteredServiceImpl"> <property name="id" value="1" /> <property name="name" value="HTTP" /> <property name="description" value="oauth wrapper callback url" /> <property name="serviceId" value="${server.prefix}/oauth2.0/callbackAuthorize" /> </bean> <bean class="org.jasig.cas.services.RegisteredServiceImpl"> <property name="id" value="2" /> <property name="name" value="key" /> <property name="description" value="secret" /> <property name="serviceId" value="http://www.doubannote.org/" /> <property name="theme" value="DoubanNote" /> </bean> ......
如代码所示,咱们新注册了两个bean,关于应用的配置在第二个bean中,name为client_id,description为client_secret,serviceId为回调地址,theme为应用名称。
关于第一个bean的用途将在下面介绍。code
一般客户端构造的url可能以下(参数能够参照标准的oauth2 protocol,可是不一样的oauth server一般提供了本身的标准):server
https://cas.sayi.com:8443/cas/oauth2.0/authorize?client_id=key&redirect_uri=http://www.doubannote.org/&response_type=code
在这里就要求cas server能对/oauth2.0/authorize的url进行处理,那么就须要配置映射,在web.xml中配置以下:xml
<servlet-mapping> <servlet-name>cas</servlet-name> <url-pattern>/oauth2.0/*</url-pattern> </servlet-mapping>
在cas-servlet.xml中配置映射:token
<prop key="/oauth2.0/*">oauth20WrapperController</prop> ... ... <bean id="oauth20WrapperController" class="org.jasig.cas.support.oauth.web.OAuth20WrapperController" p:loginUrl="${server.prefix}/login" p:servicesManager-ref="servicesManager" p:ticketRegistry-ref="ticketRegistry" p:timeout="7200" />
如上配置了以后,咱们获取受权码的连接会转向login页面,此时的service地址就是step1中配置的第一个bean的serviceId,经过这个默认提供的地址间接的获取到ST。
https://cas.sayi.com:8443/cas/login?service=https%3A%2F%2Fcas.sayi.com%3A8443%2Fcas%2Foauth2.0%2FcallbackAuthorize
认证成功以后,就会携带值为ST的参数跳转到callbackAuthorize页面,此时生成的ST即为受权码,回调地址、服务名称经过session传递过来。
https://cas.sayi.com:8443/cas/oauth2.0/callbackAuthorize?ticket=ST-5-ywMLFaXQFnDeFI7erFy7-cas.sayi.com
默认受权码只能使用一次,且有效时间为10s,能够经过票根过时策略进行配置时间。
构造的URL以下:
https://cas.sayi.com:8443/cas/oauth2.0/accessToken?client_id=key&client_secret=secret&grant_type=authorization_code&redirect_uri=http://www.doubannote.org/&code=ST-1-3jLuZnhcAvLiLdy7R6ft-cas.sayi.com access_token=TGT-2-qWkLyEbeoby043q05p5GHXfBg7qtdPZjEUhfemgg3UKbxAyB5s-cas.sayi.com&expires=7143
经过返回的值能够得到access_token.
构造URL以下:
https://cas.sayi.com:8443/cas/oauth2.0/profile?access_token=TGT-1-gn3p9EMfFEajKOJ9DdNqd2PefJdIbIeXuESyzU4EctMtBqITRG-cas.sayi.com { "id":"sayi", "attributes":[ { "uid":"uid" }, { "eduPersonAffiliation":"eduPersonAffiliation" }, { "groupMembership":"groupMembership" } ] }
cas server支持oauth2 server,无非就是考虑对/authorize、/accessToken、/profile的请求的处理,在服务端进行应用配置后,对接入的应用进行校验,好比回调地址、client_secret等。在与cas server的融合中,主要就是cas认证与/authorize的融合。在这里使用的是callbackAuthorize的方式,cas默认提供了/oauth2.0/callbackAuthorize的service地址,经过此地址cas认证成功以后生成ST,此值即为受权码,传递给应用的回调地址便可。 整体来讲oauth2的支持在cas3.5.x中并不完善,并且OAuth2的实现也不是标准的,对于3.5.x版本咱们须要扩展OAuth20WrapperController来进一步融合oauth2 protocol。