View Javadoc

1   /*
2    * $Id: SpringSecurityContextFactory.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.security.Authentication;
14  import org.mule.api.security.SecurityContext;
15  import org.mule.api.security.SecurityContextFactory;
16  
17  import org.springframework.security.core.context.SecurityContextHolder;
18  import org.springframework.security.core.context.SecurityContextImpl;
19  
20  
21  /**
22   * <code>SpringSecurityContextFactory</code> creates an SpringSecurityContext for an
23   * Authentication object.
24   */
25  public class SpringSecurityContextFactory implements SecurityContextFactory
26  {
27      public SecurityContext create(Authentication authentication)
28      {
29          org.springframework.security.core.context.SecurityContext context = new SecurityContextImpl();        
30          context.setAuthentication(((SpringAuthenticationAdapter)authentication).getDelegate());
31  
32          if (authentication.getProperties() != null)
33          {
34              if ((authentication.getProperties().containsKey("securityMode")))
35              {
36                  SecurityContextHolder.setStrategyName((String)authentication.getProperties().get(
37                      "securityMode"));
38              }
39          }
40          SecurityContextHolder.setContext(context);
41          return new SpringSecurityContext(context);
42      }
43  }