View Javadoc

1   /*
2    * $Id: AcegiProviderAdapter.java 19191 2010-08-25 21:05:23Z tcarlson $
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.acegi;
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.acegisecurity.AuthenticationException;
21  import org.acegisecurity.providers.AuthenticationProvider;
22  import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
23  
24  /**
25   * <code>AcegiProviderAdapter</code> is a wrapper for an Acegi Security provider to
26   * use with the SecurityManager
27   */
28  public class AcegiProviderAdapter extends AbstractSecurityProvider implements AuthenticationProvider
29  {
30      private AuthenticationProvider delegate;
31      private Map securityProperties;
32  
33      /** For Spring IoC only */
34      public AcegiProviderAdapter()
35      {
36          super("acegi");
37      }
38  
39      public AcegiProviderAdapter(AuthenticationProvider delegate)
40      {
41          this(delegate, "acegi");
42      }
43  
44      public AcegiProviderAdapter(AuthenticationProvider delegate, String name)
45      {
46          super(name);
47          this.delegate = delegate;
48      }
49  
50      protected void doInitialise() throws InitialisationException
51      {
52          setSecurityContextFactory(new AcegiSecurityContextFactory());
53      }
54  
55      public Authentication authenticate(Authentication authentication) throws SecurityException
56      {
57          org.acegisecurity.Authentication auth = null;
58          if (authentication instanceof AcegiAuthenticationAdapter)
59          {
60              auth = ((AcegiAuthenticationAdapter)authentication).getDelegate();
61          }
62          else
63          {
64              auth = new UsernamePasswordAuthenticationToken(authentication.getPrincipal(),
65                  authentication.getCredentials());
66  
67          }
68          auth = delegate.authenticate(auth);
69          return new AcegiAuthenticationAdapter(auth, getSecurityProperties());
70      }
71  
72      public org.acegisecurity.Authentication authenticate(org.acegisecurity.Authentication authentication) throws AuthenticationException
73      {
74          return delegate.authenticate(authentication);
75      }
76  
77      public AuthenticationProvider getDelegate()
78      {
79          return delegate;
80      }
81  
82      public void setDelegate(AuthenticationProvider 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  }