View Javadoc

1   /*
2    * Copyright 2006 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.xwss.callback.acegi;
18  
19  import java.io.IOException;
20  import java.security.cert.X509Certificate;
21  import javax.security.auth.callback.Callback;
22  import javax.security.auth.callback.UnsupportedCallbackException;
23  
24  import com.sun.xml.wss.impl.callback.CertificateValidationCallback;
25  import org.acegisecurity.Authentication;
26  import org.acegisecurity.AuthenticationException;
27  import org.acegisecurity.AuthenticationManager;
28  import org.acegisecurity.context.SecurityContextHolder;
29  import org.acegisecurity.providers.x509.X509AuthenticationToken;
30  
31  import org.springframework.beans.factory.InitializingBean;
32  import org.springframework.util.Assert;
33  import org.springframework.ws.soap.security.callback.AbstractCallbackHandler;
34  import org.springframework.ws.soap.security.callback.CleanupCallback;
35  
36  /**
37   * Callback handler that validates a certificate using an Acegi <code>AuthenticationManager</code>. Logic based on
38   * Acegi's <code>X509ProcessingFilter</code>. <p/> An Acegi <code>X509AuthenticationToken</code> is created with the
39   * certificate as the credentials. <p/> The configured authentication manager is expected to supply a provider which can
40   * handle this token (usually an instance of <code>X509AuthenticationProvider</code>).</p>
41   * <p/>
42   * This class only handles <code>CertificateValidationCallback</code>s, and throws an
43   * <code>UnsupportedCallbackException</code> for others.
44   *
45   * @author Arjen Poutsma
46   * @see X509AuthenticationToken
47   * @see org.acegisecurity.providers.x509.X509AuthenticationProvider
48   * @see org.acegisecurity.ui.x509.X509ProcessingFilter
49   * @see CertificateValidationCallback
50   * @since 1.0.0
51   */
52  public class AcegiCertificateValidationCallbackHandler extends AbstractCallbackHandler implements InitializingBean {
53  
54      private AuthenticationManager authenticationManager;
55  
56      private boolean ignoreFailure = false;
57  
58      /** Sets the Acegi authentication manager. Required. */
59      public void setAuthenticationManager(AuthenticationManager authenticationManager) {
60          this.authenticationManager = authenticationManager;
61      }
62  
63      public void setIgnoreFailure(boolean ignoreFailure) {
64          this.ignoreFailure = ignoreFailure;
65      }
66  
67      public void afterPropertiesSet() throws Exception {
68          Assert.notNull(authenticationManager, "authenticationManager is required");
69      }
70  
71      /**
72       * Handles  <code>CertificateValidationCallback</code>s, and throws an <code>UnsupportedCallbackException</code> for
73       * others
74       *
75       * @throws UnsupportedCallbackException when the callback is not supported
76       */
77      protected void handleInternal(Callback callback) throws IOException, UnsupportedCallbackException {
78          if (callback instanceof CertificateValidationCallback) {
79              ((CertificateValidationCallback) callback).setValidator(new AcegiCertificateValidator());
80          }
81          else if (callback instanceof CleanupCallback) {
82              SecurityContextHolder.clearContext();
83          }
84          else {
85              throw new UnsupportedCallbackException(callback);
86          }
87      }
88  
89      private class AcegiCertificateValidator implements CertificateValidationCallback.CertificateValidator {
90  
91          public boolean validate(X509Certificate certificate)
92                  throws CertificateValidationCallback.CertificateValidationException {
93              boolean result;
94              try {
95                  Authentication authResult =
96                          authenticationManager.authenticate(new X509AuthenticationToken(certificate));
97                  if (logger.isDebugEnabled()) {
98                      logger.debug("Authentication request for certificate with DN [" +
99                              certificate.getSubjectX500Principal().getName() + "] successful");
100                 }
101                 SecurityContextHolder.getContext().setAuthentication(authResult);
102                 return true;
103             }
104             catch (AuthenticationException failed) {
105                 if (logger.isDebugEnabled()) {
106                     logger.debug("Authentication request for certificate with DN [" +
107                             certificate.getSubjectX500Principal().getName() + "] failed: " + failed.toString());
108                 }
109                 SecurityContextHolder.clearContext();
110                 result = ignoreFailure;
111             }
112             return result;
113         }
114     }
115 }