2

I have a problem in retrieving attributes from java cas (JA-SIG). It always return null.

Below is my code. What I guess was attributeRepository bean is never called, because I have changed table name to wrong one, and it ran, but it didn't give the runtime error for SQL Exception.

This is my deployerConfigContext.xml file (only the relevant part)

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

Below is my client code to retrieve attributes. org.jasig.cas.client.authentication.Saml11AuthenticationFilter is used for getting data.

<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>");
    }
%>

When I print the attribute size it shows 0. What is wrong with my code? I'm having big trouble sorting this issue. Lot of resources are available for getting attributes from Ldap, but I need from my db.

Nunser
  • 4,512
  • 8
  • 25
  • 37
Amila
  • 243
  • 1
  • 10
  • 24

1 Answers1

3

Your config looks good, nonetheless you need to define for your CAS service which attributes you want to return and I don't see this part in your extracted config : this is done in the serviceRegistryDao bean for a RegisteredServiceImpl bean, property "allowedAttributes".

An example :

<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>`
...
jleleu
  • 2,309
  • 1
  • 13
  • 9
  • Thanks jleleu. you are correct. i gave error values for queryAttributeMapping tag. – Amila Sep 12 '12 at 05:39
  • Hey Amila/jleleu, can you please copy paste the snippet of your code here for reference. I am trying to achieve the same thing and not able to figure out how to get the user attributes data. I will be much thankful to you. – Jayesh Nov 23 '12 at 14:06