spring security cas单点登录

cas服务端和cas客户端配好,访问cas服务端可以登录,访问客户端应用资源的时候出现拒绝访问问题 

首次登录直接出现拒绝访问 


cas服务端配置: 
cas.properties 

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



deployerConfigContext.xml 

Java代码    收藏代码
  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.   
  47.   
  48. <property name="credentialsToPrincipalResolvers">   
  49. <list>   
  50. <!--   
  51. | UsernamePasswordCredentialsToPrincipalResolver supports the UsernamePasswordCredentials that we use for /login   
  52. | by default and produces SimplePrincipal instances conveying the username from the credentials.   
  53. |   
  54. | If you've changed your LoginFormAction to use credentials other than UsernamePasswordCredentials then you will also   
  55. | need to change this bean declaration (or add additional declarations) to declare a CredentialsToPrincipalResolver that supports the   
  56. | Credentials you are using.   
  57. +-->   
  58. <bean   
  59. class="org.jasig.cas.authentication.principal.UsernamePasswordCredentialsToPrincipalResolver">  
  60. <!--增加此属性,为认证过的用户的Principal添加属性-->   
  61. <property name="attributeRepository" ref="attributeRepository"></property>   
  62. </bean>   
  63. <!--   
  64. | HttpBasedServiceCredentialsToPrincipalResolver supports HttpBasedCredentials.  It supports the CAS 2.0 approach of   
  65. | authenticating services by SSL callback, extracting the callback URL from the Credentials and representing it as a   
  66. | SimpleService identified by that callback URL.   
  67. |   
  68. | If you are representing services by something more or other than an HTTPS URL whereat they are able to   
  69. | receive a proxy callback, you will need to change this bean declaration (or add additional declarations).   
  70. +-->   
  71. <bean   
  72. class="org.jasig.cas.authentication.principal.HttpBasedServiceCredentialsToPrincipalResolver" />   
  73. </list>   
  74. </property>   
  75.   
  76. <!--   
  77. | Whereas CredentialsToPrincipalResolvers identify who it is some Credentials might authenticate,   
  78. | AuthenticationHandlers actually authenticate credentials.  Here we declare the AuthenticationHandlers that   
  79. | authenticate the Principals that the CredentialsToPrincipalResolvers identified.  CAS will try these handlers in turn   
  80. | until it finds one that both supports the Credentials presented and succeeds in authenticating.   
  81. +-->   
  82.   
  83.   
  84.   
  85. <property name="authenticationHandlers">   
  86. <list>   
  87. <!--   
  88. | This is the authentication handler that authenticates services by means of callback via SSL, thereby validating   
  89. | a server side SSL certificate.   
  90. +-->   
  91. <bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"  
  92. p:httpClient-ref="httpClient" />   
  93. <!--   
  94. | This is the authentication handler declaration that every CAS deployer will need to change before deploying CAS   
  95. | into production.  The default SimpleTestUsernamePasswordAuthenticationHandler authenticates UsernamePasswordCredentials   
  96. | where the username equals the password.  You will need to replace this with an AuthenticationHandler that implements your   
  97. | local authentication strategy.  You might accomplish this by coding a new such handler and declaring   
  98. | edu.someschool.its.cas.MySpecialHandler here, or you might use one of the handlers provided in the adaptors modules.   
  99. +-->   
  100.   
  101. <!--   
  102. <bean   
  103. class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" />   
  104. -->   
  105.   
  106. <!-- 数据库认证.wucht-->   
  107.   
  108. <bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">   
  109. <property name="dataSource" ref="dataSource" />   
  110. <property name="sql" value="select password from users where name=?" />   
  111. </bean>   
  112.   
  113. </list>   
  114. </property>   
  115. </bean>   
  116.   
  117.   
  118. <!-- DATABASE 增加数据源配置 -->   
  119. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">   
  120. <property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>   
  121. <property name="url"><value>jdbc:mysql://localhost:3306/mysql?useUnicode=true&amp;characterEncoding=utf-8</value></property>   
  122. <property name="username"><value>root</value></property>   
  123. <property name="password"><value>root</value></property>   
  124. </bean>   
  125.   
  126. <!--   
  127. This bean defines the security roles for the Services Management application.  Simple deployments can use the in-memory version.   
  128. More robust deployments will want to use another option, such as the Jdbc version.   
  129.   
  130. The name of this should remain "userDetailsService" in order for Spring Security to find it.   
  131. -->   
  132.     <!-- <sec:user name="@@THIS SHOULD BE REPLACED@@" password="notused" authorities="ROLE_ADMIN" />-->   
  133.   
  134.     <sec:user-service id="userDetailsService">   
  135.         <sec:user name="@@THIS SHOULD BE REPLACED@@" password="notused" authorities="ROLE_ADMIN" />   
  136.     </sec:user-service>   
  137.   
  138. <!--   
  139. Bean that defines the attributes that a service may return.  This example uses the Stub/Mock version.  A real implementation   
  140. may go against a database or LDAP server.  The id should remain "attributeRepository" though.   
  141. -->   
  142.   
  143. <!--   
  144. <bean id="attributeRepository"   
  145. class="org.jasig.services.persondir.support.StubPersonAttributeDao">   
  146. <property name="backingMap">   
  147. <map>   
  148. <entry key="uid" value="uid" />   
  149. <entry key="eduPersonAffiliation" value="eduPersonAffiliation" />   
  150. <entry key="groupMembership" value="groupMembership" />   
  151. </map>   
  152. </property>   
  153. </bean>   
  154. -->   
  155.   
  156. <!-- 使用SingleRowJdbcPersonAttributeDao 获取更多用户的信息 -->   
  157. <bean id="attributeRepository" class="org.jasig.services.persondir.support.jdbc.SingleRowJdbcPersonAttributeDao">   
  158. <constructor-arg index="0" ref="dataSource"/>   
  159. <constructor-arg index="1" value="select role_name from role where login_name = ?"/>   
  160. <!--这里的key需写username,value对应数据库用户名字段 -->   
  161. <property name="queryAttributeMapping">   
  162. <map>   
  163. <entry key="username" value="login_name"/>   
  164. </map>   
  165. </property>   
  166. <!--key对应数据库字段,value对应客户端获取参数 -->   
  167. <!-- 返回数据认证后的数据 -->   
  168. <property name="resultAttributeMapping">   
  169. <map>   
  170. <!--这个从数据库中获取的角色,用于在应用中security的权限验证-->   
  171. <entry key="role_name" value="authorities"/>   
  172. </map>   
  173. </property>   
  174. </bean>   
  175.   
  176.   
  177.   
  178. <!--   
  179. Sample, in-memory data store for the ServiceRegistry. A real implementation   
  180. would probably want to replace this with the JPA-backed ServiceRegistry DAO   
  181. The name of this bean should remain "serviceRegistryDao".   
  182. -->   
  183. <bean   
  184. id="serviceRegistryDao"   
  185.         class="org.jasig.cas.services.InMemoryServiceRegistryDaoImpl">   
  186.           
  187.         <!--   
  188.             <property name="registeredServices">   
  189.                 <list>   
  190.                     <bean class="org.jasig.cas.services.RegisteredServiceImpl">   
  191.                         <property name="id" value="0" />   
  192.                         <property name="name" value="HTTP" />   
  193.                         <property name="description" value="Only Allows HTTP Urls" />   
  194.                         <property name="serviceId" value="http://**" />   
  195.                         <property name="evaluationOrder" value="10000001" />   
  196.                     </bean>   
  197.   
  198.                     <bean class="org.jasig.cas.services.RegisteredServiceImpl">   
  199.                         <property name="id" value="1" />   
  200.                         <property name="name" value="HTTPS" />   
  201.                         <property name="description" value="Only Allows HTTPS Urls" />   
  202.                         <property name="serviceId" value="https://**" />   
  203.                         <property name="evaluationOrder" value="10000002" />   
  204.                     </bean>   
  205.   
  206.                     <bean class="org.jasig.cas.services.RegisteredServiceImpl">   
  207.                         <property name="id" value="2" />   
  208.                         <property name="name" value="IMAPS" />   
  209.                         <property name="description" value="Only Allows HTTPS Urls" />   
  210.                         <property name="serviceId" value="imaps://**" />   
  211.                         <property name="evaluationOrder" value="10000003" />   
  212.                     </bean>   
  213.   
  214.                     <bean class="org.jasig.cas.services.RegisteredServiceImpl">   
  215.                         <property name="id" value="3" />   
  216.                         <property name="name" value="IMAP" />   
  217.                         <property name="description" value="Only Allows IMAP Urls" />   
  218.                         <property name="serviceId" value="imap://**" />   
  219.                         <property name="evaluationOrder" value="10000004" />   
  220.                     </bean>   
  221.                 </list>   
  222.             </property>   
  223.             -->   
  224.              
  225.         </bean>   
  226.   
  227.     <bean id="auditTrailManager" class="com.github.inspektr.audit.support.Slf4jLoggingAuditTrailManager" />   
  228. </beans>   



