View Javadoc

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