天天看點

spring security cas單點登入

cas服務端和cas用戶端配好,通路cas服務端可以登入,通路用戶端應用資源的時候出現拒絕通路問題 

首次登入直接出現拒絕通路 

cas服務端配置: 

cas.properties 

Java代碼   

spring security cas單點登入
  1. #server.prefix=http://localhost:8080/cas   
  2. #server.prefix=http://cas.wucht.com:8080/casServer   
  3. server.prefix=http://localhost:8080/casServer   
  4. cas.securityContext.serviceProperties.service=${server.prefix}/j_acegi_cas_security_check  
  5. # Names of roles allowed to access the CAS service manager   
  6. cas.securityContext.serviceProperties.adminRoles=ROLE_ADMIN   
  7. cas.securityContext.casProcessingFilterEntryPoint.loginUrl=${server.prefix}/login   
  8. cas.securityContext.ticketValidator.casServerUrlPrefix=${server.prefix}   
  9. cas.themeResolver.defaultThemeName=cas-theme-default   
  10. #cas.themeResolver.defaultThemeName=default   
  11. cas.viewResolver.basename=default_views   
  12. #host.name=cas   
  13. host.name=casServer   
  14. #database.hibernate.dialect=org.hibernate.dialect.OracleDialect   
  15. database.hibernate.dialect=org.hibernate.dialect.MySQLDialect   
  16. #database.hibernate.dialect=org.hibernate.dialect.HSQLDialect   

deployerConfigContext.xml 

Java代碼   

