namespaces.apt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. ----------------------------------------
  2. Security Namespace Configuration
  3. ----------------------------------------
  4. ------------------------
  5. (2007-11-7 draft)
  6. ------------------------
  7. Overview
  8. * Summary
  9. Spring Security will make use of Spring 2.0 namespace-based configuration features to allow much more concise
  10. configuration. This document outlines the current state of development and the features that have been
  11. implemented so far and raises points for discussion. The features are still largely experimental and subject to
  12. change.
  13. The {{{http://acegisecurity.svn.sourceforge.net/svnroot/acegisecurity/spring-security/trunk/core/src/main/resources/org/springframework/security/config/}schema files}}
  14. can be found in the core package. For simplicity, a relax NG compact schema (the rnc file) has been used as it is
  15. easier to work with. This is then coverted into a W3C schema using trang.
  16. * Design
  17. The initial aim is to provide a relatively small set of namespace configuration options which capture the most
  18. common uses of framework, especially for inexperienced users who don't require much in the way of customization.
  19. The focus should be mainly on providing high-level components which logically match the different aspects of
  20. securing an application. While it is also useful to have namespace options for simplifying the configuration of
  21. existing framework beans, this is a somewhat orthogonal requirement, with different target users and will be dealt
  22. with separately. Only the most obvious customizations will be implemented to start with. It should also be possible
  23. to add new features without affecting the existing design.
  24. Keeping these aims in mind, the design is largely based around the large-scale dependencies within the framework.
  25. It can be divided up into the following areas:
  26. * <<Web/HTTP Security>> - this is by far the largest and most complex area, consisting of
  27. * Filter chain proxy
  28. * HttpSessionContextIntegrationFilter - special filter which is of key importance.
  29. * The filters which are responsible for most of Spring Security's web application features.
  30. * ExceptionTranslationFilter and FilterSecurityInterceptor - other special filters.
  31. * Authentication entry point(s).
  32. * Concurrent session support (optional).
  33. * Remember-me service (optional).
  34. The only inward dependency here is that the AuthenticationManager must be made aware of the
  35. ConcurrentSessionController (if configured). Apart from this, the other areas of the framework are unaware of
  36. web-related functionality.
  37. * <<Business Object (Method) Security>> - This is currently implemented using a bean decorator within the
  38. business object bean, which is a relatively simple syntax.
  39. * <<AuthenticationManager>> - this is the main dependency of other parts of the framework. The namespace
  40. configuration creates an instance of ProviderManager as required and registers it under a known name. It assumes
  41. that this will be used by all the configured beans. This currently happens automatically. It might be worth
  42. introducing a
  43. ---
  44. <security:authentication-manager id="othername"/>
  45. ---
  46. element just to allow users to explicitly configure an authentication manager which they can use in other beans.
  47. The additional name could be registered as an alias.
  48. * <<AccessDecisionManager>> - The security interceptors (both for method and http security) require access to
  49. an access decision manager. At the moment a default one is created and used by both, but they should additionally
  50. support the use of an independently configured access manager, based on Id. The standard bean syntax should be
  51. sufficient for the moment.
  52. * <<AuthenticationProviders>> - these can be created individually and register themselves with the authentication
  53. manager. Current namespace options are limited but each provider should be pretty much self-contained and hence
  54. relatively simple to implement. There may be multiple providers within an application so some means of ordering will
  55. be required.
  56. * <<UserDetailsService>> - Closely related to the authentication providers, but often also required by other beans.
  57. Again the implementations should be relatively straightforward. There may be multiple implementations within the
  58. application.
  59. * Http Security
  60. Probably the best starting point here is to look at the {{{http://acegisecurity.svn.sourceforge.net/svnroot/acegisecurity/spring-security/trunk/samples/tutorial/src/main/webapp/WEB-INF/applicationContext-security-ns.xml}namespace configuration file}}
  61. which is in use in the tutorial sample application:
  62. +--------------------------------------------------------------------------------------------------------------------
  63. <beans xmlns="http://www.springframework.org/schema/beans"
  64. xmlns:security="http://www.springframework.org/schema/security"
  65. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  66. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  67. http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd">
  68. <security:http>
  69. <security:intercept-url pattern="/secure/extreme/**" access="ROLE_SUPERVISOR"/>
  70. <security:intercept-url pattern="/secure/**" access="IS_AUTHENTICATED_REMEMBERED" />
  71. <security:intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />
  72. <security:form-login />
  73. <security:http-basic realm="SpringSecurityTutorialApp" />
  74. <security:logout />
  75. <security:concurrent-session-control maxSessions="1" exceptionIfMaximumExceeded="true"/>
  76. <security:remember-me tokenRepository="tokenRepo"/>
  77. </security:http>
  78. <bean name="tokenRepo" class="org.springframework.security.ui.rememberme.InMemoryTokenRepositoryImpl"/>
  79. <security:authentication-provider>
  80. <security:user-service>
  81. <security:user name="bob" password="bobspassword" authorities="ROLE_SUPERVISOR" />
  82. <security:user name="bill" password="billspassword" authorities="ROLE_A,ROLE_B" />
  83. </security:user-service>
  84. </security:authentication-provider>
  85. </beans>
  86. +--------------------------------------------------------------------------------------------------------------------
  87. ** \<security:http /\>
  88. This element groups the other http-specific elements together and has attributes which allow the
  89. session creation strategy to be set. It also allows you to specify the type of pattern that will be used for URL
  90. definitions within the block. This defaults to ``ant'' since this is what users almost always use in practice.
  91. The {{{./spring-security-core/xref/org/springframework/security/config/HttpSecurityBeanDefinitionParser.html}HttpSecurityBeanDefinitionParser}}
  92. class is responsible for handling the contents. It creates a FilterChainProxy
  93. instance, HttpSessionContextIntegrationFilter, FilterSecurityInterceptor and ExceptionTranslationFilter and parses
  94. the child intercept-url elements (see below). It then delegates to separate parsers to handle the contents of other
  95. child elements. These encapsulate the functionality of separate web application concerns which are implemented
  96. by Spring Security and will typically each create a filter and possibly one or more other beans.
  97. Finally a post processor (HttpSecurityConfigPostProcessor) is registered to handle things which can't be done
  98. until the application context has been completed. This includes assembling and ordering the filter chain
  99. and the strategy for selecting which authentication entry point should be used to trigger login. The core
  100. filters now implement Ordered and a standard ordering has been established for them. Other filters in the
  101. context must also implement Ordered to be considered, so it may be necessary to provide an element which can be
  102. used to decorate filter beans to achieve this, if the user can't (or doesn't want to) make their code implement
  103. Ordered explicitly.
  104. ** \<security:intercept-url /\>
  105. In a traditional, bean-configured setup, there are several beans which require the use of URL patterns -
  106. FilterChainProxy, FilterSecurityInterceptor and ChannelProcessingFilter. These can now be specified using a
  107. single construct and the appropriate beans created and assembled by the parser. This allows options to be combined
  108. in a more logical fashion without duplication. The user picks the important URLs for their application, defines the
  109. patterns for them and then specifies which filters should be used, what access configuration attributes
  110. the FilterSecurityInterceptor should enforce and if any channel restrictions apply. Only the access decision part
  111. has been implemented. The only option for filters is currently "none", which will omit the URL from the security
  112. filter chain entirely. It's not clear how or if additional filter order customization should be implemented (other
  113. than by allowing Ids to be set on the various child elements). Channel security should be straightforward.
  114. * Method Security
  115. An example use of the \<security:intercept-methods /\> decorator is:
  116. +-----------------------------------------------------------------------------------------------------------------------
  117. <bean id="target" class="org.springframework.security.config.TestBusinessBeanImpl">
  118. <security:intercept-methods>
  119. <security:protect method="org.springframework.security.config.TestBusinessBean.set*" access="ROLE_ADMIN" />
  120. <security:protect method="org.springframework.security.config.TestBusinessBean.get*" access="ROLE_ADMIN,ROLE_USER" />
  121. <security:protect method="org.springframework.security.config.TestBusinessBean.doSomething" access="ROLE_USER" />
  122. </security:intercept-methods>
  123. </bean>
  124. +-----------------------------------------------------------------------------------------------------------------------
  125. Spring's AbstractInterceptorDrivenBeanDefinitionDecorator is used to add a MethodSecurityInterceptor to the bean.
  126. Ideally we would just have the method names here rather than fully-qualified names (any ideas??).
  127. * Authentication Manager and Providers
  128. At least one authentication provider must be defined for things to work. No providers defined as
  129. beans will currently be added to the authentication manager, but this could be done with a post
  130. processor. Alternatively, \<security:authentication-provider /\> could be used round a bean declaration, or
  131. supplied with a reference to a provider bean.
  132. * Configuration of Specific Beans
  133. As mentioned above, it is also useful to be able to use namespaces to simplify the configuration of existing beans,
  134. especially where the existing configuration is complicated. This would typically be done with bean decorators.
  135. An example of this is the simplification of FilterChainProxy configuration.
  136. ** FilterChainProxy Configuration
  137. The \<security:filter-chain-map /\> decorator sets the configuration map of paths to filter lists for FilterChainProxy. The
  138. syntax is similar to that described above for the HTTP security features.
  139. +-----------------------------------------------------------------------------------------------------------------------
  140. <bean id="filterChainProxy" class="org.springframework.security.util.FilterChainProxy">
  141. <sec:filter-chain-map pathType="ant">
  142. <sec:filter-chain pattern="/foo/**" filters="mockFilter"/>
  143. <sec:filter-chain pattern="/some/other/path/**" filters="httpSessionFilter,mockFilter,mockFilter2"/>
  144. <sec:filter-chain pattern="/do/not/filter" filters="none"/>
  145. <sec:filter-chain pattern="/**" filters="sif,apf,mockFilter"/>
  146. </sec:filter-chain-map>
  147. </bean>
  148. +-----------------------------------------------------------------------------------------------------------------------
  149. This kind of functionality can be added as requested/required without having an impact on other areas, so it is of
  150. lower priority than the design of "higher-level" namespace components.
  151. * LDAP Configuration Example
  152. As an example of what is possible in terms of reducing configuration requirements, the \<security:ldap /\>
  153. element is an excellent example of the use of high-level namespace components. It can be used to set up a complete
  154. LDAP authentication provider with or without an external server.
  155. The optional <<<url>>> attribute specifies the URL of an external LDAP server. Without this, it will create
  156. an embedded Apache Directory server instance and attempt to load any ldif files found on the classpath. Doing this
  157. with Spring beans would involve potentially hundreds of lines of configuration and is a difficult task for an
  158. inexperienced user (or indeed any user). Ultimately the user should be able to configure authentication and group
  159. membership strategy, server details (e.g. port number) and the structure of the directory.
  160. {{{http://acegisecurity.svn.sourceforge.net/svnroot/acegisecurity/spring-security/trunk/core/src/main/java/org/springframework/security/config/LdapBeanDefinitionParser.java}LdapBeanDefinitionParser}}
  161. does the parsing work here and there are currently no child elements.