http://blog.csdn.net/yunye114105/article/details/7997041
參考:
http://blog.csdn.net/diyagea/article/details/50638737
(配置SSL協議)http://www.cnblogs.com/notDog/p/5264666.html
背景
有幾個相對獨立的java的web應用系統, 各自有自己的登陸驗證功能,用戶在使用不同的系統的時候,需要登陸不同的系統。現在需要提供一個統一的登陸/登出界面, 而不修改各個系統原來的登陸驗證機制。於是采用單點登錄系統CAS。
使用步驟
要使用單點登錄,需要部署CAS系統, CAS服務端可以直接部署在tomcat下運行, 對於CAS服務端來說,所有要集成單點登錄的web應用都是它的一個客戶端, CAS有客戶端jar包, 客戶端web應用需要引入CAS客戶端的jar包,這樣CAS系統的服務端和客戶端web應用程序端才能通信。
客戶端web應用程序的通過配置web.xml,添加CAS需要的各種過濾器,來實現和CAS服務器通信, 用戶信息驗證工作在CAS服務端統一完成, 驗證通過后,客戶端web應用程序只需要補全自己的Session信息即可。
各個客戶端web應用程序需要使用一個公用的用戶表。
第一步 部署CAS系統服務端
1.從官網http://developer.jasig.org/cas/下載CAS Server, 將cas-server-webapp-3.4.12.war解壓, 可以看到是一個標准的java的web應用, 可以直接部署到tomcat的webapps目錄下的,我這里假設部署的路徑是{tomcat_home}/webapps/cas。
2. CAS默認需要tomcat配置SSL協議,使用https協議通信的。 由於項目是企事業單位內部的系統,不需要這么高的安全級別, 可以簡化操作,不走SSL協議。修改下配置文件\WEB-INF\spring-configuration\ticketGrantingTicketCookieGenerator.xml, 如下, 將默認的true改成false即可。
- <bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
- p:cookieSecure="false"
- p:cookieMaxAge="-1"
- p:cookieName="CASTGC"
- p:cookiePath="/cas" />
- <property name="authenticationHandlers">
- <list>
- <!--
- | This is the authentication handler that authenticates services by means of callback via SSL, thereby validating
- | a server side SSL certificate.
- +-->
- <bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
- p:httpClient-ref="httpClient" />
- <!--
- | This is the authentication handler declaration that every CAS deployer will need to change before deploying CAS
- | into production. The default SimpleTestUsernamePasswordAuthenticationHandler authenticates UsernamePasswordCredentials
- | where the username equals the password. You will need to replace this with an AuthenticationHandler that implements your
- | local authentication strategy. You might accomplish this by coding a new such handler and declaring
- | edu.someschool.its.cas.MySpecialHandler here, or you might use one of the handlers provided in the adaptors modules.
- +-->
- <bean class="org.jasig.cas.adaptors.jdbc.SearchModeSearchDatabaseAuthenticationHandler">
<property name="dataSource" ref="ds"/>
<property name="tableUsers" value="user"/>
<property name="fieldUser" value="username"/>
<property name="fieldPassword" value="password"/>
</bean> - <!-- <bean
- class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" /> -->
- <bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">
- <property name="sql" value="select password from userTable where userName=?" />
- <property name="passwordEncoder" ref="passwordEncoder"/>
- <property name="dataSource" ref="dataSource" />
- </bean> -->
- </list>
- </property>
- <bean id="passwordEncoder" class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder" autowire="byName">
- <constructor-arg value="MD5"/>
- </bean>
- <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
- <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
- <property name="driverUrl" value="jdbc:sqlserver://localhost:1433;databaseName=testDB;"></property>
- <property name="user" value="sa"></property>
- <property name="password" value="123456"></property>
- <property name="maximumConnectionCount" value="100"></property>
- <property name="minimumConnectionCount" value="1"></property>
- </bean>
4. 現在服務端就配置好了, 如果需要定制登錄/登出頁面的話(實際項目基本上都需要修改), 修改cas\WEB-INF\view\jsp\default\ui\下面的casLoginView.jsp和casLogoutView.jsp就可以了
第二步 客戶端web應用程序集成CAS
1. 從官網
http://developer.jasig.org/cas-clients/ 下載CAS Client, 將客戶端的jar,如cas-client-core-3.2.1.jar引入到web應用程序的classpath中
2 .配置web.xml文件, 主要是添加過濾器攔截通信, 下面的實例代碼, 假設web應用程序的端口是8080
- <!-- CAS 單點登錄(SSO) 過濾器配置 (start) -->
- <!-- 該過濾器用於實現單點登出功能。-->
- <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>
- <!-- CAS: 用於單點退出 -->
- <listener>
- <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
- </listener>
- <!-- 該過濾器負責用戶的認證工作,必須啟用它 -->
- <filter>
- <filter-name>CASFilter</filter-name>
- <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
- <init-param>
- <param-name>casServerLoginUrl</param-name>
- <!-- 下面的URL是Cas服務器的登錄地址 -->
- <param-value>http://CAS服務端所在服務器IP:8080/cas/login</param-value>
- </init-param>
- <init-param>
- <param-name>serverName</param-name>
- <!-- 下面的URL是具體某一個應用的訪問地址 -->
- <param-value>http://具體web應用程序所在服務器IP:8080</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>CASFilter</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>
- <!-- 下面的URL是Cas服務器的認證地址 -->
- <param-value>http://CAS服務端所在服務器IP:8080/cas</param-value>
- </init-param>
- <init-param>
- <param-name>serverName</param-name>
- <!-- 下面的URL是具體某一個應用的訪問地址 -->
- <param-value>http://具體web應用程序所在服務器IP: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-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>
- <!-- 自動根據單點登錄的結果設置本系統的用戶信息(具體某一個應用實現) -->
- <filter>
- <filter-name>CasForInvokeContextFilter</filter-name>
- <filter-class>com.cm.demo.filter.CasForInvokeContextFilter</filter-class>
- <init-param>
- <param-name>appId</param-name>
- <param-value>a5ea611bbff7474a81753697a1714fb0</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>CasForInvokeContextFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!-- CAS 單點登錄(SSO) 過濾器配置 (end) -->
下面是CasForInvokeContextFilter的一個簡單實現。
- /**
- * 該過濾器用戶從CAS認證服務器中獲取登錄用戶用戶名,並填充必要的Session.
- * @author jiarong_cheng
- * @created 2012-7-12
- */
- public class CasForInvokeContextFilter implements Filter {
- @Override
- public void destroy() {
- }
- @Override
- public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException {
- HttpSession session = ((HttpServletRequest) request).getSession();
- //如果session中沒有用戶信息,則填充用戶信息
- if (session.getAttribute("j_userId") == null) {
- //從Cas服務器獲取登錄賬戶的用戶名
- Assertion assertion = AssertionHolder.getAssertion();
- String userName = assertion.getPrincipal().getName();
- try {
- //根據單點登錄的賬戶的用戶名,從數據庫用戶表查找用戶信息, 填充到session中
- User user = UserDao.getUserByName(userName);
- session.setAttribute("username", userName);
- session.setAttribute("userId", user.getId());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- chain.doFilter(request, response);
- }
- @Override
- public void init(FilterConfig config) throws ServletException {
- }
- }
到此,就完成了, 當你訪問具體應用的網址, 如http://具體應用IP: 8080/ ,就會跳轉到CAS服務器的登陸頁面: http://CAS服務器IP: 8080/ 進行登錄驗證, 驗證通過后, 又會跳轉回應用的網址。