使用CAS和Spring Security进行重定向循环

丹尼尔·萨雷

我已经在GlassFish 3.1.2.2上设置了CAS 3.5.2服务器,现在我要按照官方文档,使用Spring Security 3.2.0通过CAS保护Jersey REST Web服务我的配置:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>springtest</display-name>

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/log4j.properties</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <!-- - Location of the XML file that defines the root application context 
        - Applied by ContextLoaderListener. -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        /WEB-INF/applicationContext-security.xml
    </param-value>
    </context-param>

    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>cas.root</param-value>
    </context-param>

    <!-- Include the character encoding Filter as per JASIG recommenation when 
        doing Single Sign Out https://wiki.jasig.org/display/CASC/Configuring+Single+Sign+Out -->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- Included to support Single Logout. Note that the SingleSignOutFilter 
        is included in the springSecurityFilterChain. However, it could also be placed 
        as the first filter-mapping in the web.xml -->
    <listener>
        <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
    </listener>

    <!-- - Loads the root application context of this web app at startup. - 
        The application context is then available via - WebApplicationContextUtils.getWebApplicationContext(servletContext). -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Jersey Servlet config -->

    <servlet>
        <description>JAX-RS Tools Generated - Do not modify</description>
        <servlet-name>JAX-RS Servlet</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>JAX-RS Servlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

applicationContext-security.xml

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

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

    <!-- this is what hooks up the CAS entry point -->
    <b:bean id="exceptionTranslationFilter"
        class="org.springframework.security.web.access.ExceptionTranslationFilter">
        <b:property name="authenticationEntryPoint">
            <b:ref local="casEntryPoint"client />
        </b:property>
    </b:bean>

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

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

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

    <!-- 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. -->
    <authentication-manager alias="authenticationManager">
        <authentication-provider ref="casAuthenticationProvider" />
    </authentication-manager>

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

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

</b:beans>

我确保该服务信任CAS服务器的证书,但是不知道是否需要相反的方向。以下消息反复循环,直到浏览器“对此感到无聊”:

log4j调试消息

DEBUG &#91;http-thread-pool-8181(4)&#93; &#40;ExceptionTranslationFilter.java&#58;165&#41; - Access is denied (user is anonymous); redirecting to authentication entry point
org.springframework.security.access.AccessDeniedException: Access is denied
    at org.springframework.security.access.vote.AffirmativeBased.decide(AffirmativeBased.java:83)

    ...

DEBUG &#91;http-thread-pool-8181(4)&#93; &#40;HttpSessionRequestCache.java&#58;41&#41; - DefaultSavedRequest added to Session: DefaultSavedRequest[https://localhost:8181/springtest/?ticket=ST-44-L0mrrGmf3vNFeGXCRkAj]

DEBUG &#91;http-thread-pool-8181(4)&#93; &#40;ExceptionTranslationFilter.java&#58;185&#41; - Calling Authentication entry point.

DEBUG &#91;http-thread-pool-8181(4)&#93; &#40;HttpSessionSecurityContextRepository.java&#58;300&#41; - SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.

DEBUG &#91;http-thread-pool-8181(4)&#93; &#40;SecurityContextPersistenceFilter.java&#58;97&#41; - SecurityContextHolder now cleared, as request processing completed

DEBUG &#91;http-thread-pool-8181(1)&#93; &#40;FilterChainProxy.java&#58;337&#41; - /?ticket=ST-45-3m2F3CVknJk6Af2u7d26 at position 1 of 9 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'

DEBUG &#91;http-thread-pool-8181(1)&#93; &#40;HttpSessionSecurityContextRepository.java&#58;148&#41; - HttpSession returned null object for SPRING_SECURITY_CONTEXT

DEBUG &#91;http-thread-pool-8181(1)&#93; &#40;HttpSessionSecurityContextRepository.java&#58;90&#41; - No SecurityContext was available from the HttpSession: org.apache.catalina.session.StandardSessionFacade@50e4c821. A new one will be created.

DEBUG &#91;http-thread-pool-8181(1)&#93; &#40;FilterChainProxy.java&#58;337&#41; - /?ticket=ST-45-3m2F3CVknJk6Af2u7d26 at position 2 of 9 in additional filter chain; firing Filter: 'WebAsyncManagerIntegrationFilter'

DEBUG &#91;http-thread-pool-8181(1)&#93; &#40;FilterChainProxy.java&#58;337&#41; - /?ticket=ST-45-3m2F3CVknJk6Af2u7d26 at position 3 of 9 in additional filter chain; firing Filter: 'CasAuthenticationFilter'

DEBUG &#91;http-thread-pool-8181(1)&#93; &#40;CasAuthenticationFilter.java&#58;311&#41; - serviceTicketRequest = false

