Results 1 to 5 of 5

Thread: Determine whether user logged in or not?

  1. #1

    Question Determine whether user logged in or not?

    Hi everybody, I want to check if user logged successfuly or not? I just try to use a method like this:

    Code:
             try {
                      boolean isAuthenticated = SecurityContextHolder.getContext().getAuthentication().isAuthenticated();
                      return isAuthenticated; 
             } catch (Exception e) {
                      return false;
             }
    But SecurityContextHolder.getContext().getAuthenticati on().isAuthenticated() always returns true. Any solution for this problem?

    Thanks in advance.

  2. #2
    Join Date
    Feb 2009
    Location
    Helsinki
    Posts
    151

    Default

    Hi,

    The code checks whether user has authenticated with any possible method (including the 'anonymous' or 'remember me' authentication when enabled). In case you want to check whether SAML SSO (and no other authentication) took place, something like this might work instead:

    Code:
    public boolean isLoggedInWithSAML() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            return false;
        } else if (authentication.isAuthenticated() && (authentication.getCredentials() != null && authentication.getCredentials().equals(SAMLCredential.class))) {
            return true;
        } else {
            return false;
        }        
    }
    Vladi

  3. #3

    Default

    Hi Vladi,
    What happens when webapp's session is expired? I see that your function returns false while user haven't logged out yet. I think it should return true, because when I try to login again, it automatically redirect to my homepage and I don't need to input username and password again.

  4. #4
    Join Date
    Feb 2009
    Location
    Helsinki
    Posts
    151

    Default

    The method will be returning false after user's session expiration. In case you re-initialize single sign-on after the expiration, chances are that IDP will still not ask you for credentials (because the IDP's session may still be active).

    Vladi

  5. #5

    Default

    Thank you, let me try to do it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •