2

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

下面是我的代码。我猜attributeRepository是从未调用过bean,因为我将表名更改为错误的,并且它运行了,但它没有给出SQL Exception的运行时错误。

这是我的 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 获取属性,但我需要从我的数据库中获取属性。

4

1 回答 1

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>`
...
于 2012-09-10T07:12:22.723 回答