【问题标题】:The webpage has a redirect loop in my chrome browser该网页在我的 chrome 浏览器中有一个重定向循环
【发布时间】:2014-09-02 06:27:12
【问题描述】:

如果他有 ROLE_USER,我正在尝试制作自定义登录表单并将用户重定向到学生页面。

控制器: 公共类登录控制器 { @自动连线 私有用户服务用户服务;

    @RequestMapping("/student")
    public String mypage(Model model, Principal principal) {
        model.addAttribute("TADADAGHDGHA");
        return "student/student";
    }     
//    @RequestMapping(value = "/logout", method = RequestMethod.GET)
//    public String logoutPage() {
//        return "/logoutPage";
//   }     
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String loginPage() {
        return "login/loginPage";
    }           
}

spring-security.xml

    <http auto-config="true" use-expressions="true" disable-url-rewriting="true">       
        <intercept-url pattern="/login/*" access="ROLE_USER" />         
        <form-login login-processing-url="/login" login-page="/login/loginPage"
            username-parameter="email" password-parameter="password"
            default-target-url="/student" authentication-failure-url="/loginPage?auth=fail" />
        <logout logout-success-url="/logoutPage" />  

    </http>

在 VIEWS 文件夹中,我有一个页面注销,一个名为 LOGIN 的文件夹,其中包含 login.jsp 和 tiles-definitions.xml,另一个名为 STUDENT 的文件夹包含 student.jsp 文件。

【问题讨论】:

    标签: java spring jsp controller


    【解决方案1】:

    不要限制查看您的登录页面的权限。否则,人们将不断被重定向到他们无权访问的页面...这就是您的重定向循环的来源。

    另外,您的authentication-failure-url 设置可能存在问题。根据给定的配置,它似乎不是一个有效的链接。

    【讨论】:

      【解决方案2】:

      根据 spring 文档,您可以通过这种方式检查用户是否在您的 spring-security.xml 中具有角色:

      <intercept-url pattern="/login/*" method="GET" access="hasRole('ROLE_USER')" /> OR
      <intercept-url pattern="/login/*" method="POST" access="hasRole('ROLE_USER')" />
      

      如果用户拥有该角色,则只有他才能查看该页面。

      您也可以通过这种方式限制服务层的方法访问;

      @PreAuthorize("hasRole('ROLE_USER')")
      public void methodName() {
         // Only users with ROLE_USER can access this content.
      }
      

      如果您想了解更多信息,可以使用权限,请参阅 Spring 文档、Spring in Action 或任何 Spring 书籍。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-09
        • 1970-01-01
        • 2014-12-27
        • 2013-12-22
        • 2013-11-06
        • 2013-04-14
        • 1970-01-01
        相关资源
        最近更新 更多