【问题标题】:Redirect loop in Spring Security appSpring Security 应用程序中的重定向循环
【发布时间】:2014-01-11 22:45:09
【问题描述】:

我正在开发一个 Spring MVC / Spring Security 应用程序。

我没有任何异常或错误,但其中一个页面存在重定向循环。

我正在使用 Spring 3.0.1 和 Spring Security 3.0.1。

我的dispatcher-security.xml

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:security="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.0.xsd">

   <security:http auto-config="true" use-expressions="true"> 
      <security:form-login login-page="/login" default-target-url="/login" authentication-failure-url="/fail2login"/> 
      <security:logout logout-success-url="/"/> 
      <security:intercept-url pattern="/auth/**" access="hasRole('ANONYMOUS')" /> 
      <security:intercept-url pattern="/js/**" access="hasRole('ANONYMOUS')" /> 
      <security:intercept-url pattern="/css/**" access="hasRole('ANONYMOUS')" /> 
      <security:intercept-url pattern="/**" access="hasRole('ADMIN')" /> 
   </security:http>

   <security:authentication-manager>  
      <security:authentication-provider>  
         <security:jdbc-user-service data-source-ref="dataSource1"
           users-by-username-query=" select name,password,enabled from user where name=?"      
           authorities-by-username-query="select u.name, r.role from user u, role r where u.role = r.auto_id and u.name =?  "
         />
      </security:authentication-provider>  
   </security:authentication-manager>

</beans>

请帮帮我……

【问题讨论】:

    标签: spring-mvc spring-security


    【解决方案1】:

    default-target-url 属性定义了在成功登录的情况下用户被重定向到的页面。通常它是您的应用程序的主页。你有default-target-url="/login",所以它会在成功登录后将你重定向回登录页面。

    我不明白您示例中ANONYMOUS 角色的含义。如果是匿名用户的内置角色,我想应该叫ROLE_ANONYMOUS。 在这种情况下,您可能使用不正确,这两行:

    <security:intercept-url pattern="/js/**" access="hasRole('ANONYMOUS')" />
    <security:intercept-url pattern="/css/**" access="hasRole('ANONYMOUS')" />
    

    应该换成这样的:

    <security:intercept-url pattern="/js/**" access="hasRole('ROLE_ANONYMOUS') or hasRole('ROLE_USER')" />
    <security:intercept-url pattern="/css/**" access="hasRole('ROLE_ANONYMOUS') or hasRole('ROLE_USER')" />
    

    否则未经身份验证的用户将能够访问/js//css/ 目录。

    ROLE_USER 不是内置角色,它是您为所有经过身份验证的用户手动定义的角色。

    另见:

    What is the difference between ROLE_USER and ROLE_ANONYMOUS

    The Spring Security Reference: Anonymous Authentication

    【讨论】:

      猜你喜欢
      • 2014-11-21
      • 2015-01-12
      • 2013-03-07
      • 2015-03-10
      • 1970-01-01
      • 2015-04-06
      • 2015-09-12
      • 2014-07-18
      • 2015-08-17
      相关资源
      最近更新 更多