View Javadoc

1   /*
2    * $Id: SpringProviderAdapter.java 20477 2010-12-06 23:38:52Z mike.schilling $
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.core.AuthenticationException;
21  import org.springframework.security.authentication.AuthenticationManager;
22  import org.springframework.security.authentication.AuthenticationProvider;
23  
24  
25  /**
26   * <code>AcegiProviderAdapter</code> is a wrapper for an Acegi Security provider to
27   * use with the SecurityManager
28   */
29  public class SpringProviderAdapter extends AbstractSecurityProvider implements AuthenticationProvider
30  {
31      private AuthenticationManager delegate;
32      private Map securityProperties;
33      private SpringAuthenticationProvider authenticationProvider;
34  
35      /** For Spring IoC only */
36      public SpringProviderAdapter()
37      {
38          super("spring-security");
39      }
40  
41      public SpringProviderAdapter(AuthenticationManager delegate)
42      {
43          this(delegate, "spring-security");
44      }
45  
46      public SpringProviderAdapter(AuthenticationManager delegate, String name)
47      {
48          super(name);
49          this.delegate = delegate;
50      }
51  
52      protected void doInitialise() throws InitialisationException
53      {
54          setSecurityContextFactory(new SpringSecurityContextFactory());
55      }
56  
57      public Authentication authenticate(Authentication authentication) throws SecurityException
58      {
59          org.springframework.security.core.Authentication auth = null;        
60          if (authentication instanceof SpringAuthenticationAdapter)
61          {
62              auth = ((SpringAuthenticationAdapter)authentication).getDelegate();
63          }
64          else
65          {
66              auth = this.getAuthenticationProvider().getAuthentication(authentication);
67  
68          }
69          auth = delegate.authenticate(auth);
70          return new SpringAuthenticationAdapter(auth, getSecurityProperties());
71      }
72  
73      public org.springframework.security.core.Authentication authenticate(org.springframework.security.core.Authentication authentication) throws AuthenticationException    
74      {
75          return delegate.authenticate(authentication);
76      }
77  
78      public AuthenticationManager getDelegate()
79      {
80          return delegate;
81      }
82  
83      public void setDelegate(AuthenticationManager delegate)
84      {
85          this.delegate = delegate;
86      }
87  
88      public Map getSecurityProperties()
89      {
90          return securityProperties;
91      }
92  
93      public void setSecurityProperties(Map securityProperties)
94      {
95          this.securityProperties = securityProperties;
96      }
97  
98      public SpringAuthenticationProvider getAuthenticationProvider()
99      {
100         if (this.authenticationProvider == null) {
101             this.authenticationProvider = new UserAndPasswordAuthenticationProvider();
102         }
103         return authenticationProvider;
104     }
105 
106     public void setAuthenticationProvider(SpringAuthenticationProvider authenticationProvider)
107     {
108         this.authenticationProvider = authenticationProvider;
109     }
110 }