DEBUG &#91;http-thread-pool-8181(1)&#93; &#40;CasAuthenticationFilter.java&#58;362&#41; - proxyReceptorConfigured = false

DEBUG &#91;http-thread-pool-8181(1)&#93; &#40;CasAuthenticationFilter.java&#58;349&#41; - proxyReceptorRequest = false

DEBUG &#91;http-thread-pool-8181(1)&#93; &#40;CasAuthenticationFilter.java&#58;327&#41; - proxyTicketRequest = false

DEBUG &#91;http-thread-pool-8181(1)&#93; &#40;CasAuthenticationFilter.java&#58;262&#41; - requiresAuthentication = false

DEBUG &#91;http-thread-pool-8181(1)&#93; &#40;FilterChainProxy.java&#58;337&#41; - /?ticket=ST-45-3m2F3CVknJk6Af2u7d26 at position 4 of 9 in additional filter chain; firing Filter: 'RequestCacheAwareFilter'

DEBUG &#91;http-thread-pool-8181(1)&#93; &#40;DefaultSavedRequest.java&#58;325&#41; - pathInfo: arg1=/; arg2=/ (property equals)

DEBUG &#91;http-thread-pool-8181(1)&#93; &#40;DefaultSavedRequest.java&#58;331&#41; - queryString: arg1=ticket=ST-44-L0mrrGmf3vNFeGXCRkAj; arg2=ticket=ST-45-3m2F3CVknJk6Af2u7d26 (property not equals)

DEBUG &#91;http-thread-pool-8181(1)&#93; &#40;HttpSessionRequestCache.java&#58;75&#41; - saved request doesn't match

DEBUG &#91;http-thread-pool-8181(1)&#93; &#40;FilterChainProxy.java&#58;337&#41; - /?ticket=ST-45-3m2F3CVknJk6Af2u7d26 at position 5 of 9 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter'

DEBUG &#91;http-thread-pool-8181(1)&#93; &#40;FilterChainProxy.java&#58;337&#41; - /?ticket=ST-45-3m2F3CVknJk6Af2u7d26 at position 6 of 9 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter'

DEBUG &#91;http-thread-pool-8181(1)&#93; &#40;AnonymousAuthenticationFilter.java&#58;102&#41; - Populated SecurityContextHolder with anonymous token: 'org.springframework.security.authentication.AnonymousAuthenticationToken@6faa1b5a: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@ffff6a82: RemoteIpAddress: 127.0.0.1; SessionId: 3e9339134a98fa96a8dd34676e8f; Granted Authorities: ROLE_ANONYMOUS'

DEBUG &#91;http-thread-pool-8181(1)&#93; &#40;FilterChainProxy.java&#58;337&#41; - /?ticket=ST-45-3m2F3CVknJk6Af2u7d26 at position 7 of 9 in additional filter chain; firing Filter: 'SessionManagementFilter'

DEBUG &#91;http-thread-pool-8181(1)&#93; &#40;FilterChainProxy.java&#58;337&#41; - /?ticket=ST-45-3m2F3CVknJk6Af2u7d26 at position 8 of 9 in additional filter chain; firing Filter: 'ExceptionTranslationFilter'

DEBUG &#91;http-thread-pool-8181(1)&#93; &#40;FilterChainProxy.java&#58;337&#41; - /?ticket=ST-45-3m2F3CVknJk6Af2u7d26 at position 9 of 9 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'

DEBUG &#91;http-thread-pool-8181(1)&#93; &#40;AbstractSecurityInterceptor.java&#58;194&#41; - Secure object: FilterInvocation: URL: /?ticket=ST-45-3m2F3CVknJk6Af2u7d26; Attributes: [ROLE_USER]

DEBUG &#91;http-thread-pool-8181(1)&#93; &#40;AbstractSecurityInterceptor.java&#58;310&#41; - Previously Authenticated: org.springframework.security.authentication.AnonymousAuthenticationToken@6faa1b5a: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@ffff6a82: RemoteIpAddress: 127.0.0.1; SessionId: 3e9339134a98fa96a8dd34676e8f; Granted Authorities: ROLE_ANONYMOUS

DEBUG &#91;http-thread-pool-8181(1)&#93; &#40;AffirmativeBased.java&#58;65&#41; - Voter: org.springframework.security.access.vote.RoleVoter@65b46ab9, returned: -1

DEBUG &#91;http-thread-pool-8181(1)&#93; &#40;AffirmativeBased.java&#58;65&#41; - Voter: org.springframework.security.access.vote.AuthenticatedVoter@27cacbd9, returned: 0

