该接口是cas-server认证入口,但从唯一实现类来看,它并不是认证执行者,执行者是AuthenticationHandler,该接口控制认证流程,包括异常处理和结果封装,将认证结果封装成Authentication返回。
public interface AuthenticationManager {
//有一个Credential认证成功返回Authentication,所有Credential都认证失败抛出AuthenticationException
Authentication authenticate(AuthenticationTransaction authenticationTransaction) throws AuthenticationException;
}
唯一实现PolicyBasedAuthenticationManager
核心代码
AuthenticationBuilder builder = new DefaultAuthenticationBuilder(NullPrincipal.getInstance());
builder.addCredential(new BasicCredentialMetaData(credential));
//执行认证
HandlerResult result = authenticationHandler.authenticate(credential);
//一个credential认证成功
builder.addSuccess(authenticationHandler.getName(), result);
//一个credential认证失败
builder.addFailure(authenticationHandler.getName(), e.getClass());
//credential解析,Principal才是客户真正需要的
Principal principal = principalResolver.resolve(credential);
builder.setPrincipal(principal);
//构建authentication
Authentication authentication = builder.build();
//那些情况抛出AuthenticationException?
//一个credential都没认证成功
if (builder.getSuccesses().isEmpty()) {
throw new AuthenticationException(builder.getFailures(), builder.getSuccesses());
}
//不满足认证安全策略
if (!this.authenticationPolicy.isSatisfiedBy(builder.build())) {
throw new AuthenticationException(builder.getFailures(), builder.getSuccesses());
}
//空Principal
if (principal instanceof NullPrincipal) {
throw new UnresolvedPrincipalException(authentication);
}
相关接口
Principal
客户端拿着serviceTicket来验证时,验证通过,服务器会返回一个Principal实现,默认只返回Principal的id,也可以附带返回attributes,需要自己修改xml(jsp)
- SimplePrincipal
简单实现,有id和attributes,没有其他东西 - NullPrincipal
一个空实现,id固定为字符串nobody,attributes为一个EmptyMap实例 - WebApplicationService
这个实现Principal比较奇怪,跟Principal的含义没半毛钱关系,只是要用到id属性,关于WebApplicationService 后面再写
Credential
可以把它看成用户登录时输入登录信息的实体,实际上它就是表示这个意思。看看实现就知道了
- UsernamePasswordCredential
包含username、password基本的登录信息,系统默认用这个,可看WEB-INF/webflow/login/login-webflow.xml - RememberMeUsernamePasswordCredential
继承UsernamePasswordCredential ,增加了rememberMe,是不是登录时候的那些东西 - BasicIdentifiableCredential
这个更简洁,只有一个id属性,系统没有那个地方用到它 - OneTimePasswordCredential
一次性密码,系统也没有地方使用 - HttpBasedServiceCredential
通过回调callbackUrl来验证,回调成功表示验证通过,该Credential 绑定了关联的registeredService,getId返回的是callbackUrl字符串。
AuthenticationHandler
认证的执行者,类似dao,会跟数据源打交道
- HttpBasedServiceCredentialsAuthenticationHandler
代理认证,用于HttpBasedServiceCredential认证,里面有一个HttpClient(注入),当RegisteredService的ProxyPolicy接受CallbackUrl且HttpClient请求CallbackUrl成功(返回指定的code)表示认证通过 - AbstractUsernamePasswordAuthenticationHandler
抽象类,用于UsernamePasswordCredential的认证,子类一般会使用外部数据源查库,如使用JDBC,只需依赖cas-server-support-jdbc支持。 - JaasAuthenticationHandler
使用JAAS认证,用的不多,具体配置可以看源码 - AcceptUsersAuthenticationHandler
接受一个静态的用户数据验证,默认去cas.properties中accept.authn.users值进行校验,系统默认用这种方式,实际中肯定要换成第三方数据源的。
PrincipalResolver
从给定的credential解析出一个Principal
- PersonDirectoryPrincipalResolver
使用Jasig Person Directory库解析,当客户端除了需要id外,还需要更多的用户属性时,推荐使用这个。通过使用IPersonAttributeDao返回Map<String, List<Object>>里面可存放关联的属性。默认的实现StubPersonAttributeDao只是简单的维护了一个静态backingMap,实际中建议连接数据源进行动态查询,具体可看IPersonAttributeDao实现。 - BasicPrincipalResolver
解析工作完全委托给PrincipalFactory,自己什么也不做
AuthenticationPolicy
安全策略检查,相当于这里设置最后一道关卡,检查Authentication的合法性,如果没有特别的需求用默认的即可
- AnyAuthenticationPolicy
类里有一个标志位tryAll,取cas.properties中cas.authn.policy.any.tryall值,默认false
true:Authentication在Credentials数量必须等于Successes+Failures的数量
false:Successes不为空就行 - NotPreventedAuthenticationPolicy
继承AnyAuthenticationPolicy - AllAuthenticationPolicy,增加了一个条件
public boolean isSatisfiedBy(final Authentication authentication) {
for (final String handler : authentication.getFailures().keySet()) {
if (authentication.getFailures().get(handler).isAssignableFrom(PreventedException.class)) {
return false;
}
}
return super.isSatisfiedBy(authentication);
}
- RequiredHandlerAuthenticationPolicy
//requiredHandlerName值取cas.properties中cas.authn.policy.req.handlername,默认handlerName,tryAll取cas.authn.policy.req.tryall,默认false
public boolean isSatisfiedBy(final Authentication authn) {
boolean credsOk = true;
if (this.tryAll) {
credsOk = authn.getCredentials().size() == authn.getSuccesses().size() + authn.getFailures().size();
}
return credsOk && StringUtils.isNotBlank(this.requiredHandlerName) && authn.getSuccesses().containsKey(this.requiredHandlerName);
}
- ContextualAuthenticationPolicy
接口,更复杂的校验,如获取某一个策略Context检验Authentication,具体Context看具体实现
至此cas-server的Credential 认证部分就这些内容了,看起来比较简单,但这种架构设计非常值得我们学习,相信在大部分业务场景中都会用到,这里cas-server提供了良好的扩展性,方便第三方库集成。主要还是用到了面向接口编程。