spring security cas單點登入
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <!--   
  3. | deployerConfigContext.xml centralizes into one file some of the declarative configuration that   
  4. | all CAS deployers will need to modify.   
  5. |   
  6. | This file declares some of the Spring-managed JavaBeans that make up a CAS deployment.    
  7. | The beans declared in this file are instantiated at context initialization time by the Spring   
  8. | ContextLoaderListener declared in web.xml.  It finds this file because this   
  9. | file is among those declared in the context parameter "contextConfigLocation".   
  10. |   
  11. | By far the most common change you will need to make in this file is to change the last bean   
  12. | declaration to replace the default SimpleTestUsernamePasswordAuthenticationHandler with   
  13. | one implementing your approach for authenticating usernames and passwords.   
  14. +-->   
  15. <beans xmlns="http://www.springframework.org/schema/beans"   
  16.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  17.        xmlns:p="http://www.springframework.org/schema/p"   
  18.        xmlns:sec="http://www.springframework.org/schema/security"   
  19.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  20.        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">   
  21. <!--   
  22. | This bean declares our AuthenticationManager.  The CentralAuthenticationService service bean   
  23. | declared in applicationContext.xml picks up this AuthenticationManager by reference to its id,   
  24. | "authenticationManager".  Most deployers will be able to use the default AuthenticationManager   
  25. | implementation and so do not need to change the class of this bean.  We include the whole   
  26. | AuthenticationManager here in the userConfigContext.xml so that you can see the things you will   
  27. | need to change in context.   
  28. +-->   
  29. <bean id="authenticationManager"   
  30. class="org.jasig.cas.authentication.AuthenticationManagerImpl">   
  31. <!--   
  32. | This is the List of CredentialToPrincipalResolvers that identify what Principal is trying to authenticate.   
  33. | The AuthenticationManagerImpl considers them in order, finding a CredentialToPrincipalResolver which   
  34. | supports the presented credentials.   
  35. |   
  36. | AuthenticationManagerImpl uses these resolvers for two purposes.  First, it uses them to identify the Principal   
  37. | attempting to authenticate to CAS /login .  In the default configuration, it is the DefaultCredentialsToPrincipalResolver   
  38. | that fills this role.  If you are using some other kind of credentials than UsernamePasswordCredentials, you will need to replace   
  39. | DefaultCredentialsToPrincipalResolver with a CredentialsToPrincipalResolver that supports the credentials you are   
  40. | using.   
  41. |   
  42. | Second, AuthenticationManagerImpl uses these resolvers to identify a service requesting a proxy granting ticket.   
  43. | In the default configuration, it is the HttpBasedServiceCredentialsToPrincipalResolver that serves this purpose.   
  44. | You will need to change this list if you are identifying services by something more or other than their callback URL.   
  45. +-->   
  46. <property name="credentialsToPrincipalResolvers">   
  47. <list>   
  48. <!--   
  49. | UsernamePasswordCredentialsToPrincipalResolver supports the UsernamePasswordCredentials that we use for /login   
  50. | by default and produces SimplePrincipal instances conveying the username from the credentials.   
  51. |   
  52. | If you've changed your LoginFormAction to use credentials other than UsernamePasswordCredentials then you will also   
  53. | need to change this bean declaration (or add additional declarations) to declare a CredentialsToPrincipalResolver that supports the   
  54. | Credentials you are using.   
  55. +-->   
  56. <bean   
  57. class="org.jasig.cas.authentication.principal.UsernamePasswordCredentialsToPrincipalResolver">  
  58. <!--增加此屬性,為認證過的使用者的Principal添加屬性-->   
  59. <property name="attributeRepository" ref="attributeRepository"></property>   
  60. </bean>   
  61. <!--   
  62. | HttpBasedServiceCredentialsToPrincipalResolver supports HttpBasedCredentials.  It supports the CAS 2.0 approach of   
  63. | authenticating services by SSL callback, extracting the callback URL from the Credentials and representing it as a   
  64. | SimpleService identified by that callback URL.   
  65. |   
  66. | If you are representing services by something more or other than an HTTPS URL whereat they are able to   
  67. | receive a proxy callback, you will need to change this bean declaration (or add additional declarations).   
  68. +-->   
  69. <bean   
  70. class="org.jasig.cas.authentication.principal.HttpBasedServiceCredentialsToPrincipalResolver" />   
  71. </list>   
  72. </property>   
  73. <!--   
  74. | Whereas CredentialsToPrincipalResolvers identify who it is some Credentials might authenticate,   
  75. | AuthenticationHandlers actually authenticate credentials.  Here we declare the AuthenticationHandlers that   
  76. | authenticate the Principals that the CredentialsToPrincipalResolvers identified.  CAS will try these handlers in turn   
  77. | until it finds one that both supports the Credentials presented and succeeds in authenticating.   
  78. +-->   
  79. <property name="authenticationHandlers">   
  80. <list>   
  81. <!--   
  82. | This is the authentication handler that authenticates services by means of callback via SSL, thereby validating   
  83. | a server side SSL certificate.   
  84. +-->   
  85. <bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"  
  86. p:httpClient-ref="httpClient" />   
  87. <!--   
  88. | This is the authentication handler declaration that every CAS deployer will need to change before deploying CAS   
  89. | into production.  The default SimpleTestUsernamePasswordAuthenticationHandler authenticates UsernamePasswordCredentials   
  90. | where the username equals the password.  You will need to replace this with an AuthenticationHandler that implements your   
  91. | local authentication strategy.  You might accomplish this by coding a new such handler and declaring   
  92. | edu.someschool.its.cas.MySpecialHandler here, or you might use one of the handlers provided in the adaptors modules.   
  93. +-->   
  94. <!--   
  95. <bean   
  96. class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" />   
  97. -->   
  98. <!-- 資料庫認證.wucht-->   
  99. <bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">   
  100. <property name="dataSource" ref="dataSource" />   
  101. <property name="sql" value="select password from users where name=?" />   
  102. </bean>   
  103. </list>   
  104. </property>   
  105. </bean>   
  106. <!-- DATABASE 增加資料源配置 -->   
  107. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">   
  108. <property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>   
  109. <property name="url"><value>jdbc:mysql://localhost:3306/mysql?useUnicode=true&amp;characterEncoding=utf-8</value></property>   
  110. <property name="username"><value>root</value></property>   
  111. <property name="password"><value>root</value></property>   
  112. </bean>   
  113. <!--   
  114. This bean defines the security roles for the Services Management application.  Simple deployments can use the in-memory version.   
  115. More robust deployments will want to use another option, such as the Jdbc version.   
  116. The name of this should remain "userDetailsService" in order for Spring Security to find it.   
  117. -->   
  118.     <!-- <sec:user name="@@THIS SHOULD BE [email protected]@" password="notused" authorities="ROLE_ADMIN" />-->   
  119.     <sec:user-service id="userDetailsService">   
  120.         <sec:user name="@@THIS SHOULD BE [email protected]@" password="notused" authorities="ROLE_ADMIN" />   
  121.     </sec:user-service>   
  122. <!--   
  123. Bean that defines the attributes that a service may return.  This example uses the Stub/Mock version.  A real implementation   
  124. may go against a database or LDAP server.  The id should remain "attributeRepository" though.   
  125. -->   
  126. <!--   
  127. <bean id="attributeRepository"   
  128. class="org.jasig.services.persondir.support.StubPersonAttributeDao">   
  129. <property name="backingMap">   
  130. <map>   
  131. <entry key="uid" value="uid" />   
  132. <entry key="eduPersonAffiliation" value="eduPersonAffiliation" />   
  133. <entry key="groupMembership" value="groupMembership" />   
  134. </map>   
  135. </property>   
  136. </bean>   
  137. -->   
  138. <!-- 使用SingleRowJdbcPersonAttributeDao 擷取更多使用者的資訊 -->   
  139. <bean id="attributeRepository" class="org.jasig.services.persondir.support.jdbc.SingleRowJdbcPersonAttributeDao">   
  140. <constructor-arg index="0" ref="dataSource"/>   
  141. <constructor-arg index="1" value="select role_name from role where login_name = ?"/>   
  142. <!--這裡的key需寫username,value對應資料庫使用者名字段 -->   
  143. <property name="queryAttributeMapping">   
  144. <map>   
  145. <entry key="username" value="login_name"/>   
  146. </map>   
  147. </property>   
  148. <!--key對應資料庫字段,value對應用戶端擷取參數 -->   
  149. <!-- 傳回資料認證後的資料 -->   
  150. <property name="resultAttributeMapping">   
  151. <map>   
  152. <!--這個從資料庫中擷取的角色,用于在應用中security的權限驗證-->   
  153. <entry key="role_name" value="authorities"/>   
  154. </map>   
  155. </property>   
  156. </bean>   
  157. <!--   
  158. Sample, in-memory data store for the ServiceRegistry. A real implementation   
  159. would probably want to replace this with the JPA-backed ServiceRegistry DAO   
  160. The name of this bean should remain "serviceRegistryDao".   
  161. -->   
  162. <bean   
  163. id="serviceRegistryDao"   
  164.         class="org.jasig.cas.services.InMemoryServiceRegistryDaoImpl">   
  165.         <!--   
  166.             <property name="registeredServices">   
  167.                 <list>   
  168.                     <bean class="org.jasig.cas.services.RegisteredServiceImpl">   
  169.                         <property name="id" value="0" />   
  170.                         <property name="name" value="HTTP" />   
  171.                         <property name="description" value="Only Allows HTTP Urls" />   
  172.                         <property name="serviceId" value="http://**" />   
  173.                         <property name="evaluationOrder" value="10000001" />   
  174.                     </bean>   
  175.                     <bean class="org.jasig.cas.services.RegisteredServiceImpl">   
  176.                         <property name="id" value="1" />   
  177.                         <property name="name" value="HTTPS" />   
  178.                         <property name="description" value="Only Allows HTTPS Urls" />   
  179.                         <property name="serviceId" value="https://**" />   
  180.                         <property name="evaluationOrder" value="10000002" />   
  181.                     </bean>   
  182.                     <bean class="org.jasig.cas.services.RegisteredServiceImpl">   
  183.                         <property name="id" value="2" />   
  184.                         <property name="name" value="IMAPS" />   
  185.                         <property name="description" value="Only Allows HTTPS Urls" />   
  186.                         <property name="serviceId" value="imaps://**" />   
  187.                         <property name="evaluationOrder" value="10000003" />   
  188.                     </bean>   
  189.                     <bean class="org.jasig.cas.services.RegisteredServiceImpl">   
  190.                         <property name="id" value="3" />   
  191.                         <property name="name" value="IMAP" />   
  192.                         <property name="description" value="Only Allows IMAP Urls" />   
  193.                         <property name="serviceId" value="imap://**" />   
  194.                         <property name="evaluationOrder" value="10000004" />   
  195.                     </bean>   
  196.                 </list>   
  197.             </property>   
  198.             -->   
  199.         </bean>   
  200.     <bean id="auditTrailManager" class="com.github.inspektr.audit.support.Slf4jLoggingAuditTrailManager" />   
  201. </beans>   