spring配置 

Ruby代码    收藏代码
  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.     <!--     
  12.         entry-point-ref="casEntryPoint"作用是认证的入口,是一个实现AuthenticationEntryPoint接口的类    
  13.         ,为ExceptionTranslationFilter类提供认证依据,    
  14.         <custom-filter position="FORM_LOGIN_FILTER" ref="casFilter"/> 使用自定义的Filter,放置在过滤器链的FORM_LOGIN_FILTER的位置    
  15.             
  16.         casEntryPoint只是提供认证入口的作用,当没有权限,将跳转到该地址。     
  17.         casFilter是处理CAS service ticket的,当无权访问时,会使用casEntryPoint提供认证入口    
  18.     -->    
  19.     <http auto-config="true" entry-point-ref="casEntryPoint"    
  20.         access-denied-page="/403.jsp">    
  21.             
  22.         <intercept-url pattern="/**" access="ROLE_USER" />    
  23. <!--     ROLE_ADMIN-->    
  24.             
  25.         <!-- logout-success-url="/login.html" -->    
  26.         <!-- 注销时需要先注销应用程序,再注销cas中心认证服务 -->    
  27.         <logout logout-url="/logout.html"    
  28.             success-handler-ref="casLogoutSuccessHandler" />    
  29.         <custom-filter position="CAS_FILTER" ref="casFilter" />    
  30.     </http>    
  31.     
  32.     <authentication-manager alias="authenticationManager">    
  33.         <authentication-provider ref="casAuthenticationProvider" />    
  34.     </authentication-manager>    
  35.     
  36.     <!-- cas中心认证服务入口 -->    
  37.     <beans:bean id="casEntryPoint"    
  38.         class="org.springframework.security.cas.web.CasAuthenticationEntryPoint">    
  39.         <beans:property name="loginUrl"    
  40.             value="http://localhost:8080/casServer/login" />    
  41.         <beans:property name="serviceProperties"    
  42.             ref="serviceProperties" />    
  43.     </beans:bean>    
  44.     
  45.     
  46.     <!-- cas中心认证服务配置 -->    
  47.     <beans:bean id="serviceProperties"    
  48.         class="org.springframework.security.cas.ServiceProperties">    
  49.         <beans:property name="service"    
  50.             value="http://localhost:8080/Cas_Client/j_acegi_cas_security_check" />    
  51.         <beans:property name="sendRenew" value="false" />    
  52.     </beans:bean>    
  53.     
  54.     <!-- CAS service ticket(中心认证服务凭据)验证 -->    
  55.     <beans:bean id="casFilter"    
  56.         class="org.springframework.security.cas.web.CasAuthenticationFilter">    
  57.         <beans:property name="authenticationManager"    
  58.             ref="authenticationManager" />    
  59. <!--     <beans:property name="authenticationFailureHandler">-->    
  60. <!--         <beans:bean-->    
  61. <!--             class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">-->    
  62. <!--             <beans:property name="defaultFailureUrl"-->    
  63. <!--                 value="/logout.html" />-->    
  64. <!--         </beans:bean>-->    
  65. <!--     </beans:property>-->    
  66. <!--      登录成功后的页面,如果是固定的。否则 ref="authenticationSuccessHandler" -->    
  67. <!--     <beans:property name="authenticationSuccessHandler">-->    
  68. <!--         <beans:bean-->    
  69. <!--             class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler">-->    
  70. <!--             <beans:property name="defaultTargetUrl"-->    
  71. <!--                 value="/index.jsp" />-->    
  72. <!--         </beans:bean>-->    
  73. <!--     </beans:property>-->    
  74.     </beans:bean>    
  75.     
  76.     
  77.     
  78.     <!-- 从Cas Server得到用户信息 -->    
  79.         <beans:bean id="authenticationUserDetailsService"    
  80.             class="org.springframework.security.cas.userdetails.GrantedAuthorityFromAssertionAttributesUserDetailsService">    
  81.             <beans:constructor-arg>    
  82.                 <beans:array>    
  83.                     <beans:value>authorities</beans:value>    
  84.                 </beans:array>    
  85.             </beans:constructor-arg>    
  86.         </beans:bean>    
  87.     
  88.     
  89.     <beans:bean id="userDetailsService"    
  90.         class="com.reportstart.security.service.impl.BocUserDetaislServiceImpl">    
  91. <!--     <beans:property name="userDao">-->    
  92. <!--         <beans:ref bean="userDao" />-->    
  93. <!--     </beans:property>-->    
  94.     </beans:bean>    
  95.     
  96. <!-- <beans:bean id="authenticationUserDetailsService"-->    
  97. <!--     class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">-->    
  98. <!--     <beans:property name="userDetailsService">-->    
  99. <!--         <beans:ref local="userDetailsService" />-->    
  100. <!--     </beans:property>-->    
  101. <!-- </beans:bean>-->    
  102.     
  103.     <beans:bean id="casAuthenticationProvider"    
  104.         class="org.springframework.security.cas.authentication.CasAuthenticationProvider">    
  105.         <!-- 使用自定义service获取用户信息 -->    
  106. <!--     <beans:property name="authenticationUserDetailsService"-->    
  107. <!--         ref="casAuthenticationUserDetailsService" />-->    
  108.     
  109.     
  110.         <!-- 通过Cas Server获取用户信息 -->    
  111.                 <beans:property name="authenticationUserDetailsService"    
  112.                     ref="authenticationUserDetailsService" />    
  113.     
  114.         <beans:property name="serviceProperties"    
  115.             ref="serviceProperties" />    
  116.         <beans:property name="ticketValidator">    
  117.             <beans:bean    
  118.                 class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator">    
  119.                 <beans:constructor-arg index="0"    
  120.                     value="http://localhost:8080/casServer" />    
  121.             </beans:bean>    
  122.         </beans:property>    
  123.         <!-- 自定义cas客户端应用标示.wucht.2012-6-4(每个cas客户端都需要一个key标示用于区分不同cas客户端) -->    
  124.         <beans:property name="key"    
  125.             value="Cas_Client" />    
  126.     </beans:bean>    
  127.     
  128.     <!-- 注销 -->    
  129.     <beans:bean id="casLogoutSuccessHandler"    
  130.         class="com.wucht.test.CasLogoutSuccessHandler">    
  131.     </beans:bean>    
  132.     
  133. </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

猜你喜欢

转载自qian0021514578.iteye.com/blog/2205688