jasig-cas单点登录之自定义验证

jasig-cas单点登录之自定义验证:

自定义验证(可参考系统自带的SimpleTestUsernamePasswordAuthenticationHandler):
找到WEB-INF/deployerConfigContext.xml,可以看出cas的配置和spring一样的,都是采用了bean来配置的。
找到bean(authenticationManager),该bean有2个属性credentialsToPrincipalResolvers、authenticationHandlers,
前者是用什么去溶解凭证(想想就知道,肯定是帐号和密码的对象。)、第二个是验证句柄(纯粹个人的理解)。
so在
credentialsToPrincipalResolvers列表中配置的是溶解凭证的东西,
authenticationHandlers列表中配置的是验证的东东。



请求验证句柄authenticationHandlers属性下的列表(可以配置多个,类似于filter链,有一个返回true则验证通过。),列表必须是实现AuthenticationHandler接口的类或抽象类(可以参考例子中的org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler)。
接口AuthenticationHandler是CAS验证的顶级接口.
接口Credentials是证书的意思,该接口的第一句注释:Marker interface for credentials required to authenticate a principal.所以我们必须去验证principal这样一个东西。principal其实也是一个接口,我们不能去验证接口,所以肯定是验证接口的实现类。cas-server提供的例子给出了org.jasig.cas.authentication.principal.SimplePrincipal这样一个实现类,观察之可以看出其实就是一个维护id--map的映射,只不过以Collections.unmodifiableMap(map)的形式返回不可修改的map。

在AuthenticationHandlerImpl(我们自己的AuthenticationHandler实现类)中直接返回true是不可能验证通过的。
必须满足2个条件:
第一:AuthenticationHandler接口的实现类的supports方法对其参数Credentials验证通过,其实直接return true;也行的。
第二:AuthenticationHandler接口的实现类的authenticate(真正的验证方法)必须拿到其参数credentials,(写到这里我发现Credentials似乎满天飞!!确实,这个玩意中文翻译为 证书嘛jasig的安全性靠的就是https的证书。),那么这个Credentials到底是何方圣神呢?通过观察SimpleTestUsernamePasswordAuthenticationHandler这个类发现他使用的证书对象是UsernamePasswordCredentials,一个实体bean而已。

满足了这2个条件后面的在你喜欢的地方return true;就OK了。验证会在你返回真的时候通过。

总结: AuthenticationHandler的实现AuthenticationHandlerImpl,  (配置在authenticationHandlers属性下)
CredentialsToPrincipalResolver的实现CredentialsToPrincipalResolversImpl,(配置在credentialsToPrincipalResolvers属性下)
Principal的实现PrincipalImpl    (凭证的id--map的映射,注意这个映射比较特殊,是不可以修改的)
Credentials的实现UsernamePasswordCredentials  (一个实体bean而已)

猜你喜欢

转载自jqsl2012.iteye.com/blog/1114557