似乎CASFilter只是没有意识到提供了有效的服务票证。我配置错误了吗?

罗伯·温奇

看来您的serviceProperties定义不正确。具体来说,服务必须是由CasAuthenticationFilter监视的URL否则,CasAuthenticationFilter会忽略该请求,然后Spring Security要求对该URL进行身份验证并重新请求ST。

默认情况下,CasAuthenticationFilter处理对/ j_spring_cas_security_check的请求。所以您可能想要这样的东西:

<b:bean id="serviceProperties" class="org.springframework.security.cas.ServiceProperties">
    <b:property name="service" value="https://localhost:8181/springtest/j_spring_cas_security_check" />
    <b:property name="sendRenew" value="false" />
</b:bean>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用Grails Spring Security CAS插件进行重定向循环

来自分类Dev

Spring Security应用程序中的重定向循环

来自分类Dev

如何使用htaccess进行重定向而不引起循环

来自分类Dev

具有Spring Security 4的CAS 4(java config)-在票证被授予SSO后卡在重定向循环中

来自分类Dev

使用Spring Security启用HTTPS:此网页具有重定向循环

来自分类Dev

使用Spring Security启用HTTPS:此网页具有重定向循环

来自分类Dev

重定向循环中的Spring Security OAuth2(google)Web应用

来自分类Dev

CAS + Service Worker 无限重定向循环

来自分类Dev

308使用Ingress-NGINX使用ExternalName服务进行重定向循环

来自分类Dev

通过Spring Security和CAS进行单点注销

来自分类Dev

Spring Security基本路径重定向

来自分类Dev

Spring Boot Security 403重定向

来自分类Dev

Spring Security不会重定向到主页

来自分类Dev

Spring Security和angular javascript重定向到登录页面

来自分类Dev

Spring Security和Angular javascript重定向到登录页面

来自分类Dev

Stormpath Spring Boot重定向循环

来自分类Dev

使用 JHipster、Spring Security 和 oauth2 控制身份验证重定向

来自分类Dev

使用重定向:uri登录后将Spring Security重定向到页面

来自分类Dev

无法使用CAS和Spring Security登录到应用程序

来自分类Dev

如何防止Spring MVC进行重定向?

来自分类Dev

使用ssl的nginx重定向循环

来自分类Dev

使用mod_rewrite的重定向循环

来自分类Dev

使用mod_rewrite重定向循环

来自分类Dev

在`nohup`,`&`和重定向之间进行排序?

来自分类Dev

使用htaccess进行Magento重定向

来自分类Dev

使用输入重定向进行流程替换

来自分类Dev

如何使用导航规则进行重定向

来自分类Dev

使用React Router组件进行重定向

来自分类Dev

使用htaccess进行多域重定向

Related 相关文章

  1. 1

    使用Grails Spring Security CAS插件进行重定向循环

  2. 2

    Spring Security应用程序中的重定向循环

  3. 3

    如何使用htaccess进行重定向而不引起循环

  4. 4

    具有Spring Security 4的CAS 4(java config)-在票证被授予SSO后卡在重定向循环中

  5. 5

    使用Spring Security启用HTTPS:此网页具有重定向循环

  6. 6

    使用Spring Security启用HTTPS:此网页具有重定向循环

  7. 7

    重定向循环中的Spring Security OAuth2(google)Web应用

  8. 8

    CAS + Service Worker 无限重定向循环

  9. 9

    308使用Ingress-NGINX使用ExternalName服务进行重定向循环

  10. 10

    通过Spring Security和CAS进行单点注销

  11. 11

    Spring Security基本路径重定向

  12. 12

    Spring Boot Security 403重定向

  13. 13

    Spring Security不会重定向到主页

  14. 14

    Spring Security和angular javascript重定向到登录页面

  15. 15

    Spring Security和Angular javascript重定向到登录页面

  16. 16

    Stormpath Spring Boot重定向循环

  17. 17

    使用 JHipster、Spring Security 和 oauth2 控制身份验证重定向

  18. 18

    使用重定向:uri登录后将Spring Security重定向到页面

  19. 19

    无法使用CAS和Spring Security登录到应用程序

  20. 20

    如何防止Spring MVC进行重定向?

  21. 21

    使用ssl的nginx重定向循环

  22. 22

    使用mod_rewrite的重定向循环

  23. 23

    使用mod_rewrite重定向循环

  24. 24

    在`nohup`,`&`和重定向之间进行排序?

  25. 25

    使用htaccess进行Magento重定向

  26. 26

    使用输入重定向进行流程替换

  27. 27

    如何使用导航规则进行重定向

  28. 28

    使用React Router组件进行重定向

  29. 29

    使用htaccess进行多域重定向

热门标签

归档