基于spring security 的CAS登录 重定向特定路径
时间: 2023-07-07 13:39:28 浏览: 52
如果您想要基于Spring Security的CAS登录重定向到特定路径,可以通过以下步骤实现:
1. 在Spring Security配置文件中添加以下代码,以配置CAS登录:
```
<http>
<intercept-url pattern="/secure/**" access="ROLE_USER" />
<logout logout-success-url="/logout.jsp" />
<custom-filter ref="casAuthenticationFilter" />
</http>
<authentication-manager>
<authentication-provider ref="casAuthenticationProvider" />
</authentication-manager>
<beans:bean id="casAuthenticationFilter"
class="org.springframework.security.cas.web.CasAuthenticationFilter">
<beans:property name="authenticationManager" ref="authenticationManager" />
<beans:property name="authenticationSuccessHandler">
<beans:bean
class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
<beans:property name="defaultTargetUrl" value="/secure/index.jsp" />
</beans:bean>
</beans:property>
</beans:bean>
<beans:bean id="casAuthenticationProvider"
class="org.springframework.security.cas.authentication.CasAuthenticationProvider">
<beans:property name="userDetailsService" ref="userService" />
<beans:property name="serviceProperties" ref="serviceProperties" />
<beans:property name="ticketValidator">
<beans:bean class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator">
<beans:constructor-arg index="0" value="https://localhost:8443/cas" />
</beans:bean>
</beans:property>
<beans:property name="key" value="casAuthenticationProviderKey" />
</beans:bean>
<beans:bean id="serviceProperties"
class="org.springframework.security.cas.ServiceProperties">
<beans:property name="service"
value="https://localhost:8443/myapp/j_spring_cas_security_check" />
<beans:property name="sendRenew" value="false" />
</beans:bean>
<beans:bean id="userService"
class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">
<beans:property name="userDetailsService" ref="inMemoryUserService" />
</beans:bean>
<beans:bean id="inMemoryUserService"
class="org.springframework.security.core.userdetails.memory.InMemoryDaoImpl">
<beans:property name="userProperties">
<beans:value>
casuser=123456,ROLE_USER
</beans:value>
</beans:property>
</beans:bean>
```
2. 在 `SavedRequestAwareAuthenticationSuccessHandler` 中设置默认目标URL,如上面的示例中所示,将其设置为 `/secure/index.jsp`。
3. 然后,您可以在您的应用程序中使用重定向来将用户重定向到特定路径。例如,您可以使用以下代码在控制器方法中将用户重定向到 `/myapp/home`:
```
return "redirect:/myapp/home";
```
注意,这将导致用户被重定向到CAS服务器进行身份验证,然后将其重定向回 `/myapp/home`。