Results 1 to 3 of 3

Thread: RFE: Provide a way to translate the JAAS LoginExceptions

  1. #1

    Default RFE: Provide a way to translate the JAAS LoginExceptions

    In my JAASLoginModule, I throw a subclass of LoginException upon InvalidCredentials, UserNotFound, .... However, the current LDAPProvider does not foresee a way to resolve the LoginException into an Acegi exception. I could just throw an Acegi exception straight away, but that would mean that the JAAS login module is dependent on Acegi, while it is the other way around.

    To make a long story short, could the class JaasAuthenticationProvider be alter to call a method for resolving the LoginException, so I can subclass it and provide my own resolver ?

    Code:
        public Authentication authenticate(Authentication auth)
                try {
                     ....
                } catch (LoginException e) {
                    context.publishEvent(new JaasAuthenticationFailedEvent(auth, e));
    
                    // This could become the implementation of resolveLoginException(e); so I can subclass and change behavior
                    //We have no way of knowing what caused the exception, so we cannot throw BadCredentialsException, DisabledException, or LockedException.
                    //So we'll just throw an AuthenticationServiceException
                    throw new AuthenticationServiceException(e.toString());
                }
    Stefaan

  2. #2

    Default Default: look at the getCause() of the LoginException

    What's more, if the LoginModule throws an Acegi exception straight away, it gets wrapped in a LoginException by JAAS. The exception handler could look if the target exception of the LoginException is an Acegi exception, and use that one.

    Code:
          public Authentication authenticate(Authentication auth)
                try {
                     ....
                } catch (LoginException e) {
                    context.publishEvent(new JaasAuthenticationFailedEvent(auth, e));
                     resolveLoginException(e);
                } 
                ...
    
          /**
           * Tries to resolves a JAAS login exception into an Acegie exception, such as BadCredentialsException, DisabledException, UsernameNotFoundException or LockedException
           * @param loginException The login exception causing the failed login
           * @return AcegiSecurityException The corresponding exception of Acegi
           */
          public AcegiSecurityException resolveLoginException(LoginException loginException) {
               // if the code needs to be compatible with pre 1.4, we need to use reflection here
               if (loginException.getCause() instanceof AcegiSecurityException) {
                   return (AcegiSecurityException)loginException.getCause();
                } else {
                   //We have no way of knowing what caused the exception, so we return the general AuthenticationServiceException
                   //So we'll just throw an AuthenticationServiceException
                   throw new AuthenticationServiceException(loginException.toString());
                }
          }
    Ben, if u want, I'll be happy to contribute the modified JaasAuthenticationProvider (and test case).

    Stefaan.

  3. #3
    Join Date
    Oct 2004
    Posts
    207

    Default

    I like both ideas actually...

    How about I implement the translateException method, with the default functionality being to check the getCause() method for an AcegiSecurityException.

    For example....


    Code:
        public Authentication authenticate(Authentication auth) {
                try {
                	...
                } catch (LoginException e) {
                    AcegiSecurityException translatedException = translateException(e);
                    context.publishEvent(new JaasAuthenticationFailedEvent(auth, translatedException));
                    throw translatedException;
                }
            }
    
            return null;
        }
    
        protected AcegiSecurityException translateException(LoginException e) {
            if (e.getCause() instanceof AcegiSecurityException) {
                return (AcegiSecurityException) e.getCause();
            } else {
                return new AuthenticationServiceException(e.toString());
            }
        }

Similar Threads

  1. Jboss, JAAS, Spring -- working example?
    By jkwon in forum Security
    Replies: 4
    Last Post: May 14th, 2009, 03:41 PM
  2. JAAS - Acegi
    By john017 in forum Security
    Replies: 6
    Last Post: Jul 10th, 2007, 02:17 AM
  3. acegi not calling JAAS LoginModule.logout() anywhere?
    By raysuliteanu in forum Security
    Replies: 3
    Last Post: Jun 24th, 2005, 09:32 PM
  4. Unable to translate SQLException
    By timwu1616 in forum Data
    Replies: 1
    Last Post: May 18th, 2005, 02:25 PM
  5. Replies: 7
    Last Post: Oct 20th, 2004, 08:23 PM

Posting Permissions

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