从数据库中检索jasig cas服务器的属性

时间:2012-09-07 07:11:36

标签: database attributes cas saml jasig

我在从java cas(JA-SIG)中检索属性时遇到问题。它总是返回null。

以下是我的代码。我猜是attributeRepository bean永远不会被调用,因为我已经将表名更改为错误的,并且它已运行,但它没有为SQL异常提供运行时错误。

这是我的deployerConfigContext.xml文件(只是相关部分)

<bean id="authenticationManager" class="org.jasig.cas.authentication.AuthenticationManagerImpl">        
    <property name="credentialsToPrincipalResolvers">
        <list>              
            <bean class="org.jasig.cas.authentication.principal.UsernamePasswordCredentialsToPrincipalResolver">
                <property name="attributeRepository">
                    <ref bean="attributeRepository"/>
                </property>
            </bean>  
        </list>
    </property>
</bean>

 <bean id="attributeRepository" class="org.jasig.services.persondir.support.jdbc.SingleRowJdbcPersonAttributeDao">
      <constructor-arg index="0" ref="dataSource"/>
      <constructor-arg index="1" value="SELECT id,is_admin,screen_name FROM user WHERE {0}"/>
      <property name="queryAttributeMapping">
         <map>
            <entry key="login" value="eroshan@rcapl.com" />
         </map>
      </property>
      <property name="resultAttributeMapping">
         <map>
            <entry key="id" value="150" />
            <entry key="is_admin" value="0" />
            <entry key="screen_name" value="xxxx.." />
         </map>
       </property>                            
 </bean>

以下是我的客户端代码,用于检索属性。 org.jasig.cas.client.authentication.Saml11AuthenticationFilter用于获取数据。

<h1>CAS Attribute Test</h1>
    <p>User Id: <%= request.getRemoteUser() %></p>
<%
    if (request.getUserPrincipal() != null) {
      AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();

      Map attributes = principal.getAttributes();
      out.println("attribute :"+attributes.size());
      if (attributes != null) {
        Iterator attributeNames = attributes.keySet().iterator();

        out.println("Received attributes: <b>" + (attributeNames.hasNext() ? "YES!" : "No") + "</b>");
        out.println("<hr><table border='3pt' width='100%'>");
        out.println("<th colspan='2'>Attributes</th>");
        out.println("<tr><td><b>Key</b></td><td><b>Value</b></td></tr>");

        for (; attributeNames.hasNext();) {
          out.println("<tr><td>");
          String attributeName = (String) attributeNames.next();
          out.println(attributeName);
          out.println("</td><td>");
          Object attributeValue = attributes.get(attributeName);
          out.println(attributeValue);
          out.println("</td></tr>");
        }
        out.println("</table>");
      } else {
        out.println("<pre>The attribute map is empty. Review your CAS filter configurations.</pre>");
      }
    } else {
        out.println("<pre>The user principal is empty from the request object. Review the wrapper filter configuration.</pre>");
    }
%>

当我打印属性大小时,它显示0.我的代码出了什么问题?我在整理这个问题时遇到了大麻烦。很多资源可用于从Ldap获取属性,但我需要从我的数据库中获取。

1 个答案:

答案 0 :(得分:3)

您的配置看起来不错,但您需要为CAS服务定义要返回的属性,并且我在提取的配置中没有看到此部分:这是在serviceRegistryDao bean中为RegisteredServiceImpl bean完成的,属性“ allowedAttributes”。

一个例子:

<bean id="serviceRegistryDao" class="org.jasig.cas.services.InMemoryServiceRegistryDaoImpl">
 <property name="registeredServices">
   <list>
     <bean class="org.jasig.cas.services.RegisteredServiceImpl">
       <property name="id" value="0" />
       <property name="name" value="HTTP" />
       <property name="description" value="Only Allows HTTP Urls" />
       <property name="serviceId" value="http://**" />
       <property name="evaluationOrder" value="10000001" />
       <property name="allowedAttributes">
        <list>
          <value>name</value>
          <value>first_name</value>
          <value>middle_name</value>`
...