View Javadoc
1   /*
2    * Copyright 2005-2012 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;
18  
19  import java.io.IOException;
20  import java.util.HashMap;
21  import java.util.Map;
22  import java.util.Properties;
23  import javax.security.auth.callback.UnsupportedCallbackException;
24  
25  import org.springframework.beans.factory.InitializingBean;
26  import org.springframework.util.Assert;
27  
28  import org.apache.ws.security.WSPasswordCallback;
29  
30  /**
31   * Simple callback handler that validates passwords against a in-memory <code>Properties</code> object. Password
32   * validation is done on a case-sensitive basis.
33   *
34   * @author Tareq Abed Rabbo
35   * @author Arjen Poutsma
36   * @see #setUsers(java.util.Properties)
37   * @since 1.5.0
38   */
39  public class SimplePasswordValidationCallbackHandler extends AbstractWsPasswordCallbackHandler
40          implements InitializingBean {
41  
42      private Map<String, String > users = new HashMap<String, String>();
43  
44      /** Sets the users to validate against. Property names are usernames, property values are passwords. */
45      public void setUsers(Properties users) {
46          for (Map.Entry<Object, Object> entry : users.entrySet()) {
47              if (entry.getKey() instanceof String && entry.getValue() instanceof String) {
48                  this.users.put((String) entry.getKey(), (String) entry.getValue());
49              }
50          }
51      }
52  
53      public void setUsersMap(Map<String, String> users) {
54          this.users = users;
55      }
56  
57      public void afterPropertiesSet() throws Exception {
58          Assert.notNull(users, "users is required");
59      }
60  
61      @Override
62      protected void handleUsernameToken(WSPasswordCallback callback) throws IOException, UnsupportedCallbackException {
63          String identifier = callback.getIdentifier();
64          callback.setPassword(users.get(identifier));
65      }
66  
67  }