View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.spring.security;
8   
9   import org.mule.api.lifecycle.InitialisationException;
10  import org.mule.api.security.Authentication;
11  import org.mule.api.security.SecurityException;
12  import org.mule.security.AbstractSecurityProvider;
13  
14  import java.util.Map;
15  
16  import org.springframework.security.core.AuthenticationException;
17  import org.springframework.security.authentication.AuthenticationManager;
18  import org.springframework.security.authentication.AuthenticationProvider;
19  
20  
21  /**
22   * <code>AcegiProviderAdapter</code> is a wrapper for an Acegi Security provider to
23   * use with the SecurityManager
24   */
25  public class SpringProviderAdapter extends AbstractSecurityProvider implements AuthenticationProvider
26  {
27      private AuthenticationManager delegate;
28      private Map securityProperties;
29      private SpringAuthenticationProvider authenticationProvider;
30  
31      /** For Spring IoC only */
32      public SpringProviderAdapter()
33      {
34          super("spring-security");
35      }
36  
37      public SpringProviderAdapter(AuthenticationManager delegate)
38      {
39          this(delegate, "spring-security");
40      }
41  
42      public SpringProviderAdapter(AuthenticationManager delegate, String name)
43      {
44          super(name);
45          this.delegate = delegate;
46      }
47  
48      protected void doInitialise() throws InitialisationException
49      {
50          setSecurityContextFactory(new SpringSecurityContextFactory());
51      }
52  
53      public Authentication authenticate(Authentication authentication) throws SecurityException
54      {
55          org.springframework.security.core.Authentication auth = null;        
56          if (authentication instanceof SpringAuthenticationAdapter)
57          {
58              auth = ((SpringAuthenticationAdapter)authentication).getDelegate();
59          }
60          else
61          {
62              auth = this.getAuthenticationProvider().getAuthentication(authentication);
63  
64          }
65          auth = delegate.authenticate(auth);
66          return new SpringAuthenticationAdapter(auth, getSecurityProperties());
67      }
68  
69      public org.springframework.security.core.Authentication authenticate(org.springframework.security.core.Authentication authentication) throws AuthenticationException    
70      {
71          return delegate.authenticate(authentication);
72      }
73  
74      public AuthenticationManager getDelegate()
75      {
76          return delegate;
77      }
78  
79      public void setDelegate(AuthenticationManager delegate)
80      {
81          this.delegate = delegate;
82      }
83  
84      public Map getSecurityProperties()
85      {
86          return securityProperties;
87      }
88  
89      public void setSecurityProperties(Map securityProperties)
90      {
91          this.securityProperties = securityProperties;
92      }
93  
94      public SpringAuthenticationProvider getAuthenticationProvider()
95      {
96          if (this.authenticationProvider == null) {
97              this.authenticationProvider = new UserAndPasswordAuthenticationProvider();
98          }
99          return authenticationProvider;
100     }
101 
102     public void setAuthenticationProvider(SpringAuthenticationProvider authenticationProvider)
103     {
104         this.authenticationProvider = authenticationProvider;
105     }
106 }