美文网首页cas工作
cas4.2.7添加更多返回信息

cas4.2.7添加更多返回信息

作者: sweetMemories | 来源:发表于2017-07-14 10:43 被阅读912次

服务端修改内容

  • 服务端版本:4.2.7

修改工程

  • 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);
}

客户端修改内容

  • 版本:3.4.1

修改工程

  • cas-client-core

修改类

  • org.jasig.cas.client.authentication.AttributePrincipalImpl

修改方法

  • 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;
}

额外说明

  • 上述代码中的util方法如下:

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;
    }
}

相关文章

  • cas4.2.7添加更多返回信息

    服务端修改内容 服务端版本:4.2.7 修改工程 cas-server-core-authtication 修改类...

  • cas4.2.7添加新页面

    cas4.2.7添加新页面说明 controller代码 配置WEB-INF/spring-configurati...

  • Elasticsearch学习笔记

    kibana 添加索引 返回结果 获取索引信息 返回结果 添加记录 返回结果 返回结果 更新记录 返回结果 以下使...

  • 数组方法

    push 向数组的末尾添加一个或更多元素,并返回新的长度。 unshift 向数组的开头添加一个或更多元素,并返回...

  • CAS单点登录之支持数据库认证

    本博客介绍一下基于CAS4.2.7的配置,之前博客CAS4.2.7服务端配置已经介绍了怎么部署CAS服务端,不过在...

  • Json学习

    json的返回与解析 添加json依赖包以及处理json为bean的包 返回json格式数据到前端(返回信息均为键...

  • 数组方法总结

    push(): 向数组的末尾添加一个或更多元素 返回值为添加的元素 pop(): 删除数组的最后一个元素 返回值为...

  • Array对象的方法

    1,push()向数组的末尾添加一个或更多元素,并返回新的长度 2.unshift()向数组的开头添加一个或更多元...

  • CAS 5.1.5版本多属性返回

    默认情况下单点登录只返回登录的用户名,不会返回其它的用户信息。如果想要返回更多的用户信息,我们需要进行扩展开发。比...

  • 为通知添加更多页面(Adding Pages to a Noti

    为通知添加更多页面(Adding Pages to a Notification) 当你想提供更多的信息,而这些信...

网友评论

    本文标题:cas4.2.7添加更多返回信息

    本文链接:https://www.haomeiwen.com/subject/vcynhxtx.html