“需要帮助创建”;“成功验证”;spring security cas客户端的第页

“需要帮助创建”;“成功验证”;spring security cas客户端的第页,spring,spring-mvc,spring-security,cas,Spring,Spring Mvc,Spring Security,Cas,我正在使用SpringMVC编写一个客户端应用程序,该应用程序将针对SpringSecurityCAS服务器进行身份验证 我遇到的问题是,在用户成功验证后,浏览器显示404错误。我不知道如何在我的应用程序中设置“成功”页面。或者我需要在CAS服务器属性中的某个位置定义回调url吗?以下是我目前的代码: web.xml: <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java

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

我遇到的问题是,在用户成功验证后,浏览器显示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>
<?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>

上下文配置位置
/WEB-INF/spring/applicationContext-security.xml
org.springframework.web.context.ContextLoaderListener
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*
index.html
index.jsp

applicationContext security.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>
<?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>

我将感谢任何帮助

上述代码来自以下教程:


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

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

  • 浏览器从
    /myapp/secure/index.jsp执行“302临时移动”

  • CAS提示登录
    https://localhost:8443/cas/login?service=https%3A%2F%2Flocalhost%3A8443%2Fcas%2Fj_spring_cas_security_check

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

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


  • 您可以通过如下方式添加一个
    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将用于目标,否则将在身份验证过程开始之前重定向到原始目标

    另见:


    您可以通过如下方式添加一个
    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将用于目标,否则将在身份验证过程开始之前重定向到原始目标

    另见:


    你好,拉维,谢谢你的帮助。我解决了404错误(由于我的一个URL中的错误)。我还插入了您的代码,但是我发现在成功验证后,浏览器不会重定向到我在defaultTargetUrl中指定的页面。相反,CAS总是闪烁“登录成功”的横幅。您能看到我的代码中可能缺少的其他内容吗?CAS服务器设置是否可能覆盖casAuthenticationFilter?我尝试设置AlwaysSuseDefaultTargetUrl,还尝试了SimpleRuThenticationSuccessHandler。两者都没有区别……以下是登录成功横幅的全文:“您已成功登录到中央身份验证服务。出于安全原因,请在访问完需要身份验证的服务后注销并退出web浏览器!”尝试删除http元素上的自动配置属性或将其设置为false。您还可以看到这个示例,它对无状态服务进行身份验证。嗨,拉维,在将auto-config设置为false并将其完全删除之后,我得到了相同的结果。我还尝试在您提供的链接处使用源代码创建一个单独的项目,但同样的行为再次发生。服务属性值应该是,您的Web应用程序的名称,因为它是一个回调url。嗨,Ravi,感谢您的帮助。我解决了404错误(由于我的一个URL中的错误)。我还插入了您的代码,但是我发现在成功验证后,浏览器不会重定向到我在defaultTargetUrl中指定的页面。相反,CAS总是闪烁“登录成功”的横幅。您能看到我的代码中可能缺少的其他内容吗?CAS服务器设置是否可能覆盖casAuthenticationFilter?我尝试设置AlwaysSuseDefaultTargetUrl,还尝试了SimpleRuThenticationSuccessHandler。两者都没有区别……以下是登录成功横幅的全文:“您已成功登录到中央身份验证服务。出于安全原因,请在访问完需要身份验证的服务后注销并退出web浏览器!”尝试删除http元素上的自动配置属性或将其设置为false。您还可以看到这个示例,它对无状态服务进行身份验证。嗨,拉维,在将auto-config设置为false并将其完全删除之后,我得到了相同的结果。我还尝试在您提供的链接处使用源代码创建一个单独的项目,同样的行为再次发生。服务属性值应该是,您的webapp的名称,因为它是回调url。