我们都知道CAS在登录成功后会返回一些我们所需要的值来提供给各个客户端,有一种情况就是根据不同的情况我们要查询不同的表来进行反馈信息,比如说 当 是管理员登录的时候返回的可能是从A表中查询的数据,当普通用户登录的时候可能返回的是B表中的信息,CAS怎么才能实现这种需求呢?现在我们就实现这种需求,需要修改deployerConfigContext.xml 配置文件,也需要修改源码,现在我们现讲怎么修改配置文件,其实很简单 我们配置返回登录信息的配置如下:
下面是配置的attributeRepsoitory和attributeRepsoitory1两个Bean
class="org.jasig.services.persondir.support.jdbc.SingleRowJdbcPersonAttributeDao"
id="attributeRepository">
value="select * from A where {0}" />
class="org.jasig.services.persondir.support.jdbc.SingleRowJdbcPersonAttributeDao"
id="attributeRepository1">
value="select * from B where {0}" />
以上就是配置文件部分的修改(该部分修改时基于原来返回信息的配置基础上增加的一个attributeReponsitory1 而已)
下面讲一下源码怎么修改
在org.jasig.cas.authentication.principal.AbstractPersonDirectoryCredentialsToPrincipalResolver这个类中有如下代码:
@NotNull
private IPersonAttributeDao attributeRepository = new StubPersonAttributeDao(new HashMap>());
看到上述代码相比就知道了上述配置文件中
这句话的作用了,所以我们就参照这个attributeRepository 的定义,我们也定义一个类似的 attributeRepository1,如下:
private IPersonAttributeDao attributeRepository1 = new StubPersonAttributeDao(new HashMap>());
public final void setAttributeRepository(final IPersonAttributeDao attributeRepository1) {
this.attributeRepository1 = attributeRepository1;
}
这样注入Bean 就完成了,下面就可以具体的应用了,还是在这个类中,找到下面的代码:
final IPersonAttributes personAttributes = this.attributeRepository.getPerson(principalId);
这句代码的作用就是根据username查询返回的信息,
根据配置我们可以这样改造这句话,假定有一个loginrole 参数 为1的时候执行attributRepository1这个bean
int loginrole = 1;
final IPersonAttributes personAttributes = loginrole ==1?this.attributeRepository1.getPerson(principalId):this.attributeRepository.getPerson(principalId);
//这样就完成了如果查不同的表返回登录信息
以上纯属个人应用总结的方法,不一定是最合适的也不一定是最正确的。
分享到:
2013-11-23 15:43
浏览 441
评论