View Javadoc
1   /*
2    * Copyright 2005-2010 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;
18  
19  import java.io.IOException;
20  import javax.security.auth.callback.Callback;
21  import javax.security.auth.callback.UnsupportedCallbackException;
22  
23  import com.sun.xml.wss.impl.callback.PasswordValidationCallback;
24  
25  import org.springframework.beans.factory.InitializingBean;
26  import org.springframework.security.core.Authentication;
27  import org.springframework.security.core.AuthenticationException;
28  import org.springframework.security.authentication.AuthenticationManager;
29  import org.springframework.security.core.context.SecurityContextHolder;
30  import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
31  import org.springframework.util.Assert;
32  import org.springframework.ws.soap.security.callback.AbstractCallbackHandler;
33  import org.springframework.ws.soap.security.callback.CleanupCallback;
34  
35  /**
36   * Callback handler that validates a certificate uses an Spring Security <code>AuthenticationManager</code>. Logic based
37   * on Spring Security's <code>BasicProcessingFilter</code>.
38   * <p/>
39   * This handler requires an Spring Security <code>AuthenticationManager</code> to operate. It can be set using the
40   * <code>authenticationManager</code> property. An Spring Security <code>UsernamePasswordAuthenticationToken</code> is
41   * created with the username as principal and password as credentials.
42   * <p/>
43   * This class only handles <code>PasswordValidationCallback</code>s that contain a
44   * <code>PlainTextPasswordRequest</code>, and throws an <code>UnsupportedCallbackException</code> for others.
45   *
46   * @author Arjen Poutsma
47   * @see org.springframework.security.authentication.UsernamePasswordAuthenticationToken
48   * @see com.sun.xml.wss.impl.callback.PasswordValidationCallback
49   * @see com.sun.xml.wss.impl.callback.PasswordValidationCallback.PlainTextPasswordRequest
50   * @since 1.5.0
51   */
52  public class SpringPlainTextPasswordValidationCallbackHandler extends AbstractCallbackHandler
53          implements InitializingBean {
54  
55      private AuthenticationManager authenticationManager;
56  
57      private boolean ignoreFailure = false;
58  
59      /** Sets the Spring Security authentication manager. Required. */
60      public void setAuthenticationManager(AuthenticationManager authenticationManager) {
61          this.authenticationManager = authenticationManager;
62      }
63  
64      public void setIgnoreFailure(boolean ignoreFailure) {
65          this.ignoreFailure = ignoreFailure;
66      }
67  
68      public void afterPropertiesSet() throws Exception {
69          Assert.notNull(authenticationManager, "authenticationManager is required");
70      }
71  
72      /**
73       * Handles <code>PasswordValidationCallback</code>s that contain a <code>PlainTextPasswordRequest</code>, and throws
74       * an <code>UnsupportedCallbackException</code> for others.
75       *
76       * @throws javax.security.auth.callback.UnsupportedCallbackException
77       *          when the callback is not supported
78       */
79      @Override
80      protected void handleInternal(Callback callback) throws IOException, UnsupportedCallbackException {
81          if (callback instanceof PasswordValidationCallback) {
82              PasswordValidationCallback validationCallback = (PasswordValidationCallback) callback;
83              if (validationCallback.getRequest() instanceof PasswordValidationCallback.PlainTextPasswordRequest) {
84                  validationCallback.setValidator(new SpringSecurityPlainTextPasswordValidator());
85                  return;
86              }
87          }
88          else if (callback instanceof CleanupCallback) {
89              SecurityContextHolder.clearContext();
90              return;
91          }
92          throw new UnsupportedCallbackException(callback);
93      }
94  
95      private class SpringSecurityPlainTextPasswordValidator implements PasswordValidationCallback.PasswordValidator {
96  
97          public boolean validate(PasswordValidationCallback.Request request)
98                  throws PasswordValidationCallback.PasswordValidationException {
99              PasswordValidationCallback.PlainTextPasswordRequest plainTextRequest =
100                     (PasswordValidationCallback.PlainTextPasswordRequest) request;
101             try {
102                 Authentication authResult = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
103                         plainTextRequest.getUsername(), plainTextRequest.getPassword()));
104                 if (logger.isDebugEnabled()) {
105                     logger.debug("Authentication success: " + authResult.toString());
106                 }
107                 SecurityContextHolder.getContext().setAuthentication(authResult);
108                 return true;
109             }
110             catch (AuthenticationException failed) {
111                 if (logger.isDebugEnabled()) {
112                     logger.debug("Authentication request for user '" + plainTextRequest.getUsername() + "' failed: " +
113                             failed.toString());
114                 }
115                 SecurityContextHolder.clearContext();
116                 return ignoreFailure;
117             }
118         }
119     }
120 
121 }