【问题标题】:cas+spring securitycas+spring 安全性
【发布时间】:2017-11-03 10:19:48
【问题描述】:

我正在尝试在我的应用中实现 SSO,目前我遇到了下一个问题:

HTTP Status 401 - Authentication Failed: Failed to provide a CAS service ticket to validate

type Status report

message Authentication Failed: Failed to provide a CAS service ticket to validate

description This request requires HTTP authentication (Authentication Failed: Failed to provide a CAS service ticket to validate).

我在另一个tomcat上启动我的cas服务器,在那里登录,当我进入我的应用程序的登录页面时,然后: 1.然后是没有自动登录 2. 当我输入凭据时,我看到上面显示的堆栈跟踪 我从this site 中举了一个例子。

这是我的 Spring 安全上下文:

<security:global-method-security  pre-post-annotations="enabled" />

    <security:http use-expressions="true" auto-config="true"
        entry-point-ref="casAuthenticationEntryPoint" >

    <security:custom-filter position="CAS_FILTER" ref="casAuthenticationFilter"></security:custom-filter>

        <security:access-denied-handler  ref="accessDeniedHandler" />

        <security:form-login login-page="/login.html"
            authentication-failure-url="/login.html?login_error=1"
            default-target-url="/index.html" 
            always-use-default-target="false"
            login-processing-url="/j_spring_security_check"
            authentication-failure-handler-ref="failureHandler"
            authentication-success-handler-ref="successHandler" />
        <security:logout logout-url="/logout" logout-success-url="/login.html" />

        <security:intercept-url pattern="/member/**" access="hasRole('viewer')" />
        <security:intercept-url pattern="/admin/**" access="hasRole('SiteAdmin')" />
        <security:intercept-url pattern="/admin/**" access="hasRole('SystemAdmin')" />

        <security:remember-me />
    </security:http>

    <security:authentication-manager alias="authenticationManager">
        <security:authentication-provider user-service-ref="myUserDetailsService" />
        <security:authentication-provider ref="casAuthenticationProvider"></security:authentication-provider>
    </security:authentication-manager>

    <bean id="myUserDetailsService"
        class="com.mypackage.MyUserDetailsService">
    </bean>

    <bean id="authenticationEntryPoint"
        class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
        <property name="loginFormUrl" value="/login.html" />
        <property name="forceHttps" value="false" />
    </bean>

    <bean id="successHandler"
        class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
        <property name="defaultTargetUrl" value="/member/index.html" />
    </bean>

    <bean id="failureHandler"
        class="org.springframework.security.web.authentication.ExceptionMappingAuthenticationFailureHandler">
        <property name="defaultFailureUrl" value="/login.html?error=auth_fail" />
    </bean>

    <bean id="bannedHandler"
        class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
        <property name="defaultFailureUrl" value="/login.html?error=banned" />
    </bean>

    <bean id="accessDeniedHandler"
        class="org.springframework.security.web.access.AccessDeniedHandlerImpl">
        <property name="errorPage" value="/403.jsp"></property>
    </bean>


    <bean id="serviceProperties" class="org.springframework.security.cas.ServiceProperties">
        <property name="service" value="http://localhost:8080/spring-security-cas/j_spring_cas_security_check"></property>
        <property name="sendRenew" value="false"></property>
    </bean> 

    <bean id="casAuthenticationFilter" class="org.springframework.security.cas.web.CasAuthenticationFilter">
        <property name="authenticationManager" ref="authenticationManager"></property>
    </bean>

    <bean id="casAuthenticationEntryPoint" class="org.springframework.security.cas.web.CasAuthenticationEntryPoint">
        <property name="loginUrl" value="http://localhost:9080/cas-server-webapp-3.4.10/login"></property>
        <property name="serviceProperties" ref="serviceProperties"></property>
    </bean>

    <bean id="casAuthenticationProvider" class="org.springframework.security.cas.authentication.CasAuthenticationProvider">
        <property name="userDetailsService" ref="myUserDetailsService"></property>
        <property name="serviceProperties" ref="serviceProperties"></property>
        <property name="ticketValidator">
            <bean class="org.jasig.cas.client.validation.Cas20ServiceTicketValidator">
                <constructor-arg index="0" value="http://localhost:9080/cas-server-webapp-3.4.10"></constructor-arg>
            </bean>
        </property>
        <property name="key" value="cas"></property>
    </bean>

在 web.xml 中,我已经包含了 spring 安全上下文并设置了适当的侦听器等。 在 pom(我使用 maven)中,我添加了所需的依赖项,如文章中所述。 但问题依然存在。 我很乐意提供任何建议

【问题讨论】:

    标签: java spring spring-security cas jasig


    【解决方案1】:

    从您的错误消息“此请求需要 HTTP 身份验证”看来,您的 CAS 服务器已启用身份验证。您需要停用它并通过 HTML 页面登录(即它将是 JSP、ASP 或类似的东西)。

    【讨论】:

    • 如何在 CAS 中停用身份验证?逻辑是什么?
    • 好吧,我使用了 springsource 中的示例:link,并使用以下命令启动了他们的 CAS-Server:mvn jetty:run-war。我刚刚发现了一个可能对您有更多帮助的注释:import server certificate to the local keystore command keytool.exe -importkeystore -srckeystore "spring-security-samples\certificates\server.jks" -destkeystore "%JAVA_HOME%\jre\security\cacerts"
    • 我事先安装了证书...可能还有其他原因吗?
    • 如前所述,您的服务器是否需要身份验证?当您直接将浏览器导航到它(使用新会话)时,它会在您看到 CAS 登录页面之前要求进行身份验证吗?
    • 我不太明白你的意思,但我会试着解释一下我目前所拥有的。在第一个 tomcat 上,我启动了 CAS-server(我可以在那里登录,然后在控制台中看到它给了我什么票),在第二个 tomcat 上,我启动了我的应用程序,我想在其中进行 SSO(单点登录),因此我不会登录,它应该使用票证,但它需要凭据,并且在我将它们放入之后,我看到了上面的堆栈跟踪。那么我应该执行哪些步骤?
    【解决方案2】:

    我得到同样的痕迹,当

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    

    但是 cas 登录 url 没有 *.do 后缀。当我添加 .do 时它工作得很好。

    【讨论】:

      猜你喜欢
      • 2014-09-11
      • 2021-01-17
      • 1970-01-01
      • 2015-01-30
      • 2013-10-30
      • 2018-01-17
      • 2020-07-01
      • 2017-12-21
      • 2012-04-22
      相关资源
      最近更新 更多