View Javadoc

    1   /* Copyright 2004 Acegi Technology Pty Limited
    2    *
    3    * Licensed under the Apache License, Version 2.0 (the "License");
    4    * you may not use this file except in compliance with the License.
    5    * You may obtain a copy of the License at
    6    *
    7    *     http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    * Unless required by applicable law or agreed to in writing, software
    10   * distributed under the License is distributed on an "AS IS" BASIS,
    11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License.
    14   */
    15
    16  package org.acegisecurity.providers.rcp;
    17
    18  import org.acegisecurity.Authentication;
    19  import org.acegisecurity.AuthenticationException;
    20  import org.acegisecurity.GrantedAuthority;
    21  import org.acegisecurity.providers.AuthenticationProvider;
    22  import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
    23
    24  import org.apache.commons.logging.Log;
    25  import org.apache.commons.logging.LogFactory;
    26
    27  import org.springframework.beans.factory.InitializingBean;
    28  import org.springframework.util.Assert;
    29
    30
    31  /***
    32   * Client-side object which queries a  {@link RemoteAuthenticationManager} to
    33   * validate an authentication request.
    34   *
    35   * <P>
    36   * A new <code>Authentication</code> object is created by this class comprising
    37   * the request <code>Authentication</code> object's <code>principal</code>,
    38   * <code>credentials</code> and the <code>GrantedAuthority</code>[]s returned
    39   * by the <code>RemoteAuthenticationManager</code>.
    40   * </p>
    41   *
    42   * <P>
    43   * The <code>RemoteAuthenticationManager</code> should not require any special
    44   * username or password setting on the remoting client proxy factory to
    45   * execute the call. Instead the entire authentication request must be
    46   * encapsulated solely within the <code>Authentication</code> request object.
    47   * In practical terms this means the <code>RemoteAuthenticationManager</code>
    48   * will <B>not</B> be protected by BASIC or any other HTTP-level
    49   * authentication.
    50   * </p>
    51   *
    52   * <P>
    53   * If authentication fails, a <code>RemoteAuthenticationException</code> will
    54   * be thrown. This exception should be caught and displayed to the user,
    55   * enabling them to retry with alternative credentials etc.
    56   * </p>
    57   *
    58   * @author Ben Alex
    59   * @version $Id: RemoteAuthenticationProvider.java,v 1.3 2005/11/17 00:55:51 benalex Exp $
    60   */
    61  public class RemoteAuthenticationProvider implements AuthenticationProvider,
    62      InitializingBean {
    63      //~ Static fields/initializers =============================================
    64
    65      private static final Log logger = LogFactory.getLog(RemoteAuthenticationProvider.class);
    66
    67      //~ Instance fields ========================================================
    68
    69      private RemoteAuthenticationManager remoteAuthenticationManager;
    70
    71      //~ Methods ================================================================
    72
    73      public void setRemoteAuthenticationManager(
    74          RemoteAuthenticationManager remoteAuthenticationManager) {
    75          this.remoteAuthenticationManager = remoteAuthenticationManager;
    76      }
    77
    78      public RemoteAuthenticationManager getRemoteAuthenticationManager() {
    79          return remoteAuthenticationManager;
    80      }
    81
    82      public void afterPropertiesSet() throws Exception {
    83          Assert.notNull(this.remoteAuthenticationManager, "remoteAuthenticationManager is mandatory");
    84      }
    85
    86      public Authentication authenticate(Authentication authentication)
    87          throws AuthenticationException {
    88          String username = authentication.getPrincipal().toString();
    89          String password = authentication.getCredentials().toString();
    90          GrantedAuthority[] authorities = remoteAuthenticationManager
    91              .attemptAuthentication(username, password);
    92
    93          return new UsernamePasswordAuthenticationToken(username, password,
    94              authorities);
    95      }
    96
    97      public boolean supports(Class authentication) {
    98          return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));
    99      }
    100 }