2012-10-27 65 views
1

原/請求的頁面如何實現下面的場景與四郎安全的Spring MVC應用程序:四郎/ Spring MVC的重定向到成功登錄後

如果用戶沒有通過認證,並請求一個頁面,四郎應 重定向到登錄頁面。用戶成功登錄和四郎 重定向到先前請求的頁面,而不是successUrl URL

登錄部分在我的應用程序工作正常的。下面是我現有的代碼

<!-- Shiro filter --> 
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> 
     <property name="securityManager" ref="securityManager" /> 
     <property name="loginUrl" value="/login" /> 
     <property name="successUrl" value="/dashboard" /> 
     <property name="unauthorizedUrl" value="/error" /> 
     <property name="filterChainDefinitions"> 
      <value> 
       <!-- !!! Order matters !!! --> 
       /authenticate = anon 
       /login = anon 
       /logout = anon 
       /error = anon 
       /static/** = anon 
       /** = authc 
      </value> 
     </property> 
    </bean> 

回答

2

在LoginController中的一個片段:

public String doLogin(
     HttpServletRequest request, 
     HttpServletResponse response, 
     @RequestParam(required = true) String username, 
     @RequestParam(required = true) String password, 
     @RequestParam(required = false, defaultValue = "false") boolean rememberMe, 
     Model model) { 
    Subject currentUser = SecurityUtils.getSubject(); 

    ... 

    if (currentUser.isAuthenticated()) { 
     String fallbackUrl = "redirect:/"; 
     try { 
      // redirect to previously requested page 
      WebUtils.redirectToSavedRequest(request, response, fallbackUrl); 
     } catch (IOException e) { 
      logger.error(e.getMessage(), e); 
      return fallbackUrl; 
     } 
     // return null to prevent spring render another page 
     return null; 
    } else { 
     session.setAttribute("loginFailCount", ++loginFailCount); 
    } 
    return "login"; 
} 
相關問題