View Javadoc

1   /*
2    * Copyright 2002-2009 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 javax.security.auth.callback.UnsupportedCallbackException;
21  
22  import org.apache.ws.security.WSPasswordCallback;
23  import org.apache.ws.security.WSUsernameTokenPrincipal;
24  
25  import org.springframework.beans.factory.InitializingBean;
26  import org.springframework.dao.DataAccessException;
27  import org.springframework.security.context.SecurityContextHolder;
28  import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
29  import org.springframework.security.providers.dao.UserCache;
30  import org.springframework.security.providers.dao.cache.NullUserCache;
31  import org.springframework.security.userdetails.UserDetails;
32  import org.springframework.security.userdetails.UserDetailsService;
33  import org.springframework.security.userdetails.UsernameNotFoundException;
34  import org.springframework.util.Assert;
35  import org.springframework.ws.soap.security.callback.CleanupCallback;
36  import org.springframework.ws.soap.security.support.SpringSecurityUtils;
37  
38  /**
39   * Callback handler that validates a password digest using an Spring Security <code>UserDetailsService</code>. Logic
40   * based on Spring Security's <code>DigestProcessingFilter</code>.
41   * <p/>
42   * An Spring Security <code>UserDetailService</code> is used to load <code>UserDetails</code> from. The digest of the
43   * password contained in this details object is then compared with the digest in the message.
44   *
45   * @author Arjen Poutsma
46   * @see org.springframework.security.userdetails.UserDetailsService
47   * @see org.springframework.security.ui.digestauth.DigestProcessingFilter
48   * @since 1.5.0
49   */
50  public class SpringDigestPasswordValidationCallbackHandler extends AbstractWsPasswordCallbackHandler
51          implements InitializingBean {
52  
53      private UserCache userCache = new NullUserCache();
54  
55      private UserDetailsService userDetailsService;
56  
57      /** Sets the users cache. Not required, but can benefit performance. */
58      public void setUserCache(UserCache userCache) {
59          this.userCache = userCache;
60      }
61  
62      /** Sets the Spring Security user details service. Required. */
63      public void setUserDetailsService(UserDetailsService userDetailsService) {
64          this.userDetailsService = userDetailsService;
65      }
66  
67      public void afterPropertiesSet() throws Exception {
68          Assert.notNull(userDetailsService, "userDetailsService is required");
69      }
70  
71      protected void handleUsernameToken(WSPasswordCallback callback) throws IOException, UnsupportedCallbackException {
72          String identifier = callback.getIdentifier();
73          UserDetails user = loadUserDetails(identifier);
74          if (user != null) {
75              SpringSecurityUtils.checkUserValidity(user);
76              callback.setPassword(user.getPassword());
77          }
78      }
79  
80      protected void handleUsernameTokenPrincipal(UsernameTokenPrincipalCallback callback)
81              throws IOException, UnsupportedCallbackException {
82          UserDetails user = loadUserDetails(callback.getPrincipal().getName());
83          WSUsernameTokenPrincipal principal = callback.getPrincipal();
84          UsernamePasswordAuthenticationToken authRequest =
85                  new UsernamePasswordAuthenticationToken(principal, principal.getPassword(), user.getAuthorities());
86          if (logger.isDebugEnabled()) {
87              logger.debug("Authentication success: " + authRequest.toString());
88          }
89          SecurityContextHolder.getContext().setAuthentication(authRequest);
90      }
91  
92      protected void handleCleanup(CleanupCallback callback) throws IOException, UnsupportedCallbackException {
93          SecurityContextHolder.clearContext();
94      }
95  
96      private UserDetails loadUserDetails(String username) throws DataAccessException {
97          UserDetails user = userCache.getUserFromCache(username);
98  
99          if (user == null) {
100             try {
101                 user = userDetailsService.loadUserByUsername(username);
102             }
103             catch (UsernameNotFoundException notFound) {
104                 if (logger.isDebugEnabled()) {
105                     logger.debug("Username '" + username + "' not found");
106                 }
107                 return null;
108             }
109             userCache.putUserInCache(user);
110         }
111         return user;
112     }
113 }