View Javadoc

1   /*
2    * Copyright 2008 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.soap.security.wss4j.callback.acegi;
18  
19  import java.io.IOException;
20  import javax.security.auth.callback.UnsupportedCallbackException;
21  
22  import org.acegisecurity.Authentication;
23  import org.acegisecurity.AuthenticationException;
24  import org.acegisecurity.AuthenticationManager;
25  import org.acegisecurity.context.SecurityContextHolder;
26  import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
27  import org.apache.ws.security.WSPasswordCallback;
28  import org.apache.ws.security.WSSecurityException;
29  
30  import org.springframework.beans.factory.InitializingBean;
31  import org.springframework.util.Assert;
32  import org.springframework.ws.soap.security.callback.CleanupCallback;
33  import org.springframework.ws.soap.security.wss4j.callback.AbstractWsPasswordCallbackHandler;
34  
35  /**
36   * Callback handler that validates a certificate uses an Acegi <code>AuthenticationManager</code>. Logic based on
37   * Acegi's <code>BasicProcessingFilter</code>.
38   * <p/>
39   * This handler requires an Acegi <code>AuthenticationManager</code> to operate. It can be set using the
40   * <code>authenticationManager</code> property. An Acegi <code>UsernamePasswordAuthenticationToken</code> is created
41   * with the username as principal and password as credentials.
42   *
43   * @author Arjen Poutsma
44   * @see org.acegisecurity.providers.UsernamePasswordAuthenticationToken
45   * @see org.acegisecurity.ui.basicauth.BasicProcessingFilter
46   * @since 1.5.0
47   */
48  public class AcegiPlainTextPasswordValidationCallbackHandler extends AbstractWsPasswordCallbackHandler
49          implements InitializingBean {
50  
51      private AuthenticationManager authenticationManager;
52  
53      private boolean ignoreFailure = false;
54  
55      /** Sets the Acegi authentication manager. Required. */
56      public void setAuthenticationManager(AuthenticationManager authenticationManager) {
57          this.authenticationManager = authenticationManager;
58      }
59  
60      public void setIgnoreFailure(boolean ignoreFailure) {
61          this.ignoreFailure = ignoreFailure;
62      }
63  
64      public void afterPropertiesSet() throws Exception {
65          Assert.notNull(authenticationManager, "authenticationManager is required");
66      }
67  
68      protected void handleCleanup(CleanupCallback callback) throws IOException, UnsupportedCallbackException {
69          SecurityContextHolder.clearContext();
70      }
71  
72      protected void handleUsernameTokenUnknown(WSPasswordCallback callback)
73              throws IOException, UnsupportedCallbackException {
74          String identifier = callback.getIdentifer();
75          try {
76              Authentication authResult = authenticationManager
77                      .authenticate(new UsernamePasswordAuthenticationToken(identifier, callback.getPassword()));
78              if (logger.isDebugEnabled()) {
79                  logger.debug("Authentication success: " + authResult.toString());
80              }
81              SecurityContextHolder.getContext().setAuthentication(authResult);
82          }
83          catch (AuthenticationException failed) {
84              if (logger.isDebugEnabled()) {
85                  logger.debug("Authentication request for user '" + identifier + "' failed: " + failed.toString());
86              }
87              SecurityContextHolder.clearContext();
88              if (!ignoreFailure) {
89                  throw new WSSecurityException(WSSecurityException.FAILED_AUTHENTICATION);
90              }
91          }
92      }
93  }