spring配置 

Ruby代碼   

spring security cas單點登入
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <beans:beans xmlns="http://www.springframework.org/schema/security"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  4.     xmlns:p="http://www.springframework.org/schema/p"    
  5.     xmlns:beans="http://www.springframework.org/schema/beans"    
  6.     xsi:schemaLocation="    
  7.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
  8.             http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"    
  9.     default-lazy-init="true">    
  10.     <!--     
  11.         entry-point-ref="casEntryPoint"作用是認證的入口,是一個實作AuthenticationEntryPoint接口的類    
  12.         ,為ExceptionTranslationFilter類提供認證依據,    
  13.         <custom-filter position="FORM_LOGIN_FILTER" ref="casFilter"/> 使用自定義的Filter,放置在過濾器鍊的FORM_LOGIN_FILTER的位置    
  14.         casEntryPoint隻是提供認證入口的作用,當沒有權限,将跳轉到該位址。     
  15.         casFilter是處理CAS service ticket的,當無權通路時,會使用casEntryPoint提供認證入口    
  16.     -->    
  17.     <http auto-config="true" entry-point-ref="casEntryPoint"    
  18.         access-denied-page="/403.jsp">    
  19.         <intercept-url pattern="/**" access="ROLE_USER" />    
  20. <!--     ROLE_ADMIN-->    
  21.         <!-- logout-success-url="/login.html" -->    
  22.         <!-- 登出時需要先登出應用程式,再登出cas中心認證服務 -->    
  23.         <logout logout-url="/logout.html"    
  24.             success-handler-ref="casLogoutSuccessHandler" />    
  25.         <custom-filter position="CAS_FILTER" ref="casFilter" />    
  26.     </http>    
  27.     <authentication-manager alias="authenticationManager">    
  28.         <authentication-provider ref="casAuthenticationProvider" />    
  29.     </authentication-manager>    
  30.     <!-- cas中心認證服務入口 -->    
  31.     <beans:bean id="casEntryPoint"    
  32.         class="org.springframework.security.cas.web.CasAuthenticationEntryPoint">    
  33.         <beans:property name="loginUrl"    
  34.             value="http://localhost:8080/casServer/login" />    
  35.         <beans:property name="serviceProperties"    
  36.             ref="serviceProperties" />    
  37.     </beans:bean>    
  38.     <!-- cas中心認證服務配置 -->    
  39.     <beans:bean id="serviceProperties"    
  40.         class="org.springframework.security.cas.ServiceProperties">    
  41.         <beans:property name="service"    
  42.             value="http://localhost:8080/Cas_Client/j_acegi_cas_security_check" />    
  43.         <beans:property name="sendRenew" value="false" />    
  44.     </beans:bean>    
  45.     <!-- CAS service ticket(中心認證服務憑據)驗證 -->    
  46.     <beans:bean id="casFilter"    
  47.         class="org.springframework.security.cas.web.CasAuthenticationFilter">    
  48.         <beans:property name="authenticationManager"    
  49.             ref="authenticationManager" />    
  50. <!--     <beans:property name="authenticationFailureHandler">-->    
  51. <!--         <beans:bean-->    
  52. <!--             class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">-->    
  53. <!--             <beans:property name="defaultFailureUrl"-->    
  54. <!--                 value="/logout.html" />-->    
  55. <!--         </beans:bean>-->    
  56. <!--     </beans:property>-->    
  57. <!--      登入成功後的頁面,如果是固定的。否則 ref="authenticationSuccessHandler" -->    
  58. <!--     <beans:property name="authenticationSuccessHandler">-->    
  59. <!--         <beans:bean-->    
  60. <!--             class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler">-->    
  61. <!--             <beans:property name="defaultTargetUrl"-->    
  62. <!--                 value="/index.jsp" />-->    
  63. <!--         </beans:bean>-->    
  64. <!--     </beans:property>-->    
  65.     </beans:bean>    
  66.     <!-- 從Cas Server得到使用者資訊 -->    
  67.         <beans:bean id="authenticationUserDetailsService"    
  68.             class="org.springframework.security.cas.userdetails.GrantedAuthorityFromAssertionAttributesUserDetailsService">    
  69.             <beans:constructor-arg>    
  70.                 <beans:array>    
  71.                     <beans:value>authorities</beans:value>    
  72.                 </beans:array>    
  73.             </beans:constructor-arg>    
  74.         </beans:bean>    
  75.     <beans:bean id="userDetailsService"    
  76.         class="com.reportstart.security.service.impl.BocUserDetaislServiceImpl">    
  77. <!--     <beans:property name="userDao">-->    
  78. <!--         <beans:ref bean="userDao" />-->    
  79. <!--     </beans:property>-->    
  80.     </beans:bean>    
  81. <!-- <beans:bean id="authenticationUserDetailsService"-->    
  82. <!--     class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">-->    
  83. <!--     <beans:property name="userDetailsService">-->    
  84. <!--         <beans:ref local="userDetailsService" />-->    
  85. <!--     </beans:property>-->    
  86. <!-- </beans:bean>-->    
  87.     <beans:bean id="casAuthenticationProvider"    
  88.         class="org.springframework.security.cas.authentication.CasAuthenticationProvider">    
  89.         <!-- 使用自定義service擷取使用者資訊 -->    
  90. <!--     <beans:property name="authenticationUserDetailsService"-->    
  91. <!--         ref="casAuthenticationUserDetailsService" />-->    
  92.         <!-- 通過Cas Server擷取使用者資訊 -->    
  93.                 <beans:property name="authenticationUserDetailsService"    
  94.                     ref="authenticationUserDetailsService" />    
  95.         <beans:property name="serviceProperties"    
  96.             ref="serviceProperties" />    
  97.         <beans:property name="ticketValidator">    
  98.             <beans:bean    
  99.                 class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator">    
  100.                 <beans:constructor-arg index="0"    
  101.                     value="http://localhost:8080/casServer" />    
  102.             </beans:bean>    
  103.         </beans:property>    
  104.         <!-- 自定義cas用戶端應用标示.wucht.2012-6-4(每個cas用戶端都需要一個key标示用于區分不同cas用戶端) -->    
  105.         <beans:property name="key"    
  106.             value="Cas_Client" />    
  107.     </beans:bean>    
  108.     <!-- 登出 -->    
  109.     <beans:bean id="casLogoutSuccessHandler"    
  110.         class="com.wucht.test.CasLogoutSuccessHandler">    
  111.     </beans:bean>    
  112. </beans:beans>    

Spring Security與CAS內建,第一次通路用戶端頁面,出現異常是正常的,因為Spring Security有個異常攔截的filter攔截到通路拒絕異常,才會跳轉到入口點entry-point的(即:CAS 服務端登入界面)。 

在CAS服務端登入界面輸入賬号、密碼後,無法跳轉到用戶端,而背景列印的日志已經清楚說明了 

org.springframework.security.web.access.ExceptionTranslationFilter:165 - Access is denied (user is anonymous); redirecting to authentication entry point 

登入賬号屬于anonymous