需要帮助为spring security cas client创建“成功身份验证”页面

时间:2012-11-20 20:01:28

标签: spring spring-mvc spring-security cas

我正在使用Spring MVC编写一个客户端应用程序,该应用程序将针对Spring Security CAS服务器进行身份验证。

我遇到的问题是,在用户成功验证后,浏览器显示404错误。我不确定如何在我的应用程序中设置“成功”页面。或者我是否需要在CAS服务器属性中的某处定义回调URL?到目前为止,这是我的代码:

的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- The definition of the Root Spring Container shared by all Servlets 
    and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/applicationContext-security.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

的applicationContext-security.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:sec="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<!-- Enable security, let the casAuthenticationEntryPoint handle all intercepted 
    urls. The CAS_FILTER needs to be in the right position within the filter 
    chain. -->
<security:http entry-point-ref="casAuthenticationEntryPoint"
    auto-config="true">
    <security:intercept-url pattern="/**" access="ROLE_USER"></security:intercept-url>
    <security:custom-filter position="CAS_FILTER"
        ref="casAuthenticationFilter"></security:custom-filter>
</security:http>

<!-- Required for the casProcessingFilter, so define it explicitly set and 
    specify an Id Even though the authenticationManager is created by default 
    when namespace based config is used. -->
<security:authentication-manager alias="authenticationManager">
    <security:authentication-provider
        ref="casAuthenticationProvider"></security:authentication-provider>
</security:authentication-manager>

<!-- This section is used to configure CAS. The service is the actual redirect 
    that will be triggered after the CAS login sequence. -->
<bean id="serviceProperties" class="org.springframework.security.cas.ServiceProperties">
    <property name="service"
        value="https://localhost:8443/cas/j_spring_cas_security_check"></property>
    <property name="sendRenew" value="false"></property>
</bean>

<!-- The CAS filter handles the redirect from the CAS server and starts 
    the ticket validation. -->
<bean id="casAuthenticationFilter"
    class="org.springframework.security.cas.web.CasAuthenticationFilter">
    <property name="authenticationManager" ref="authenticationManager"></property>
</bean>

<!-- The entryPoint intercepts all the CAS authentication requests. It redirects 
    to the CAS loginUrl for the CAS login page. -->
<bean id="casAuthenticationEntryPoint"
    class="org.springframework.security.cas.web.CasAuthenticationEntryPoint">
    <property name="loginUrl" value="https://localhost:8443/cas/login"></property>
    <property name="serviceProperties" ref="serviceProperties"></property>
</bean>

<!-- Handles the CAS ticket processing. -->
<bean id="casAuthenticationProvider"
    class="org.springframework.security.cas.authentication.CasAuthenticationProvider">
    <property name="userDetailsService" ref="userService"></property>
    <property name="serviceProperties" ref="serviceProperties"></property>
    <property name="ticketValidator">
        <bean class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator">
            <constructor-arg index="0" value="https://localhost:8443/cas">
            </constructor-arg>
        </bean>
    </property>
    <property name="key" value="cas"></property>
</bean>

<!-- The users available for this application. -->
<security:user-service id="userService">
    <security:user name="user" password="user" authorities="ROLE_USER"></security:user>
</security:user-service>

我将不胜感激任何帮助!

上面的代码来自以下教程:http://www.oudmaijer.com/2009/12/28/spring-3-spring-security-3-cas-3-3-4-integration/


更新:这是网络流程(取自Firebug):

  1. 用户点击https://localhost:8443/SpringMVC_CAS/secure/index.jsp

  2. 浏览器从/myapp/secure/index.jsp

  3. 执行“302暂时移动”
  4. CAS提示登录https://localhost:8443/cas/login?service=https%3A%2F%2Flocalhost%3A8443%2Fcas%2Fj_spring_cas_security_check

  5. https://localhost:8443/cas/j_spring_cas_security_check?ticket=ST-17-RHf3OTJXAWePgzVGP2nc-cas

  6. 浏览器显示https://localhost:8443/cas/login?ticket=ST-17-RHf3OTJXAWePgzVGP2nc-cas

1 个答案:

答案 0 :(得分:4)

您可以通过添加AuthenticationSuccessHandler这样做:

<bean id="casAuthenticationFilter" class="org.springframework.security.cas.web.CasAuthenticationFilter">
    <property name="authenticationManager" ref="authenticationManager"/>
    <property name="authenticationFailureHandler">
        <bean class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
            <property name="defaultFailureUrl" value="/casfailed.jsp"/>
        </bean>
    </property>
    <property name="authenticationSuccessHandler">
        <bean class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
            <property name="defaultTargetUrl" value="/"/>
        </bean>
    </property>
</bean>

如果您也将alwaysUseDefaultTargetUrl属性设置为true,则defaultTargetUrl将用于目标,否则在开始身份验证过程之前重定向到原始目标将会停止。

另见: