View Javadoc

1   /*
2    * $Id: SpringProviderAdapter.java 21765 2011-05-03 11:33:05Z dirk.olmes $
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.authentication.AuthenticationManager;
21  import org.springframework.security.authentication.AuthenticationProvider;
22  import org.springframework.security.core.AuthenticationException;
23  
24  
25  /**
26   * <code>SpringProviderAdapter</code> is a wrapper for a Spring 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      @Override
53      protected void doInitialise() throws InitialisationException
54      {
55          setSecurityContextFactory(new SpringSecurityContextFactory());
56      }
57  
58      public Authentication authenticate(Authentication authentication) throws SecurityException
59      {
60          org.springframework.security.core.Authentication auth = null;        
61          if (authentication instanceof SpringAuthenticationAdapter)
62          {
63              auth = ((SpringAuthenticationAdapter)authentication).getDelegate();
64          }
65          else
66          {
67              auth = this.getAuthenticationProvider().getAuthentication(authentication);
68  
69          }
70          auth = delegate.authenticate(auth);
71          return new SpringAuthenticationAdapter(auth, getSecurityProperties(), authentication.getEvent());
72      }
73  
74      public org.springframework.security.core.Authentication authenticate(org.springframework.security.core.Authentication authentication) throws AuthenticationException    
75      {
76          return delegate.authenticate(authentication);
77      }
78  
79      public AuthenticationManager getDelegate()
80      {
81          return delegate;
82      }
83  
84      public void setDelegate(AuthenticationManager delegate)
85      {
86          this.delegate = delegate;
87      }
88  
89      public Map getSecurityProperties()
90      {
91          return securityProperties;
92      }
93  
94      public void setSecurityProperties(Map securityProperties)
95      {
96          this.securityProperties = securityProperties;
97      }
98  
99      public SpringAuthenticationProvider getAuthenticationProvider()
100     {
101         if (this.authenticationProvider == null) {
102             this.authenticationProvider = new UserAndPasswordAuthenticationProvider();
103         }
104         return authenticationProvider;
105     }
106 
107     public void setAuthenticationProvider(SpringAuthenticationProvider authenticationProvider)
108     {
109         this.authenticationProvider = authenticationProvider;
110     }
111 }