2012-03-09 53 views
4

我爲我的web應用程序使用spring security 3.1。Spring Security 3.1 - 如何識別用戶已經登錄或不是?

我已經實現了我的自定義過濾器來提供所請求URL的過濾。

用戶登錄後,用戶點擊登錄URL,此時登錄URL不應該被打開。我的意思是說我如何檢查用戶是否已經登錄?

如果用戶已經登錄,則不應打開登錄頁面。它應該打開default-target-url頁面。

謝謝。

回答

4

您可以使用SecurityContextHolder類中的靜態方法從Security Context 獲得Authentication對象,然後您可以找到用戶當前是否已登錄。

+0

感謝您的回答。 但我的問題是,如果用戶已經登錄,則不應顯示登錄頁面。應顯示登錄頁面主頁面(登錄頁面後)。 所以請幫助我解決這個問題,如果可能的話,使用preauthentication過濾器。 謝謝。 – 2012-03-12 07:06:38

0

試試這個:

Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); 
if (authentication == null) { 
//not authorized 
} 
Object principalObject = authentication.getPrincipal(); 
if (principalObject == null) { 
//not authorized 
} 

或者你可以配置安全,我認爲這是你在你的applicationContext需要的東西:

<security:http auto-config="true" authentication-manager-ref="authenticationManager"> 
     <!-- Don't set any role restrictions on login.jsp and index.jsp --> 
     <security:intercept-url pattern="/login" 
           access="IS_AUTHENTICATED_ANONYMOUSLY"/> 
     <security:intercept-url pattern="/urlOfAView" 
           access="IS_AUTHENTICATED_ANONYMOUSLY"/> 

     <!-- Restrict access to ALL other pages --> 
     <security:intercept-url pattern="/**" access="ROLE_USER"/> 

     <!-- Set the login page and what to do if login fails --> 
     <security:form-login login-page="/login" 
          authentication-failure-url="/login?login_error=1" default-target-url="/loginUser"/> 

     <!-- Set the logout page and where to go after logout is successful --> 
     <security:logout logout-success-url="/index"/> 
    </security:http> 

我使用Spring 3.1,所以我的命名空間:

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:security="http://www.springframework.org/schema/security" 
     xsi:schemaLocation=" 
     http://www.springframework.org/schema/security 
     http://www.springframework.org/schema/security/spring-security-3.1.xsd 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
     "> 

祝你好運

+0

感謝您的回答。 但我的問題是,如果用戶已經登錄,則不應顯示登錄頁面。應顯示登錄頁面主頁面(登錄頁面後)。 所以請幫助我解決這個問題,如果可能的話,使用preauthentication過濾器。 謝謝。 – 2012-03-12 07:06:31

+0

我更新了我的答案。如果您將配置添加到應用程序上下文中,那麼當您訪問索引頁時,spring會檢查您是否已通過身份驗證。如果你不是,它會將你重定向到登錄頁面。您還可以看到:[安全命名空間配置](http://static.springsource.org/spring-security/site/docs/3.0.x/reference/ns-config。html) – 2012-03-12 08:42:01

+0

感謝您的回覆。 但我想它也是副詩。我的意思是說,如果用戶已經登錄,那麼登錄頁面將不會被打開,但目標網址將被打開。 謝謝。 – 2012-03-15 13:13:50

4

我正在尋找一個解決方案,以完全相同的問題。我結束了以下工作:

@RequestMapping(method = RequestMethod.GET, value = "/admin/login") 
public ModelAndView login(HttpServletRequest request) { 
    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 
    if(principal instanceof UserDetails && validLogin((UserDetails) principal)){ 
     return new ModelAndView("redirect:/admin/home"); // Go to admin home if already 
                 // logged in 
    } 

    final String error = request.getParameter("login_error"); 
    return loginPage.display(error); // Not logged in, so admin login page is displayed 
} 

private boolean validLogin(UserDetails userDetails) { 
    // This function does a check to ascertain the validity of the logged in user 
    // You may also consider evaluating userDetails.getAuthorities() 
    return userDetails.isAccountNonExpired() && 
      userDetails.isAccountNonLocked() && 
      userDetails.isCredentialsNonExpired() && 
      userDetails.isEnabled(); 
} 

但我當然希望有一個更可配置的方式來實現這一點。

0

我現在正在編寫類似的功能。在我來說,我只是添加了此代碼與Spring的安全配置:當然

<http pattern="/**" auto-config="false" use-expressions="true" > 

<intercept-url pattern="/login" access="!isAuthenticated()" /> 
<access-denied-handler error-page="/"/> 

<!-- More configuration here --> 

</http> 

的這種配置會將所有「拒絕訪問」請求您的主頁。如果你只想爲你的登錄頁面使用不同的重定向,你應該提供你自己的訪問拒絕處理程序實現。

相關問題