cas 集成springsecurity
时间: 2023-09-04 22:09:31 浏览: 259
要将CAS集成到Spring Security中,需要遵循以下步骤:
1. 在pom.xml中添加依赖项:
```
<dependency>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-support-security</artifactId>
<version>${cas.version}</version>
</dependency>
```
2. 在Spring Security的配置文件中添加以下内容:
```
<security:http entry-point-ref="casEntryPoint">
<security:intercept-url pattern="/secure/**" access="ROLE_USER"/>
<security:custom-filter ref="casAuthenticationFilter" after="CAS_FILTER"/>
</security:http>
<bean id="casEntryPoint"
class="org.springframework.security.cas.web.CasAuthenticationEntryPoint">
<property name="loginUrl" value="https://CAS_SERVER/login"/>
<property name="serviceProperties" ref="serviceProperties"/>
</bean>
<bean id="serviceProperties" class="org.springframework.security.cas.ServiceProperties">
<property name="service" value="https://${APP_HOST}:${APP_PORT}/j_spring_cas_security_check"/>
<property name="sendRenew" value="false"/>
</bean>
<bean id="casAuthenticationFilter"
class="org.springframework.security.cas.web.CasAuthenticationFilter">
<property name="authenticationManager" ref="authenticationManager"/>
</bean>
<security:authentication-manager alias="authenticationManager">
<security:authentication-provider ref="casAuthenticationProvider"/>
</security:authentication-manager>
<bean id="casAuthenticationProvider"
class="org.springframework.security.cas.authentication.CasAuthenticationProvider">
<property name="userDetailsService" ref="userDetailsService"/>
<property name="serviceProperties" ref="serviceProperties"/>
<property name="ticketValidator">
<bean class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator">
<constructor-arg value="https://CAS_SERVER"/>
</bean>
</property>
<property name="key" value="casAuthenticationProvider"/>
</bean>
<bean id="userDetailsService"
class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl">
<property name="dataSource" ref="dataSource"/>
<property name="usersByUsernameQuery">
<value>SELECT username,password,enabled FROM users WHERE username=?</value>
</property>
<property name="authoritiesByUsernameQuery">
<value>SELECT username, role FROM user_roles WHERE username=?</value>
</property>
</bean>
```
3. 在CAS服务器上配置应用程序的服务:
```
{
"@class" : "org.apereo.cas.services.RegexRegisteredService",
"serviceId" : "https://${APP_HOST}:${APP_PORT}/j_spring_cas_security_check",
"name" : "My Application",
"id" : 1,
"evaluationOrder" : 10,
"attributeReleasePolicy": {
"@class": "org.apereo.cas.services.ReturnAllAttributeReleasePolicy"
}
}
```
这些步骤将使CAS和Spring Security集成在一起,从而实现基于CAS的身份验证和授权。
阅读全文