我们都知道CAS在登录成功后会返回一些我们所需要的值来提供给各个客户端,有一种情况就是根据不同的情况我们要查询不同的表来进行反馈信息,比如说 当 是管理员登录的时候返回的可能是从A表中查询的数据,当普通用户登录的时候可能返回的是B表中的信息,CAS怎么才能实现这种需求呢?现在我们就实现这种需求,需要修改deployerConfigContext.xml 配置文件,也需要修改源码,现在我们现讲怎么修改配置文件,其实很简单 我们配置返回登录信息的配置如下:
<property name="credentialsToPrincipalResolvers">
<list>
<bean class="org.jasig.cas.authentication.principal.UsernamePasswordCredentialsToPrincipalResolver" >
<property name="attributeRepository" ref="attributeRepository" />
<property name="attributeRepository1" ref="attributeRepository1" />
</bean>
<bean class="org.jasig.cas.authentication.principal.HttpBasedServiceCredentialsToPrincipalResolver" />
</list>
</property>
下面是配置的attributeRepsoitory和attributeRepsoitory1两个Bean
<bean
class="org.jasig.services.persondir.support.jdbc.SingleRowJdbcPersonAttributeDao"
id="attributeRepository">
<constructor-arg index="0" ref="casDataSource" />
<constructor-arg index="1"
value="select * from A where {0}" />
<property name="queryAttributeMapping">
<map>
<!-- key对应username,value对应数据库用户名字段 -->
<entry key="username" value="loginname" />
</map>
</property>
<property name="resultAttributeMapping">
<map>
<!--key为对应的数据库字段名称,value为提供给客户端获取的属性名字,系统会自动填充值 -->
<entry key="A" value="A" />
<entry key="AA" value="AA" />
</map>
</property>
</bean>
<bean
class="org.jasig.services.persondir.support.jdbc.SingleRowJdbcPersonAttributeDao"
id="attributeRepository1">
<constructor-arg index="0" ref="casDataSource" />
<constructor-arg index="1"
value="select * from B where {0}" />
<property name="queryAttributeMapping">
<map>
<!-- key对应username,value对应数据库用户名字段 -->
<entry key="username" value="loginname" />
</map>
</property>
<property name="resultAttributeMapping">
<map>
<!--key为对应的数据库字段名称,value为提供给客户端获取的属性名字,系统会自动填充值 -->
<entry key="B" value="B" />
<entry key="BB" value="BB" />
</map>
</property>
</bean>
以上就是配置文件部分的修改(该部分修改时基于原来返回信息的配置基础上增加的一个attributeReponsitory1 而已)
下面讲一下源码怎么修改
在org.jasig.cas.authentication.principal.AbstractPersonDirectoryCredentialsToPrincipalResolver这个类中有如下代码:
@NotNull
private IPersonAttributeDao attributeRepository = new StubPersonAttributeDao(new HashMap<String, List<Object>>());
看到上述代码相比就知道了上述配置文件中 <property name="attributeRepository" ref="attributeRepository" />
这句话的作用了,所以我们就参照这个attributeRepository 的定义,我们也定义一个类似的 attributeRepository1,如下:
private IPersonAttributeDao attributeRepository1 = new StubPersonAttributeDao(new HashMap<String, List<Object>>());
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);
//这样就完成了如果查不同的表返回登录信息
以上纯属个人应用总结的方法,不一定是最合适的也不一定是最正确的。