Friday, September 20, 2013

CAS Client with Java web.xml

We have configured the CAS Server in the previous blog post. Now we will see how to call it from client ie. in Projects where Single Sign On is needed.
 
Download CAS-Client and go to module folder. add all the jar files in the folder to your project library.
 
The following configurations are to be added to your web.xml for adding CAS Single Sign on to your project
  1. AuthenticationFilter
  2. TicketValidationFilter (whichever one is chosen)
  3. HttpServletRequestWrapperFilter
  4. AssertionThreadLocalFilter

Add the below to web.xml file

<filter>
<filter-name>CAS Authentication Filter</filter-name>
<filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
<init-param>
         <param-name>casServerLoginUrl</param-name>
          <param-value>https://localhost/cas-server-webapp-3.5.2/login</param-value>
</init-param>
<init-param>
          <param-name>serverName</param-name>
          <param-value>http://localhost:8080</param-value>
</init-param>
<init-param>
         <param-name>renew</param-name>
          <param-value>false</param-value>
</init-param>
<init-param>
        <param-name>gateway</param-name>
         <param-value>false</param-value>
</init-param>
</filter>

<filter>
<filter-name>CAS Validation Filter</filter-name>
<filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>
<init-param>
       <param-name>casServerUrlPrefix</param-name>
       <param-value>https://localhost/cas-server-webapp-3.5.2</param-value>
</init-param>

<init-param>
         <param-name>serverName</param-name>
         <param-value>http://localhost:8080</param-value>
</init-param>

<init-param>
      <param-name>proxyCallbackUrl</param-name>
      <param-value>http://localhost:8080/webappcas2/proxyCallback</param-value>
</init-param>

<init-param>
       <param-name>proxyReceptorUrl</param-name>
       <param-value>/webappcas2/proxyCallback</param-value>
</init-param>

</filter>
 
<filter>
            <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
          <filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>
</filter>
 
<filter>
            <filter-name>CAS Assertion Thread Local Filter</filter-name>
            <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>
</filter>
 
<filter-mapping>
         <filter-name>CAS Authentication Filter</filter-name>
          <url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
         filter-name>CAS Validation Filter</filter-name>
         <url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
      <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
      <url-pattern>/*</url-pattern>
</filter-mapping>
 
<filter-mapping>
       <filter-name>CAS Assertion Thread Local Filter</filter-name>
       <url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
        <filter-name>CAS Validation Filter</filter-name>
        <url-pattern>/proxyCallback</url-pattern>
</filter-mapping>

Setting up CAS Server for JDBC Postgres

We are going to see on how to setup CAS Server for Post gres.database. This is for single sign on

Click here to Download CAS Server.

 What you are viewing is the folder structure of the CAS Server you have downloaded.

Go to modules folder.

Take the cas-server-webapp-3.5.2.war and deploy it in your tomcat.

Start the tomcat server. Now the war will be deployed. Now stop the tomcat server and remove the cas-server-webapp-3.5.2.war file from server so that it will not overwrite the folder deployed.

Copy cas-server-support-jdbc-3.5.2.jar from Modules folder and put it inside the webapps\cas-server-webapp-3.5.2\WEB-INF\lib folder

This jar is required for JDBC Connection.

As we are using Postgres we need to download the driver for it. Please Download

We also require 2 more jars. Also download these jars mentioned below.

     commons-dbcp-1.2.2.jar

     commons-pool-1.2.jar

Put all the jars in the same path webapps\cas-server-webapp-3.5.2\WEB-INF\lib


Go to the WEB-INF folder. you will see the folder structure as shown here. Open the deployerConfigContext.xml file and modify the contents as shown below.

Search for the line below

<bean class="org.jasig.cas.authentication.handler.support. SimpleTestUsernamePasswordAuthenticationHandler" />
 </list>
  </property>
 </bean>
Replace it with below code



     <bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">
        <property name="dataSource" ref="dataSource" />
        <property name="sql" value="select password from user_list where lower(user_name) = lower(?)" />
      </bean>

        
           </list>
        </property>
    </bean>
    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
     <property name="driverClassName">
      <value>org.postgresql.Driver</value>
     </property>
     <property name="url">
      <value>jdbc:postgresql://localhost:5432/testing</value>
     </property>
     <property name="username">
      <value>postgres</value>
     </property>
     <property name="password">
      <value>postgres</value>
     </property>
    </bean>

Now run the apache tomcat and go to url https://localhost/cas-server-webapp-3.5.2/login
If it is working correctly, you should see authentication screen.