View Javadoc

1   package org.springframework.security.config;
2   
3   import org.apache.commons.logging.Log;
4   import org.apache.commons.logging.LogFactory;
5   import org.springframework.beans.factory.config.BeanDefinition;
6   import org.springframework.beans.factory.config.RuntimeBeanReference;
7   import org.springframework.beans.factory.parsing.BeanComponentDefinition;
8   import org.springframework.beans.factory.support.ManagedList;
9   import org.springframework.beans.factory.support.RootBeanDefinition;
10  import org.springframework.beans.factory.xml.BeanDefinitionParser;
11  import org.springframework.beans.factory.xml.ParserContext;
12  import org.springframework.security.providers.anonymous.AnonymousAuthenticationProvider;
13  import org.springframework.security.providers.anonymous.AnonymousProcessingFilter;
14  import org.springframework.util.StringUtils;
15  import org.w3c.dom.Element;
16  
17  /**
18   * @author Ben Alex
19   * @version $Id: RememberMeBeanDefinitionParser.java 2231 2007-11-07 13:29:15Z luke_t $
20   */
21  public class AnonymousBeanDefinitionParser implements BeanDefinitionParser {
22      static final String ATT_KEY = "key";
23      static final String DEF_KEY = "doesNotMatter";
24  
25      static final String ATT_USERNAME = "username";
26      static final String DEF_USERNAME = "roleAnonymous";
27  
28      static final String ATT_GRANTED_AUTHORITY = "granted-authority";
29      static final String DEF_GRANTED_AUTHORITY = "ROLE_ANONYMOUS";
30  
31      protected final Log logger = LogFactory.getLog(getClass());
32  
33      public BeanDefinition parse(Element element, ParserContext parserContext) {
34          String grantedAuthority = null;
35          String username = null;
36          String key = null;
37          Object source = null;
38  
39          if (element != null) {
40              grantedAuthority = element.getAttribute(ATT_GRANTED_AUTHORITY);
41              username = element.getAttribute(ATT_USERNAME);
42              key = element.getAttribute(ATT_KEY);
43              source = parserContext.extractSource(element);
44          }
45  
46          if (!StringUtils.hasText(grantedAuthority)) {
47              grantedAuthority = DEF_GRANTED_AUTHORITY;
48          }
49  
50          if (!StringUtils.hasText(username)) {
51              username = DEF_USERNAME;
52          }
53  
54          if (!StringUtils.hasText(key)) {
55              key = DEF_KEY;
56          }
57  
58          RootBeanDefinition filter = new RootBeanDefinition(AnonymousProcessingFilter.class);
59  
60          filter.setSource(source);
61          filter.getPropertyValues().addPropertyValue("userAttribute", username + "," + grantedAuthority);
62          filter.getPropertyValues().addPropertyValue(ATT_KEY, key);
63  
64          RootBeanDefinition provider = new RootBeanDefinition(AnonymousAuthenticationProvider.class);
65          provider.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
66          provider.setSource(source);
67          provider.getPropertyValues().addPropertyValue(ATT_KEY, key);
68          
69          parserContext.getRegistry().registerBeanDefinition(BeanIds.ANONYMOUS_AUTHENTICATION_PROVIDER, provider);
70          ConfigUtils.addAuthenticationProvider(parserContext, BeanIds.ANONYMOUS_AUTHENTICATION_PROVIDER);
71  
72          parserContext.getRegistry().registerBeanDefinition(BeanIds.ANONYMOUS_PROCESSING_FILTER, filter);
73          ConfigUtils.addHttpFilter(parserContext, new RuntimeBeanReference(BeanIds.ANONYMOUS_PROCESSING_FILTER));
74          parserContext.registerComponent(new BeanComponentDefinition(filter, BeanIds.ANONYMOUS_PROCESSING_FILTER));
75          
76          return null;
77      }
78  }