CAS 6.1.x single sign on and sign out

Posted by nev25 on Wed, 15 Sep 2021 07:55:33 +0200

CAS (Central Authentication Service): a set of solutions for SSO, that is, unified identity authentication service or central identity server, which is composed of server and client, which is easy to integrate enterprise applications.

CAS principle
  • framework

  • The most basic protocol process of CAS

  • Access service: the SSO client sends a request to access the service resources provided by the application system.
  • Directional authentication: the SSO client redirects the user request to the SSO server.
  • User authentication: user identity authentication.
  • Issue ticket: the SSO server will generate a random Service Ticket.
  • Verify ticket: the SSO server verifies the validity of the ticket Service Ticket. After verification, the client is allowed to access the service.
  • Transmit user information: after the SSO server verifies the ticket, it transmits the user authentication result information to the client.
CAS Server deployment
  • Build war package

    • CAS overlay template project download address: , select the required version and download it locally. The new version needs to build its own war package.

      git clone -b Branch name
    • After the initial construction of the project is completed, execute the command according to the prompt in README. cas.war can be found in the build/libs / directory.

    • If you want to configure by yourself, you can execute the. / gradlew[.bat] explodeWar command to decompress the war package, copy the extracted CAS Resources folder to the src/main / directory, and then modify the relevant files. The main configuration file src/main/resources/ needs to be built again after modification to make the latest configuration effective.

  • cas needs to use https to access, so it needs to generate certificates and keys. Here, use the data certificate management tool keytool provided by Java

    • Generate key (alias, key and certificate name should be consistent)

      keytool -genkey -alias castest -keyalg RSA -keystore Corresponding path/keystore/castest
    • Export certificate (issue certificate to castest here)

      keytool -export -trustcacerts -alias castest -file Corresponding path/keystore/castest.cer -keystore Corresponding path//keystore/castest
    • Import the certificate into the JDK certificate Library (default password: changeit)

      keytool -import -trustcacerts -alias castest -file Corresponding path/keystore/castest.cer -keystore "jdk route/jdk-11/lib/security/cacerts"
    • Configure tomcat https access, find conf/server.xml in the tomcat installation directory, add https configuration, and configure the generated key and the password set when generating the key into the file.

      <Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
                     maxThreads="150" SSLEnabled="true" scheme="https" secure="true" 
      			   clientAuth="false" sslProtocol="TLS" 
    • Find the hosts file in the C:\Windows\System32\drivers\etc directory, add the configuration, and point the domain name issued by the certificate to castest
  • Put the constructed war package into the webapps directory of Tomact, and start tomcat to automatically decompress and deploy the war package.

    • If it starts normally, access https://castest:8443/cas/login You can access the default login page of CAS.
    • The default static authentication method is adopted, with user name casuser and password Mellon.
  • We generally do not use static authentication in our actual development. We can configure database authentication as needed. MySQL database is used here.

    • Introduce relevant dependencies in build.gradle and pay attention to your own MySQL version

      dependencies {
          // Other CAS dependencies/modules may be listed here...
          compile "org.apereo.cas:cas-server-webapp-init:${casServerVersion}"
          compile "org.apereo.cas:cas-server-support-json-service-registry:${casServerVersion}"
      //    Add package related to database authentication
          compile "org.apereo.cas:cas-server-support-jdbc:${casServerVersion}"
          compile "org.apereo.cas:cas-server-support-jdbc-drivers:${casServerVersion}"
          compile "mysql:mysql-connector-java:5.7.9"
      //    compile "org.jasig.cas:${casServerVersion}"
    • Comment out the previous static authentication configuration in src/main/resources/ and add the database authentication configuration.

      • The default password encryption method of CAS is MD5. If the password in the database is not encrypted, you need to manually generate the corresponding MD5 value to pass the verification. However, I use the online tool to generate MD5 encryption password, and the verification has not passed. The general reason for consulting the data is that the MD5 algorithm used by CAS is different from that used by us. You can implement PasswordEncoder yourself, The configuration uses its own implementation to verify the password (not implemented here).
      • If it is not configured, the plaintext mode is used. Here, the plaintext mode is used.
      # CAS Authentication Credentials
      #cas.authn.accept.users=casuser::Mellon Credentials
      cas.authn.jdbc.query[0].sql=select user_passwd from d_user where user_name=?
      # Password field name in database
      # com.mysql.cj.jdbc.Driver: MySQL connector java version 6 has more time zone settings than version 5

    Here, we can start tomcat and log in to CAS Server through the user name and password in the database.

  • Add http access configuration in src/main/resources/services/HTTPSandIMAPS-10000001.json

  "@class": "",
  "serviceId": "^(http|https|imaps)://.*",
  "name": "HTTP,HTTPS and IMAPS",
  "id": 10000001,
  "description": "This service definition authorizes all application urls that support HTTP,HTTPS and IMAPS protocols.",
  "evaluationOrder": 10000
  • Add http access configuration in src/main/resources/

    # Set http access
    # json configuration file path for service registration
    # Set security to false
    # *******The setting cannot be correctly resolved when tomcat automatically deploys cas.war*******
    # Open the json file for recognition. The default value is false
CAS Client Deployment
  • The client here is built based on spring MVC. You can build two modules under one project to facilitate subsequent testing of single sign on and logout. Deploy the two modules to tomcat and set different ports, such as http://localhost:18081/ , http://localhost:18082/ At the same time, it can be accessed normally.

  • Add cas client dependency in pom.xml

    <!-- CAS Client related dependencies-->
  • Configure the relevant filters in src/main/webapp/WEB-INF/web.xml

    • Note that localhost cannot be used as the access address of CAS Server and client, otherwise an error will be reported. I understand that it may be from CAS Server
    <!-- Responsible for user authentication, must be enabled -->
        <!-- cas server Server of IP -->
        <!-- cas client Client for IP -->
    <!-- be responsible for Ticket The verification of must be enabled -->
        <filter-name>CAS Validation Filter</filter-name>
        <!-- cas server Server of IP -->
        <!-- cas client Client for IP -->
        <filter-name>CAS Validation Filter</filter-name>
    <!-- The filter is responsible for the implementation HttpServletRequest Requested package, if available SSO Login user name -->
        <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
        <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
        <filter-name>CAS Assertion Thread Local Filter</filter-name>
        <filter-name>CAS Assertion Thread Local Filter</filter-name>
  • After configuration, restart the client to access http://localhost:18081/ At this time, you will be directly redirected to the login authentication page of CAS. Enter the correct user name and password to access the startup page, and then visit again http://localhost:18082/ You will directly access the startup page without logging in, which is single sign on.

  • Single sign out

    • Configure relevant filters and listeners in src/main/webapp/WEB-INF/web.xml
    <!-- Listener and filter for single sign out -->
        <filter-name>CAS Single Sign Out Filter</filter-name>
        <filter-name>CAS Single Sign Out Filter</filter-name>
    • Write the logout method and redirect to the logout method of CAS Server. You can add a suffix service after the redirect url to indicate that you will return to the page specified by the service after logout.
    @RequestMapping(value = "logout", method = {RequestMethod.GET, RequestMethod.POST})
    public String logout (HttpSession session) {
        return "redirect:https://castest:8443/cas/logout?service=https://castest:8443/cas/login";
    • In index.jsp, you can add a login button to obtain the current login user and other information.
    <%@page contentType="text/html; charset=utf-8" language="java" %>>
    <div style="text-align: right">
        <a href="/logout">Logout</a>
    <h2>Hello World!</h2>
    • Restart the client to access http://localhost:18081/ , enter the correct user name and password to access the startup page, and then visit http://localhost:18082/ Then click log out and go back to http://localhost:18081/ The startup page refreshes the interface. At this time, the page will become the login page, that is, if you log out at 18082, then 18081 will also log out, that is, single sign out.

Here, the CAS Client is deployed.

Topics: Java cas sso