CAS實現單點登陸

CAS介紹

  • 開源的企業級單點登陸解決方案
  • CAS Server 是須要獨立部署的 Web 應用
  • CAS Client 支持很是多的客戶端(這裏指單點登陸系統中的各個 Web 應用),包括 Java, .Net, PHP, Perl, Apache, uPortal, Ruby 等
       

CAS服務部署

  • CAS服務端下載:http://downloads.jasig.org/cas/
  • 解壓zip文件,更名爲cas.war,而後複製到tomcat/webapp目錄下
  • 瀏覽器訪問:http://localhost:8080/cas


CAS-SERVER的默認驗證規則:只要用戶名和密碼相同就認證經過(僅僅用於測試,生成環境須要根據實際狀況修改),輸入admin/admin 點擊登陸,就能夠看到登陸成功的頁面 java

Tomcat配置HTTPS

  • 建立證書

       這裏使用JDK生成的證書,正式環境需購買專業提供商的證書
web

       用JDK自帶的keytool工具生成證書: 瀏覽器

keytool -genkey -alias xiaokaceng -keyalg RSA -keystore d
:/cas/xiaokaceng

注意:127.0.0.1==sso.xiaokaceng.com 在C:\Windows\System32\drivers\etc\hosts配置,只能輸入域名不能輸入IP tomcat

  • 導出證書

keytool -export -file d:/cas/xiaokaceng.crt -alias xiaoka
ceng -keystore d:/cas/xiaokaceng

證書導出完成,可提供JDK使用 session

  • JVM導入證書

keytool -import -keystore D:\JavaDev\jdk1.7\jre\lib\secur
ity\cacerts -file d:\cas\xiaokaceng.crt -alias xiaokaceng

特別提示: app

keytool error: java.io.IOException: Keystore was tampered with, or password was incorrect

那麼請輸入密碼:changeit webapp

  • 應用到Web容器

       啓用Tomcat的SSL,開啓83和87的註釋,配置keystoreFile和keystorePass jsp

<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" keystoreFile="D:/cas/xiaokaceng" keystorePass="123456"/>

注意:keystoreFile和keystorePass有大小寫之分 工具


CAS登陸

  • 啓動兩個tomcat客戶端,修改其端口(tomcat默認自帶的 webapps\examples 做爲演示的簡單web項目)
  • 整合CAS-Client

      CAS-Client 下載地址:http://downloads.jasig.org/cas-clients/,解壓並拷貝相應jar 測試

  


修改web.xml

<!-- ======================== 單點登陸開始 ======================== -->
    <!-- 用於單點退出,該過濾器用於實現單點登出功能,可選配置-->
    <listener>
      <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
    </listener>

    <!-- 該過濾器用於實現單點登出功能,可選配置。 -->
    <filter>
      <filter-name>CAS Single Sign Out Filter</filter-name>
      <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
    </filter>
    <filter-mapping>
      <filter-name>CAS Single Sign Out Filter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
      <filter-name>CAS Filter</filter-name>
      <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
      <init-param>
        <param-name>casServerLoginUrl</param-name>
        <param-value>https://sso.xiaokaceng.com:8443/cas/login</param-value>
      </init-param>
      <init-param>
        <param-name>serverName</param-name>
        <param-value>http://localhost:18080</param-value>
      </init-param>
    </filter>
    <filter-mapping>
      <filter-name>CAS Filter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- 該過濾器負責對Ticket的校驗工做,必須啓用它 -->
    <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://sso.xiaokaceng.com:8443/cas</param-value>
      </init-param>
      <init-param>
        <param-name>serverName</param-name>
        <param-value>http://localhost:18080</param-value>
      </init-param>
    </filter>
    <filter-mapping>
      <filter-name>CAS Validation Filter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--
      該過濾器負責實現HttpServletRequest請求的包裹,
      好比容許開發者經過HttpServletRequest的getRemoteUser()方法得到SSO登陸用戶的登陸名,可選配置。
    -->
    <filter>
      <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
      <filter-class>
        org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>
    </filter>
    <filter-mapping>
      <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--
    該過濾器使得開發者能夠經過org.jasig.cas.client.util.AssertionHolder來獲取用戶的登陸名。
    好比AssertionHolder.getAssertion().getPrincipal().getName()。
    -->
    <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 Assertion Thread Local Filter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- ======================== 單點登陸結束 ======================== -->


測試驗證,訪問http://localhost:18080/examples/servlets/servlet/HelloWorldExample


CAS登出

退出連接爲:https://sso.xiaokaceng.com:8443/cas/logout


CAS服務界面

  • 登陸界面:casLoginView.jsp
  • 登陸成功:casGenericSuccess.jsp
  • 登出界面:casLogoutView.jsp


CAS實現單點登陸其實不難!
相關文章
相關標籤/搜索