服务端修改内容
修改工程
- cas-server-core-authtication
修改类
- org.jasig.cas.authentication.principal.DefaultPrincipalFactory
修改方法
- public Principal createPrincipal(final String id, final Map<String, Object> attributes)
修改内容
- 获取dataSource后,使用jdbcTemplate获取需要的认证信息,然后将内容转换成xml,并添加到attributes中返回,代码示例:
DataSource dataSource = ApplicationContextProvider.getApplicationContext().getBean("dataSource", DataSource.class);
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
//获取用户信息
CsUser csUser = getUserInfo(id,jdbcTemplate);
List<CsOrgan> list = null;
if(csUser!=null){
//获取所属机构信息
list = getOrganInfo(csUser.getUserId(),jdbcTemplate);
}
WdimInfo wdimInfo = new WdimInfo(csUser,list);
if(wdimInfo!=null){
String wdim = null ;
try {
wdim = Marshal.marshal(wdimInfo);
} catch (Exception e) {
e.printStackTrace();
}
attributes.put("wdim",wdim);
}
客户端修改内容
修改工程
修改类
- org.jasig.cas.client.authentication.AttributePrincipalImpl
修改方法
修改内容
- 由于返回的内容为xml格式,故需要在客户端获取之后转换成正确的对象,添加代码示例如下:
try {
if(attributes.get("wdim")!=null){
this.wdimInfo = (WdimInfo) Unmarshal.unmarshal(WdimInfo.class,(String)attributes.get("wdim"));
}
} catch (Exception e) {
this.wdimInfo = null;
e.printStackTrace();
}
添加方法
- 在转换完wdimInfo之后,需要增加get方法,如下:
public WdimInfo getWdimInfo() {
return wdimInfo;
}
额外说明
Marshal
/**
* bean 转xml
*/
public class Marshal {
public static String marshal(Object object)throws Exception{
String returnValue=null;
Class<?> clazz=null;
JAXBContext context=null;
Marshaller marshaller=null;
StringWriter writer=null;
try{
clazz=object.getClass();
writer=new StringWriter();
context=JAXBContext.newInstance(clazz);
marshaller=context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT,true);
marshaller.marshal(object,writer);
returnValue=writer.toString();
}finally{
if(writer!=null){
try{
writer.close();
}catch(Exception e){
}
writer=null;
}
}
return returnValue;
}
}
Unmarshal
public class Unmarshal {
public static Object unmarshal(Class<?> clazz, String xml)throws Exception {
Object returnValue=null;
JAXBContext context=null;
StringReader reader=null;
Unmarshaller unmarshaller=null;
try{
reader=new StringReader(xml);
context= JAXBContext.newInstance(clazz);
unmarshaller=context.createUnmarshaller();
returnValue=unmarshaller.unmarshal(reader);
}finally{
if(reader!=null){
try{
reader.close();
}catch(Exception e){
}
reader=null;
}
}
return returnValue;
}
}