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.context.SecurityContextHolder;
23  import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
24  import org.acegisecurity.providers.dao.UserCache;
25  import org.acegisecurity.providers.dao.cache.NullUserCache;
26  import org.acegisecurity.userdetails.UserDetails;
27  import org.acegisecurity.userdetails.UserDetailsService;
28  import org.acegisecurity.userdetails.UsernameNotFoundException;
29  import org.apache.ws.security.WSPasswordCallback;
30  import org.apache.ws.security.WSUsernameTokenPrincipal;
31  
32  import org.springframework.beans.factory.InitializingBean;
33  import org.springframework.dao.DataAccessException;
34  import org.springframework.util.Assert;
35  import org.springframework.ws.soap.security.callback.CleanupCallback;
36  import org.springframework.ws.soap.security.wss4j.callback.AbstractWsPasswordCallbackHandler;
37  import org.springframework.ws.soap.security.wss4j.callback.UsernameTokenPrincipalCallback;
38  
39  /**
40   * Callback handler that validates a password digest using an Acegi <code>UserDetailsService</code>. Logic based on
41   * Acegi's <code>DigestProcessingFilter</code>.
42   * <p/>
43   * An Acegi <code>UserDetailService</code> is used to load <code>UserDetails</code> from. The digest of the password
44   * contained in this details object is then compared with the digest in the message.
45   *
46   * @author Arjen Poutsma
47   * @see org.acegisecurity.userdetails.UserDetailsService
48   * @see org.acegisecurity.ui.digestauth.DigestProcessingFilter
49   * @since 1.5.0
50   */
51  public class AcegiDigestPasswordValidationCallbackHandler extends AbstractWsPasswordCallbackHandler
52          implements InitializingBean {
53  
54      private UserCache userCache = new NullUserCache();
55  
56      private UserDetailsService userDetailsService;
57  
58      /** Sets the users cache. Not required, but can benefit performance. */
59      public void setUserCache(UserCache userCache) {
60          this.userCache = userCache;
61      }
62  
63      /** Sets the Acegi user details service. Required. */
64      public void setUserDetailsService(UserDetailsService userDetailsService) {
65          this.userDetailsService = userDetailsService;
66      }
67  
68      public void afterPropertiesSet() throws Exception {
69          Assert.notNull(userDetailsService, "userDetailsService is required");
70      }
71  
72      protected void handleUsernameToken(WSPasswordCallback callback) throws IOException, UnsupportedCallbackException {
73          String identifier = callback.getIdentifer();
74          UserDetails user = loadUserDetails(identifier);
75          if (user != null) {
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 }