namespace-config.xml 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <chapter xmlns="http://docbook.org/ns/docbook" version="5.0" xml:id="ns-config"
  3. xmlns:xlink="http://www.w3.org/1999/xlink">
  4. <info>
  5. <title>Security Namespace Configuration</title>
  6. </info>
  7. <section>
  8. <title>Introduction</title>
  9. <para> Namespace configuration has been available since version 2.0 of the Spring framework.
  10. It allows you to supplement the traditional Spring beans application context syntax with
  11. elements from additional XML schema. You can find more information in the Spring <link
  12. xlink:href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/htmlsingle/spring-framework-reference.html"
  13. > Reference Documentation</link>. A namespace element can be used simply to allow a more
  14. concise way of configuring an individual bean or, more powerfully, to define an
  15. alternative configuration syntax which more closely matches the problem domain and hides
  16. the underlying complexity from the user. A simple element may conceal the fact that
  17. multiple beans and processing steps are being added to the application context. For
  18. example, adding the following element from the security namespace to an application
  19. context will start up an embedded LDAP server for testing use within the application: <programlisting language="xml"><![CDATA[
  20. <security:ldap-server />
  21. ]]></programlisting> This is much simpler than wiring up the equivalent Apache Directory Server
  22. beans. The most common alternative configuration requirements are supported by
  23. attributes on the <literal>ldap-server</literal> element and the user is isolated from
  24. worrying about which beans they need to create and what the bean property names are. <footnote>
  25. <para>You can find out more about the use of the <literal>ldap-server</literal> element
  26. in the chapter on <link xlink:href="#ldap">LDAP</link>.</para>
  27. </footnote>. Use of a good XML editor while editing the application context file should
  28. provide information on the attributes and elements that are available. We would
  29. recommend that you try out the <link
  30. xlink:href="http://www.springsource.com/products/sts">SpringSource Tool Suite</link> as
  31. it has special features for working with standard Spring namespaces. </para>
  32. <para> To start using the security namespace in your application context, you need to have
  33. the <literal>spring-security-config</literal> jar on your classpath. Then all you need
  34. to do is add the schema declaration to your application context file: <programlisting language="xml">
  35. <![CDATA[
  36. <beans xmlns="http://www.springframework.org/schema/beans"
  37. xmlns:security="http://www.springframework.org/schema/security"
  38. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  39. xsi:schemaLocation="http://www.springframework.org/schema/beans
  40. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  41. http://www.springframework.org/schema/security
  42. http://www.springframework.org/schema/security/spring-security-3.1.xsd">
  43. ...
  44. </beans>
  45. ]]></programlisting> In many of the examples you will see (and in the sample) applications, we
  46. will often use "security" as the default namespace rather than "beans", which means we
  47. can omit the prefix on all the security namespace elements, making the content easier to
  48. read. You may also want to do this if you have your application context divided up into
  49. separate files and have most of your security configuration in one of them. Your
  50. security application context file would then start like this <programlisting language="xml"><![CDATA[
  51. <beans:beans xmlns="http://www.springframework.org/schema/security"
  52. xmlns:beans="http://www.springframework.org/schema/beans"
  53. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  54. xsi:schemaLocation="http://www.springframework.org/schema/beans
  55. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  56. http://www.springframework.org/schema/security
  57. http://www.springframework.org/schema/security/spring-security-3.1.xsd">
  58. ...
  59. </beans:beans>
  60. ]]></programlisting> We'll assume this syntax is being used from now on in this chapter. </para>
  61. <section>
  62. <title>Design of the Namespace</title>
  63. <para> The namespace is designed to capture the most common uses of the framework and
  64. provide a simplified and concise syntax for enabling them within an application. The
  65. design is based around the large-scale dependencies within the framework, and can be
  66. divided up into the following areas: <itemizedlist>
  67. <listitem>
  68. <para> <emphasis>Web/HTTP Security</emphasis> - the most complex part. Sets up
  69. the filters and related service beans used to apply the framework
  70. authentication mechanisms, to secure URLs, render login and error pages and
  71. much more.</para>
  72. </listitem>
  73. <listitem>
  74. <para> <emphasis>Business Object (Method) Security</emphasis> - options for
  75. securing the service layer.</para>
  76. </listitem>
  77. <listitem>
  78. <para> <emphasis>AuthenticationManager</emphasis> - handles authentication
  79. requests from other parts of the framework.</para>
  80. </listitem>
  81. <listitem>
  82. <para> <emphasis>AccessDecisionManager</emphasis> - provides access decisions
  83. for web and method security. A default one will be registered, but you can
  84. also choose to use a custom one, declared using normal Spring bean
  85. syntax.</para>
  86. </listitem>
  87. <listitem>
  88. <para> <emphasis>AuthenticationProvider</emphasis>s - mechanisms against which
  89. the authentication manager authenticates users. The namespace provides
  90. supports for several standard options and also a means of adding custom
  91. beans declared using a traditional syntax. </para>
  92. </listitem>
  93. <listitem>
  94. <para> <emphasis>UserDetailsService</emphasis> - closely related to
  95. authentication providers, but often also required by other beans.</para>
  96. </listitem>
  97. <!-- todo: diagram and link to other sections which describe the interfaces -->
  98. </itemizedlist></para>
  99. <para>We'll see how to configure these in the following sections.</para>
  100. </section>
  101. </section>
  102. <section xml:id="ns-getting-started">
  103. <title>Getting Started with Security Namespace Configuration</title>
  104. <para> In this section, we'll look at how you can build up a namespace configuration to use
  105. some of the main features of the framework. Let's assume you initially want to get up
  106. and running as quickly as possible and add authentication support and access control to
  107. an existing web application, with a few test logins. Then we'll look at how to change
  108. over to authenticating against a database or other security repository. In later
  109. sections we'll introduce more advanced namespace configuration options. </para>
  110. <section xml:id="ns-web-xml">
  111. <title><literal>web.xml</literal> Configuration</title>
  112. <para> The first thing you need to do is add the following filter declaration to your
  113. <literal>web.xml</literal> file: <programlisting language="xml"><![CDATA[
  114. <filter>
  115. <filter-name>springSecurityFilterChain</filter-name>
  116. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  117. </filter>
  118. <filter-mapping>
  119. <filter-name>springSecurityFilterChain</filter-name>
  120. <url-pattern>/*</url-pattern>
  121. </filter-mapping>]]>
  122. </programlisting> This provides a hook into the Spring Security web
  123. infrastructure. <classname>DelegatingFilterProxy</classname> is a Spring Framework
  124. class which delegates to a filter implementation which is defined as a Spring bean
  125. in your application context. In this case, the bean is named
  126. <quote>springSecurityFilterChain</quote>, which is an internal infrastructure bean
  127. created by the namespace to handle web security. Note that you should not use this
  128. bean name yourself. Once you've added this to your <filename>web.xml</filename>,
  129. you're ready to start editing your application context file. Web security services
  130. are configured using the <literal>&lt;http&gt;</literal> element. </para>
  131. </section>
  132. <section xml:id="ns-minimal">
  133. <title>A Minimal <literal>&lt;http&gt;</literal> Configuration</title>
  134. <para> All you need to enable web security to begin with is <programlisting language="xml"><![CDATA[
  135. <http auto-config='true'>
  136. <intercept-url pattern="/**" access="ROLE_USER" />
  137. </http>
  138. ]]>
  139. </programlisting> Which says that we want all URLs within our application to be secured,
  140. requiring the role <literal>ROLE_USER</literal> to access them. The
  141. <literal>&lt;http></literal> element is the parent for all web-related namespace
  142. functionality. The <literal>&lt;intercept-url></literal> element defines a
  143. <literal>pattern</literal> which is matched against the URLs of incoming requests
  144. using an ant path style syntax<footnote>
  145. <para>See the section on <link xlink:href="#request-matching">Request
  146. Matching</link> in the Web Application Infrastructure chapter for more details
  147. on how matches are actually performed.</para>
  148. </footnote>. You can also use regular-expression matching as an alternative (see the
  149. namespace appendix for more details). The <literal>access</literal> attribute
  150. defines the access requirements for requests matching the given pattern. With the
  151. default configuration, this is typically a comma-separated list of roles, one of
  152. which a user must have to be allowed to make the request. The prefix
  153. <quote>ROLE_</quote> is a marker which indicates that a simple comparison with the
  154. user's authorities should be made. In other words, a normal role-based check should
  155. be used. Access-control in Spring Security is not limited to the use of simple roles
  156. (hence the use of the prefix to differentiate between different types of security
  157. attributes). We'll see later how the interpretation can vary<footnote>
  158. <para>The interpretation of the comma-separated values in the
  159. <literal>access</literal> attribute depends on the implementation of the <link
  160. xlink:href="#ns-access-manager">AccessDecisionManager</link> which is used. In
  161. Spring Security 3.0, the attribute can also be populated with an <link
  162. xlink:href="#el-access">EL expression</link>.</para>
  163. </footnote>.</para>
  164. <note>
  165. <para>You can use multiple <literal>&lt;intercept-url&gt;</literal> elements to
  166. define different access requirements for different sets of URLs, but they will
  167. be evaluated in the order listed and the first match will be used. So you must
  168. put the most specific matches at the top. You can also add a
  169. <literal>method</literal> attribute to limit the match to a particular HTTP
  170. method (<literal>GET</literal>, <literal>POST</literal>, <literal>PUT</literal>
  171. etc.). If a request matches multiple patterns, the method-specific match will
  172. take precedence regardless of ordering.</para>
  173. </note>
  174. <para> To add some users, you can define a set of test data directly in the namespace: <programlisting language="xml"><![CDATA[
  175. <authentication-manager>
  176. <authentication-provider>
  177. <user-service>
  178. <user name="jimi" password="jimispassword" authorities="ROLE_USER, ROLE_ADMIN" />
  179. <user name="bob" password="bobspassword" authorities="ROLE_USER" />
  180. </user-service>
  181. </authentication-provider>
  182. </authentication-manager>
  183. ]]>
  184. </programlisting></para>
  185. <sidebar>
  186. <para>If you are familiar with pre-namespace versions of the framework, you can
  187. probably already guess roughly what's going on here. The
  188. <literal>&lt;http&gt;</literal> element is responsible for creating a
  189. <classname>FilterChainProxy</classname> and the filter beans which it uses.
  190. Common problems like incorrect filter ordering are no longer an issue as the
  191. filter positions are predefined.</para>
  192. <para>The <literal>&lt;authentication-provider&gt;</literal> element creates a
  193. <classname>DaoAuthenticationProvider</classname> bean and the
  194. <literal>&lt;user-service&gt;</literal> element creates an
  195. <classname>InMemoryDaoImpl</classname>. All
  196. <literal>authentication-provider</literal> elements must be children of the
  197. <literal>&lt;authentication-manager></literal> element, which creates a
  198. <classname>ProviderManager</classname> and registers the authentication
  199. providers with it. You can find more detailed information on the beans that are
  200. created in the <link xlink:href="#appendix-namespace">namespace appendix</link>.
  201. It's worth cross-checking this if you want to start understanding what the
  202. important classes in the framework are and how they are used, particularly if
  203. you want to customise things later.</para>
  204. </sidebar>
  205. <para> The configuration above defines two users, their passwords and their roles within
  206. the application (which will be used for access control). It is also possible to load
  207. user information from a standard properties file using the
  208. <literal>properties</literal> attribute on <literal>user-service</literal>. See the
  209. section on <link xlink:href="#core-services-in-memory-service">in-memory
  210. authentication</link> for more details on the file format. Using the
  211. <literal>&lt;authentication-provider&gt;</literal> element means that the user
  212. information will be used by the authentication manager to process authentication
  213. requests. You can have multiple <literal>&lt;authentication-provider&gt;</literal>
  214. elements to define different authentication sources and each will be consulted in
  215. turn.</para>
  216. <para> At this point you should be able to start up your application and you will be
  217. required to log in to proceed. Try it out, or try experimenting with the
  218. <quote>tutorial</quote> sample application that comes with the project. The above
  219. configuration actually adds quite a few services to the application because we have
  220. used the <literal>auto-config</literal> attribute. For example, form-based login
  221. processing is automatically enabled. </para>
  222. <section xml:id="ns-auto-config">
  223. <title>What does <literal>auto-config</literal> Include?</title>
  224. <para> The <literal>auto-config</literal> attribute, as we have used it above, is
  225. just a shorthand syntax for: <programlisting language="xml"><![CDATA[
  226. <http>
  227. <form-login />
  228. <http-basic />
  229. <logout />
  230. </http>
  231. ]]></programlisting> These other elements are responsible for setting up form-login, basic
  232. authentication and logout handling services respectively <footnote>
  233. <para>In versions prior to 3.0, this list also included remember-me
  234. functionality. This could cause some confusing errors with some
  235. configurations and was removed in 3.0. In 3.0, the addition of an
  236. <classname>AnonymousAuthenticationFilter</classname> is part of the default
  237. <literal>&lt;http></literal> configuration, so the <literal>&lt;anonymous
  238. /></literal> element is added regardless of whether
  239. <literal>auto-config</literal> is enabled.</para>
  240. </footnote>. They each have attributes which can be used to alter their
  241. behaviour. In anything other than very basic scenarios, it is probably better to
  242. omit the <literal>auto-config</literal> attribute and configure what you require
  243. explicitly in the interest of clarity.</para>
  244. </section>
  245. </section>
  246. <section xml:id="ns-form-and-basic">
  247. <title>Form and Basic Login Options</title>
  248. <para> You might be wondering where the login form came from when you were prompted to
  249. log in, since we made no mention of any HTML files or JSPs. In fact, since we didn't
  250. explicitly set a URL for the login page, Spring Security generates one
  251. automatically, based on the features that are enabled and using standard values for
  252. the URL which processes the submitted login, the default target URL the user will be
  253. sent to after loggin in and so on. However, the namespace offers plenty of support
  254. to allow you to customize these options. For example, if you want to supply your own
  255. login page, you could use: <programlisting language="xml"><![CDATA[
  256. <http auto-config='true'>
  257. <intercept-url pattern="/login.jsp*" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
  258. <intercept-url pattern="/**" access="ROLE_USER" />
  259. <form-login login-page='/login.jsp'/>
  260. </http>
  261. ]]>
  262. </programlisting> Note that you can still use <literal>auto-config</literal>. The
  263. <literal>form-login</literal> element just overrides the default settings. Also note
  264. that we've added an extra <literal>intercept-url</literal> element to say that any
  265. requests for the login page should be available to anonymous users <footnote>
  266. <para>See the chapter on <link xlink:href="#anonymous">anonymous
  267. authentication</link> and also the <link xlink:href="#authz-authenticated-voter"
  268. >AuthenticatedVoter</link> class for more details on how the value
  269. <literal>IS_AUTHENTICATED_ANONYMOUSLY</literal> is processed.</para>
  270. </footnote>. Otherwise the request would be matched by the pattern
  271. <literal>/**</literal> and it wouldn't be possible to access the login page itself!
  272. This is a common configuration error and will result in an infinite loop in the
  273. application. Spring Security will emit a warning in the log if your login page
  274. appears to be secured. It is also possible to have all requests matching a
  275. particular pattern bypass the security filter chain completely, by defining a
  276. separate <literal>http</literal> element for the pattern like this: <programlisting language="xml"><![CDATA[
  277. <http pattern="/css/**" security="none"/>
  278. <http pattern="/login.jsp*" security="none"/>
  279. <http auto-config='true'>
  280. <intercept-url pattern="/**" access="ROLE_USER" />
  281. <form-login login-page='/login.jsp'/>
  282. </http>
  283. ]]>
  284. </programlisting> From Spring Security 3.1 it is now possible to use multiple
  285. <literal>http</literal> elements to define separate security filter chain
  286. configurations for different request patterns. If the <literal>pattern</literal>
  287. attribute is omitted from an <literal>http</literal> element, it matches all
  288. requests. Creating an unsecured pattern is a simple example of this syntax, where
  289. the pattern is mapped to an empty filter chain <footnote>
  290. <para>The use of multiple <literal>&lt;http&gt;</literal> elements is an important
  291. feature, allowing the namespace to simultaneously support both stateful and
  292. stateless paths within the same application, for example. The previous syntax,
  293. using the attribute <literal>filters="none"</literal> on an
  294. <literal>intercept-url</literal> element is incompatible with this change and is
  295. no longer supported in 3.1.</para>
  296. </footnote>. We'll look at this new syntax in more detail in the chapter on the
  297. <link xlink:href="#filter-chains-with-ns">Security Filter Chain</link>. </para>
  298. <para> It's important to realise that these unsecured requests will be completely
  299. oblivious to any Spring Security web-related configuration or additional attributes
  300. such as <literal>requires-channel</literal>, so you will not be able to access
  301. information on the current user or call secured methods during the request. Use
  302. <literal>access='IS_AUTHENTICATED_ANONYMOUSLY'</literal> as an alternative if you
  303. still want the security filter chain to be applied.</para>
  304. <para>If you want to use basic authentication instead of form login, then change the
  305. configuration to <programlisting language="xml"><![CDATA[
  306. <http auto-config='true'>
  307. <intercept-url pattern="/**" access="ROLE_USER" />
  308. <http-basic />
  309. </http>
  310. ]]>
  311. </programlisting> Basic authentication will then take precedence and will be used to
  312. prompt for a login when a user attempts to access a protected resource. Form login
  313. is still available in this configuration if you wish to use it, for example through
  314. a login form embedded in another web page. </para>
  315. <section xml:id="ns-form-target">
  316. <title>Setting a Default Post-Login Destination</title>
  317. <para> If a form login isn't prompted by an attempt to access a protected resource,
  318. the <literal>default-target-url</literal> option comes into play. This is the
  319. URL the user will be taken to after successfully logging in, and defaults to
  320. "/". You can also configure things so that the user <emphasis>always</emphasis>
  321. ends up at this page (regardless of whether the login was "on-demand" or they
  322. explicitly chose to log in) by setting the
  323. <literal>always-use-default-target</literal> attribute to "true". This is useful
  324. if your application always requires that the user starts at a "home" page, for
  325. example: <programlisting language="xml"><![CDATA[
  326. <http pattern="/login.htm*" security="none"/>
  327. <http>
  328. <intercept-url pattern='/**' access='ROLE_USER' />
  329. <form-login login-page='/login.htm' default-target-url='/home.htm'
  330. always-use-default-target='true' />
  331. </http>
  332. ]]> </programlisting></para>
  333. <para>For even more control over the destination, you can use the
  334. <literal>authentication-success-handler-ref</literal> attribute as an
  335. alternative to <literal>default-target-url</literal>. The referenced bean should
  336. be an instance of <interfacename>AuthenticationSuccessHandler</interfacename>.
  337. You'll find more on this in the <link xlink:href="#form-login-flow-handling"
  338. >Core Filters</link> chapter and also in the namespace appendix, as well as
  339. information on how to customize the flow when authentication fails. </para>
  340. </section>
  341. </section>
  342. <section xml:id="ns-logout">
  343. <title>Logout Handling</title>
  344. <para>The <literal>logout</literal> element adds support for logging out by navigating
  345. to a particular URL. The default logout URL is <literal>/j_spring_security_logout</literal>,
  346. but you can set it to something else using the <literal>logout-url</literal> attribute.
  347. More information on other available attributes may be found in the namespace appendix.
  348. </para>
  349. </section>
  350. <section xml:id="ns-auth-providers">
  351. <title>Using other Authentication Providers</title>
  352. <para> In practice you will need a more scalable source of user information than a few
  353. names added to the application context file. Most likely you will want to store your
  354. user information in something like a database or an LDAP server. LDAP namespace
  355. configuration is dealt with in the <link xlink:href="#ldap">LDAP chapter</link>, so
  356. we won't cover it here. If you have a custom implementation of Spring Security's
  357. <classname>UserDetailsService</classname>, called "myUserDetailsService" in your
  358. application context, then you can authenticate against this using <programlisting language="xml"><![CDATA[
  359. <authentication-manager>
  360. <authentication-provider user-service-ref='myUserDetailsService'/>
  361. </authentication-manager>
  362. ]]>
  363. </programlisting> If you want to use a database, then you can use <programlisting language="xml"><![CDATA[
  364. <authentication-manager>
  365. <authentication-provider>
  366. <jdbc-user-service data-source-ref="securityDataSource"/>
  367. </authentication-provider>
  368. </authentication-manager>
  369. ]]>
  370. </programlisting> Where <quote>securityDataSource</quote> is the name of a
  371. <classname>DataSource</classname> bean in the application context, pointing at a
  372. database containing the standard Spring Security <link
  373. xlink:href="#db_schema_users_authorities">user data tables</link>. Alternatively,
  374. you could configure a Spring Security <classname>JdbcDaoImpl</classname> bean and
  375. point at that using the <literal>user-service-ref</literal> attribute: <programlisting language="xml"><![CDATA[
  376. <authentication-manager>
  377. <authentication-provider user-service-ref='myUserDetailsService'/>
  378. </authentication-manager>
  379. <beans:bean id="myUserDetailsService"
  380. class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl">
  381. <beans:property name="dataSource" ref="dataSource"/>
  382. </beans:bean>
  383. ]]>
  384. </programlisting> You can also use standard
  385. <interfacename>AuthenticationProvider</interfacename> beans as follows <programlisting language="xml"><![CDATA[
  386. <authentication-manager>
  387. <authentication-provider ref='myAuthenticationProvider'/>
  388. </authentication-manager>
  389. ]]>
  390. </programlisting> where <literal>myAuthenticationProvider</literal> is the name of a
  391. bean in your application context which implements
  392. <interfacename>AuthenticationProvider</interfacename>. You can use multiple
  393. <literal>authentication-provider</literal> elements, in which case the providers
  394. will be queried in the order they are declared. See <xref linkend="ns-auth-manager"
  395. /> for more on information on how the Spring Security
  396. <interfacename>AuthenticationManager</interfacename> is configured using the
  397. namespace. </para>
  398. <section xml:id="ns-password-encoder">
  399. <title>Adding a Password Encoder</title>
  400. <para> Often your password data will be encoded using a hashing algorithm. This is
  401. supported by the <literal>&lt;password-encoder&gt;</literal> element. With SHA
  402. encoded passwords, the original authentication provider configuration would look
  403. like this: <programlisting language="xml"><![CDATA[
  404. <authentication-manager>
  405. <authentication-provider>
  406. <password-encoder hash="sha"/>
  407. <user-service>
  408. <user name="jimi" password="d7e6351eaa13189a5a3641bab846c8e8c69ba39f"
  409. authorities="ROLE_USER, ROLE_ADMIN" />
  410. <user name="bob" password="4e7421b1b8765d8f9406d87e7cc6aa784c4ab97f"
  411. authorities="ROLE_USER" />
  412. </user-service>
  413. </authentication-provider>
  414. </authentication-manager>
  415. ]]>
  416. </programlisting></para>
  417. <para> When using hashed passwords, it's also a good idea to use a salt value to
  418. protect against dictionary attacks and Spring Security supports this too.
  419. Ideally you would want to use a randomly generated salt value for each user, but
  420. you can use any property of the <classname>UserDetails</classname> object which
  421. is loaded by your <classname>UserDetailsService</classname>. For example, to use
  422. the <literal>username</literal> property, you would use <programlisting><![CDATA[
  423. <password-encoder hash="sha">
  424. <salt-source user-property="username"/>
  425. </password-encoder>
  426. ]]></programlisting> You can use a custom password encoder bean by using the
  427. <literal>ref</literal> attribute of <literal>password-encoder</literal>. This
  428. should contain the name of a bean in the application context which is an
  429. instance of Spring Security's <interfacename>PasswordEncoder</interfacename>
  430. interface. </para>
  431. </section>
  432. </section>
  433. </section>
  434. <section xml:id="ns-web-advanced">
  435. <title>Advanced Web Features</title>
  436. <section xml:id="ns-remember-me">
  437. <title>Remember-Me Authentication</title>
  438. <para>See the separate <link xlink:href="#remember-me">Remember-Me chapter</link> for
  439. information on remember-me namespace configuration.</para>
  440. </section>
  441. <section xml:id="ns-requires-channel">
  442. <title>Adding HTTP/HTTPS Channel Security</title>
  443. <para>If your application supports both HTTP and HTTPS, and you require that particular
  444. URLs can only be accessed over HTTPS, then this is directly supported using the
  445. <literal>requires-channel</literal> attribute on
  446. <literal>&lt;intercept-url&gt;</literal>: <programlisting language="xml"><![CDATA[
  447. <http>
  448. <intercept-url pattern="/secure/**" access="ROLE_USER" requires-channel="https"/>
  449. <intercept-url pattern="/**" access="ROLE_USER" requires-channel="any"/>
  450. ...
  451. </http>]]>
  452. </programlisting>With this configuration in place, if a user attempts to access
  453. anything matching the "/secure/**" pattern using HTTP, they will first be redirected
  454. to an HTTPS URL
  455. <footnote><para>For more details on how channel-processing is implemented, see the Javadoc
  456. for <classname>ChannelProcessingFilter</classname> and related classes.
  457. </para></footnote>.
  458. The available options are "http", "https" or "any". Using the value
  459. "any" means that either HTTP or HTTPS can be used. </para>
  460. <para>If your application uses non-standard ports for HTTP and/or HTTPS, you can specify
  461. a list of port mappings as follows: <programlisting><![CDATA[
  462. <http>
  463. ...
  464. <port-mappings>
  465. <port-mapping http="9080" https="9443"/>
  466. </port-mappings>
  467. </http>]]>
  468. </programlisting><!--You can find a more in-depth discussion of channel security
  469. in <xref xlink:href="#channel-security"/--></para>
  470. </section>
  471. <section xml:id="ns-session-mgmt">
  472. <title>Session Management</title>
  473. <section>
  474. <title>Detecting Timeouts</title>
  475. <para> You can configure Spring Security to detect the submission of an invalid
  476. session ID and redirect the user to an appropriate URL. This is achieved through
  477. the <literal>session-management</literal> element: <programlisting language="xml"><![CDATA[
  478. <http>
  479. ...
  480. <session-management invalid-session-url="/invalidSession.htm" />
  481. </http>]]></programlisting>Note that if you use this mechanism to detect session timeouts, it
  482. may falsely report an error if the user logs out and then logs back in without
  483. closing the browser. This is because the session cookie is not cleared when you
  484. invalidate the session and will be resubmitted even if the user has logged out.
  485. You may be able to explicitly delete the JSESSIONID cookie on logging out, for
  486. example by using the following syntax in the logout handler: <programlisting language="xml"><![CDATA[
  487. <http>
  488. <logout delete-cookies="JSESSIONID" />
  489. </http>
  490. ]]></programlisting> Unfortunately this can't be guaranteed to work with every servlet container,
  491. so you will need to test it in your environment<footnote>
  492. <para>If you are running your application behind a proxy, you may also be able
  493. to remove the session cookie by configuring the proxy server. For example,
  494. using Apache HTTPD's mod_headers, the following directive would delete the
  495. <literal>JSESSIONID</literal> cookie by expiring it in the response to a
  496. logout request (assuming the application is deployed under the path
  497. <literal>/tutorial</literal>):
  498. <programlisting> &lt;LocationMatch "/tutorial/j_spring_security_logout">
  499. Header always set Set-Cookie "JSESSIONID=;Path=/tutorial;Expires=Thu, 01 Jan 1970 00:00:00 GMT"
  500. &lt;/LocationMatch></programlisting></para>
  501. </footnote>. </para>
  502. </section>
  503. <section xml:id="ns-concurrent-sessions">
  504. <title>Concurrent Session Control</title>
  505. <para>If you wish to place constraints on a single user's ability to log in to your
  506. application, Spring Security supports this out of the box with the following
  507. simple additions. First you need to add the following listener to your
  508. <filename>web.xml</filename> file to keep Spring Security updated about session
  509. lifecycle events: <programlisting language="xml"><![CDATA[
  510. <listener>
  511. <listener-class>
  512. org.springframework.security.web.session.HttpSessionEventPublisher
  513. </listener-class>
  514. </listener>
  515. ]]></programlisting> Then add the following lines to your application context: <programlisting language="xml"><![CDATA[
  516. <http>
  517. ...
  518. <session-management>
  519. <concurrency-control max-sessions="1" />
  520. </session-management>
  521. </http>]]>
  522. </programlisting> This will prevent a user from logging in multiple times - a
  523. second login will cause the first to be invalidated. Often you would prefer to
  524. prevent a second login, in which case you can use <programlisting language="xml"><![CDATA[
  525. <http>
  526. ...
  527. <session-management>
  528. <concurrency-control max-sessions="1" error-if-maximum-exceeded="true" />
  529. </session-management>
  530. </http>]]>
  531. </programlisting>The second login will then be rejected. By
  532. <quote>rejected</quote>, we mean that the user will be sent to the
  533. <literal>authentication-failure-url</literal> if form-based login is being used.
  534. If the second authentication takes place through another non-interactive
  535. mechanism, such as <quote>remember-me</quote>, an <quote>unauthorized</quote>
  536. (402) error will be sent to the client. If instead you want to use an error
  537. page, you can add the attribute
  538. <literal>session-authentication-error-url</literal> to the
  539. <literal>session-management</literal> element. </para>
  540. <para>If you are using a customized authentication filter for form-based login, then
  541. you have to configure concurrent session control support explicitly. More
  542. details can be found in the <link xlink:href="#session-mgmt">Session Management
  543. chapter</link>. </para>
  544. </section>
  545. <section xml:id="ns-session-fixation">
  546. <title>Session Fixation Attack Protection</title>
  547. <para> <link xlink:href="http://en.wikipedia.org/wiki/Session_fixation">Session
  548. fixation</link> attacks are a potential risk where it is possible for a
  549. malicious attacker to create a session by accessing a site, then persuade
  550. another user to log in with the same session (by sending them a link containing
  551. the session identifier as a parameter, for example). Spring Security protects
  552. against this automatically by creating a new session when a user logs in. If you
  553. don't require this protection, or it conflicts with some other requirement, you
  554. can control the behaviour using the
  555. <literal>session-fixation-protection</literal> attribute on
  556. <literal>&lt;session-management&gt;</literal>, which has three options <itemizedlist>
  557. <listitem>
  558. <para><literal>migrateSession</literal> - creates a new session and copies
  559. the existing session attributes to the new session. This is the
  560. default.</para>
  561. </listitem>
  562. <listitem>
  563. <para><literal>none</literal> - Don't do anything. The original session will
  564. be retained.</para>
  565. </listitem>
  566. <listitem>
  567. <para><literal>newSession</literal> - Create a new "clean" session, without
  568. copying the existing session data.</para>
  569. </listitem>
  570. </itemizedlist>
  571. See the <link xlink:href="#session-mgmt">Session Management</link> chapter for
  572. additional information.
  573. </para>
  574. </section>
  575. </section>
  576. <section xml:id="ns-openid">
  577. <title>OpenID Support</title>
  578. <para>The namespace supports <link xlink:href="http://openid.net/">OpenID</link> login
  579. either instead of, or in addition to normal form-based login, with a simple change: <programlisting language="xml"><![CDATA[
  580. <http>
  581. <intercept-url pattern="/**" access="ROLE_USER" />
  582. <openid-login />
  583. </http>
  584. ]]></programlisting>You should then register yourself with an OpenID provider (such as
  585. myopenid.com), and add the user information to your in-memory
  586. <literal>&lt;user-service&gt;</literal> : <programlisting language="xml"><![CDATA[
  587. <user name="http://jimi.hendrix.myopenid.com/" authorities="ROLE_USER" />
  588. ]]></programlisting> You should be able to login using the <literal>myopenid.com</literal> site to
  589. authenticate. It is also possible to select a specific
  590. <interfacename>UserDetailsService</interfacename> bean for use OpenID by setting the
  591. <literal>user-service-ref</literal> attribute on the <literal>openid-login</literal>
  592. element. See the previous section on <link xlink:href="#ns-auth-providers"
  593. >authentication providers</link> for more information. Note that we have omitted the
  594. password attribute from the above user configuration, since this set of user data is
  595. only being used to load the authorities for the user. A random password will be
  596. generate internally, preventing you from accidentally using this user data as an
  597. authentication source elsewhere in your configuration.</para>
  598. <section>
  599. <title>Attribute Exchange</title>
  600. <para>Support for OpenID <link
  601. xlink:href="http://openid.net/specs/openid-attribute-exchange-1_0.html"
  602. >attribute exchange</link>. As an example, the following configuration would
  603. attempt to retrieve the email and full name from the OpenID provider, for use by
  604. the application:<programlisting language="xml"><![CDATA[
  605. <openid-login>
  606. <attribute-exchange>
  607. <openid-attribute name="email" type="http://axschema.org/contact/email" required="true"/>
  608. <openid-attribute name="name" type="http://axschema.org/namePerson"/>
  609. </attribute-exchange>
  610. </openid-login>]]></programlisting>The <quote>type</quote> of each OpenID attribute is a URI,
  611. determined by a particular schema, in this case <link
  612. xlink:href="http://axschema.org/">http://axschema.org/</link>. If an attribute
  613. must be retrieved for successful authentication, the <literal>required</literal>
  614. attribute can be set. The exact schema and attributes supported will depend on
  615. your OpenID provider. The attribute values are returned as part of the
  616. authentication process and can be accessed afterwards using the following code:
  617. <programlisting language="java">
  618. OpenIDAuthenticationToken token =
  619. (OpenIDAuthenticationToken)SecurityContextHolder.getContext().getAuthentication();
  620. List&lt;OpenIDAttribute> attributes = token.getAttributes();</programlisting>The
  621. <classname>OpenIDAttribute</classname> contains the attribute type and the
  622. retrieved value (or values in the case of multi-valued attributes). We'll see
  623. more about how the <classname>SecurityContextHolder</classname> class is used
  624. when we look at core Spring Security components in the <link
  625. xlink:href="#core-components">technical overview</link> chapter. Multiple
  626. attribute exchange configurations are also be supported, if you wish to use
  627. multiple identity providers. You can supply multiple
  628. <literal>attribute-exchange</literal> elements, using an
  629. <literal>identifier-matcher</literal> attribute on each. This contains a regular
  630. expression which will be matched against the OpenID identifier supplied by the
  631. user. See the OpenID sample application in the codebase for an example
  632. configuration, providing different attribute lists for the Google, Yahoo and
  633. MyOpenID providers.</para>
  634. </section>
  635. </section>
  636. <section xml:id="ns-custom-filters">
  637. <title>Adding in Your Own Filters</title>
  638. <para>If you've used Spring Security before, you'll know that the framework maintains a
  639. chain of filters in order to apply its services. You may want to add your own
  640. filters to the stack at particular locations or use a Spring Security filter for
  641. which there isn't currently a namespace configuration option (CAS, for example). Or
  642. you might want to use a customized version of a standard namespace filter, such as
  643. the <classname>UsernamePasswordAuthenticationFilter</classname> which is created by the
  644. <literal>&lt;form-login&gt;</literal> element, taking advantage of some of the extra
  645. configuration options which are available by using the bean explicitly. How can you
  646. do this with namespace configuration, since the filter chain is not directly
  647. exposed? </para>
  648. <para>The order of the filters is always strictly enforced when using the namespace.
  649. When the application context is being created, the filter beans are sorted by the
  650. namespace handling code and the standard Spring Security filters each have an alias
  651. in the namespace and a well-known position.<note>
  652. <para>In previous versions, the sorting took place after the filter instances had
  653. been created, during post-processing of the application context. In version 3.0+
  654. the sorting is now done at the bean metadata level, before the classes have been
  655. instantiated. This has implications for how you add your own filters to the
  656. stack as the entire filter list must be known during the parsing of the
  657. <literal>&lt;http></literal> element, so the syntax has changed slightly in
  658. 3.0.</para>
  659. </note>The filters, aliases and namespace elements/attributes which create the
  660. filters are shown in <xref linkend="filter-stack"/>. The filters are listed in the
  661. order in which they occur in the filter chain. <table xml:id="filter-stack">
  662. <title>Standard Filter Aliases and Ordering</title>
  663. <tgroup cols="3" align="left">
  664. <colspec colnum="1" colname="col1" colwidth="2*"/>
  665. <colspec colnum="2" colname="col2" colwidth="2*"/>
  666. <colspec colnum="3" colname="col3" colwidth="1*"/>
  667. <thead>
  668. <row>
  669. <entry align="center">Alias</entry>
  670. <entry align="center">Filter Class</entry>
  671. <entry align="center">Namespace Element or Attribute</entry>
  672. </row>
  673. </thead>
  674. <tbody>
  675. <row>
  676. <entry> CHANNEL_FILTER</entry>
  677. <entry><literal>ChannelProcessingFilter</literal></entry>
  678. <entry><literal>http/intercept-url@requires-channel</literal></entry>
  679. </row>
  680. <row>
  681. <entry> CONCURRENT_SESSION_FILTER</entry>
  682. <entry><literal>ConcurrentSessionFilter</literal> </entry>
  683. <entry><literal>session-management/concurrency-control</literal></entry>
  684. </row>
  685. <row>
  686. <entry> SECURITY_CONTEXT_FILTER</entry>
  687. <entry><classname>SecurityContextPersistenceFilter</classname></entry>
  688. <entry><literal>http</literal></entry>
  689. </row>
  690. <row>
  691. <entry> LOGOUT_FILTER </entry>
  692. <entry><literal>LogoutFilter</literal></entry>
  693. <entry><literal>http/logout</literal></entry>
  694. </row>
  695. <row>
  696. <entry> X509_FILTER </entry>
  697. <entry><literal>X509AuthenticationFilter</literal></entry>
  698. <entry><literal>http/x509</literal></entry>
  699. </row>
  700. <row>
  701. <entry> PRE_AUTH_FILTER </entry>
  702. <entry><literal>AstractPreAuthenticatedProcessingFilter</literal>
  703. Subclasses</entry>
  704. <entry>N/A</entry>
  705. </row>
  706. <row>
  707. <entry> CAS_FILTER </entry>
  708. <entry><literal>CasAuthenticationFilter</literal></entry>
  709. <entry>N/A</entry>
  710. </row>
  711. <row>
  712. <entry> FORM_LOGIN_FILTER </entry>
  713. <entry><literal>UsernamePasswordAuthenticationFilter</literal></entry>
  714. <entry><literal>http/form-login</literal></entry>
  715. </row>
  716. <row>
  717. <entry> BASIC_AUTH_FILTER </entry>
  718. <entry><literal>BasicAuthenticationFilter</literal></entry>
  719. <entry><literal>http/http-basic</literal></entry>
  720. </row>
  721. <row>
  722. <entry> SERVLET_API_SUPPORT_FILTER</entry>
  723. <entry><literal>SecurityContextHolderAwareFilter</literal></entry>
  724. <entry><literal>http/@servlet-api-provision</literal></entry>
  725. </row>
  726. <row>
  727. <entry>JAAS_API_SUPPORT_FILTER</entry>
  728. <entry><literal>JaasApiIntegrationFilter</literal></entry>
  729. <entry><literal>http/@jaas-api-provision</literal></entry>
  730. </row>
  731. <row>
  732. <entry> REMEMBER_ME_FILTER </entry>
  733. <entry><classname>RememberMeAuthenticationFilter</classname></entry>
  734. <entry><literal>http/remember-me</literal></entry>
  735. </row>
  736. <row>
  737. <entry> ANONYMOUS_FILTER </entry>
  738. <entry><literal>AnonymousAuthenticationFilter</literal></entry>
  739. <entry><literal>http/anonymous</literal></entry>
  740. </row>
  741. <row>
  742. <entry> SESSION_MANAGEMENT_FILTER</entry>
  743. <entry><literal>SessionManagementFilter</literal></entry>
  744. <entry><literal>session-management</literal></entry>
  745. </row>
  746. <row>
  747. <entry>EXCEPTION_TRANSLATION_FILTER </entry>
  748. <entry><classname>ExceptionTranslationFilter</classname></entry>
  749. <entry><literal>http</literal></entry>
  750. </row>
  751. <row>
  752. <entry> FILTER_SECURITY_INTERCEPTOR </entry>
  753. <entry><classname>FilterSecurityInterceptor</classname></entry>
  754. <entry><literal>http</literal></entry>
  755. </row>
  756. <row>
  757. <entry> SWITCH_USER_FILTER </entry>
  758. <entry><literal>SwitchUserFilter</literal></entry>
  759. <entry>N/A</entry>
  760. </row>
  761. </tbody>
  762. </tgroup>
  763. </table> You can add your own filter to the stack, using the
  764. <literal>custom-filter</literal> element and one of these names to specify the
  765. position your filter should appear at: <programlisting language="xml"><![CDATA[
  766. <http>
  767. <custom-filter position="FORM_LOGIN_FILTER" ref="myFilter" />
  768. </http>
  769. <beans:bean id="myFilter" class="com.mycompany.MySpecialAuthenticationFilter"/>
  770. ]]>
  771. </programlisting> You can also use the <literal>after</literal> or <literal>before</literal>
  772. attributes if you want your filter to be inserted before or after another filter in
  773. the stack. The names "FIRST" and "LAST" can be used with the
  774. <literal>position</literal> attribute to indicate that you want your filter to
  775. appear before or after the entire stack, respectively. </para>
  776. <tip>
  777. <title>Avoiding filter position conflicts</title>
  778. <para> If you are inserting a custom filter which may occupy the same position as
  779. one of the standard filters created by the namespace then it's important that
  780. you don't include the namespace versions by mistake. Avoid using the
  781. <literal>auto-config</literal> attribute and remove any elements which create
  782. filters whose functionality you want to replace. </para>
  783. <para> Note that you can't replace filters which are created by the use of the
  784. <literal>&lt;http&gt;</literal> element itself -
  785. <classname>SecurityContextPersistenceFilter</classname>,
  786. <classname>ExceptionTranslationFilter</classname> or
  787. <classname>FilterSecurityInterceptor</classname>. Some other filters are added
  788. by default, but you can disable them. An <classname>AnonymousAuthenticationFilter</classname>
  789. is added by default and unless you have
  790. <link xlink:href="#ns-session-fixation">session-fixation protection</link>
  791. disabled, a <classname>SessionManagementFilter</classname> will also be added
  792. to the filter chain.
  793. </para>
  794. </tip>
  795. <para> If you're replacing a namespace filter which requires an authentication entry
  796. point (i.e. where the authentication process is triggered by an attempt by an
  797. unauthenticated user to access to a secured resource), you will need to add a custom
  798. entry point bean too. </para>
  799. <section xml:id="ns-entry-point-ref">
  800. <title>Setting a Custom
  801. <interfacename>AuthenticationEntryPoint</interfacename></title>
  802. <para> If you aren't using form login, OpenID or basic authentication through the
  803. namespace, you may want to define an authentication filter and entry point using
  804. a traditional bean syntax and link them into the namespace, as we've just seen.
  805. The corresponding <interfacename>AuthenticationEntryPoint</interfacename> can be
  806. set using the <literal>entry-point-ref</literal> attribute on the
  807. <literal>&lt;http&gt;</literal> element. </para>
  808. <para> The CAS sample application is a good example of the use of custom beans with
  809. the namespace, including this syntax. If you aren't familiar with authentication
  810. entry points, they are discussed in the <link
  811. xlink:href="#tech-intro-auth-entry-point">technical overview</link> chapter.
  812. </para>
  813. </section>
  814. </section>
  815. </section>
  816. <section xml:id="ns-method-security">
  817. <title>Method Security</title>
  818. <para>From version 2.0 onwards Spring Security has improved support substantially for adding
  819. security to your service layer methods. It provides support for JSR-250 annotation
  820. security as well as the framework's original <literal>@Secured</literal> annotation.
  821. From 3.0 you can also make use of new <link xlink:href="#el-access">expression-based
  822. annotations</link>. You can apply security to a single bean, using the
  823. <literal>intercept-methods</literal> element to decorate the bean declaration, or you
  824. can secure multiple beans across the entire service layer using the AspectJ style
  825. pointcuts. </para>
  826. <section xml:id="ns-global-method">
  827. <title>The <literal>&lt;global-method-security&gt;</literal> Element</title>
  828. <para> This element is used to enable annotation-based security in your application (by
  829. setting the appropriate attributes on the element), and also to group together
  830. security pointcut declarations which will be applied across your entire application
  831. context. You should only declare one
  832. <literal>&lt;global-method-security&gt;</literal> element. The following declaration
  833. would enable support for Spring Security's <literal>@Secured</literal>: <programlisting><![CDATA[
  834. <global-method-security secured-annotations="enabled" />
  835. ]]>
  836. </programlisting> Adding an annotation to a method (on an class or interface) would then limit
  837. the access to that method accordingly. Spring Security's native annotation support
  838. defines a set of attributes for the method. These will be passed to the
  839. <interfacename>AccessDecisionManager</interfacename> for it to make the actual
  840. decision:
  841. <programlisting language="java">
  842. public interface BankService {
  843. @Secured("IS_AUTHENTICATED_ANONYMOUSLY")
  844. public Account readAccount(Long id);
  845. @Secured("IS_AUTHENTICATED_ANONYMOUSLY")
  846. public Account[] findAccounts();
  847. @Secured("ROLE_TELLER")
  848. public Account post(Account account, double amount);
  849. }
  850. </programlisting>Support
  851. for JSR-250 annotations can be enabled using <programlisting><![CDATA[
  852. <global-method-security jsr250-annotations="enabled" />
  853. ]]>
  854. </programlisting>These are standards-based and allow simple role-based constraints to
  855. be applied but do not have the power Spring Security's native annotations. To use
  856. the new expression-based syntax, you would use <programlisting><![CDATA[
  857. <global-method-security pre-post-annotations="enabled" />
  858. ]]></programlisting>and the equivalent Java code would
  859. be<programlisting language="java">
  860. public interface BankService {
  861. @PreAuthorize("isAnonymous()")
  862. public Account readAccount(Long id);
  863. @PreAuthorize("isAnonymous()")
  864. public Account[] findAccounts();
  865. @PreAuthorize("hasAuthority('ROLE_TELLER')")
  866. public Account post(Account account, double amount);
  867. }
  868. </programlisting>Expression-based
  869. annotations are a good choice if you need to define simple rules that go beyond
  870. checking the role names against the user's list of authorities. You can enable more
  871. than one type of annotation in the same application, but you should avoid mixing
  872. annotations types in the same interface or class to avoid confusion. <note>
  873. <para>The annotated methods will only be secured for instances which are defined as
  874. Spring beans (in the same application context in which method-security is
  875. enabled). If you want to secure instances which are not created by Spring (using
  876. the <literal>new</literal> operator, for example) then you need to use AspectJ.
  877. </para>
  878. </note> </para>
  879. <section xml:id="ns-protect-pointcut">
  880. <title>Adding Security Pointcuts using <literal>protect-pointcut</literal></title>
  881. <para> The use of <literal>protect-pointcut</literal> is particularly powerful, as
  882. it allows you to apply security to many beans with only a simple declaration.
  883. Consider the following example: <programlisting language="xml"><![CDATA[
  884. <global-method-security>
  885. <protect-pointcut expression="execution(* com.mycompany.*Service.*(..))"
  886. access="ROLE_USER"/>
  887. </global-method-security>
  888. ]]>
  889. </programlisting> This will protect all methods on beans declared in the application
  890. context whose classes are in the <literal>com.mycompany</literal> package and
  891. whose class names end in "Service". Only users with the
  892. <literal>ROLE_USER</literal> role will be able to invoke these methods. As with
  893. URL matching, the most specific matches must come first in the list of
  894. pointcuts, as the first matching expression will be used. </para>
  895. </section>
  896. </section>
  897. </section>
  898. <section xml:id="ns-access-manager">
  899. <title>The Default AccessDecisionManager</title>
  900. <para> This section assumes you have some knowledge of the underlying architecture for
  901. access-control within Spring Security. If you don't you can skip it and come back to it
  902. later, as this section is only really relevant for people who need to do some
  903. customization in order to use more than simple role-based security. </para>
  904. <para> When you use a namespace configuration, a default instance of
  905. <interfacename>AccessDecisionManager</interfacename> is automatically registered for you
  906. and will be used for making access decisions for method invocations and web URL access,
  907. based on the access attributes you specify in your <literal>intercept-url</literal> and
  908. <literal>protect-pointcut</literal> declarations (and in annotations if you are using
  909. annotation secured methods). </para>
  910. <para> The default strategy is to use an <classname>AffirmativeBased</classname>
  911. <interfacename>AccessDecisionManager</interfacename> with a
  912. <classname>RoleVoter</classname> and an <classname>AuthenticatedVoter</classname>. You
  913. can find out more about these in the chapter on <link xlink:href="#authz-arch"
  914. >authorization</link>.</para>
  915. <section xml:id="ns-custom-access-mgr">
  916. <title>Customizing the AccessDecisionManager</title>
  917. <para> If you need to use a more complicated access control strategy then it is easy to
  918. set an alternative for both method and web security. </para>
  919. <para> For method security, you do this by setting the
  920. <literal>access-decision-manager-ref</literal> attribute on
  921. <literal>global-method-security</literal> to the <literal>id</literal> of the appropriate
  922. <interfacename>AccessDecisionManager</interfacename> bean in the application
  923. context: <programlisting language="xml"><![CDATA[
  924. <global-method-security access-decision-manager-ref="myAccessDecisionManagerBean">
  925. ...
  926. </global-method-security>
  927. ]]></programlisting></para>
  928. <para> The syntax for web security is the same, but on the <literal>http</literal>
  929. element: <programlisting language="xml"><![CDATA[
  930. <http access-decision-manager-ref="myAccessDecisionManagerBean">
  931. ...
  932. </http>
  933. ]]></programlisting></para>
  934. </section>
  935. </section>
  936. <section xml:id="ns-auth-manager">
  937. <title>The Authentication Manager and the Namespace</title>
  938. <para> The main interface which provides authentication services in Spring Security is the
  939. <interfacename>AuthenticationManager</interfacename>. This is usually an instance of
  940. Spring Security's <classname>ProviderManager</classname> class, which you may already be
  941. familiar with if you've used the framework before. If not, it will be covered later, in
  942. the <link xlink:href="#tech-intro-authentication">technical overview chapter</link>. The
  943. bean instance is registered using the <literal>authentication-manager</literal>
  944. namespace element. You can't use a custom <classname>AuthenticationManager</classname>
  945. if you are using either HTTP or method security through the namespace, but this should
  946. not be a problem as you have full control over the
  947. <classname>AuthenticationProvider</classname>s that are used.</para>
  948. <para> You may want to register additional <classname>AuthenticationProvider</classname>
  949. beans with the <classname>ProviderManager</classname> and you can do this using the
  950. <literal>&lt;authentication-provider&gt;</literal> element with the
  951. <literal>ref</literal> attribute, where the value of the attribute is the name of the
  952. provider bean you want to add. For example: <programlisting language="xml"><![CDATA[
  953. <authentication-manager>
  954. <authentication-provider ref="casAuthenticationProvider"/>
  955. </authentication-manager>
  956. <bean id="casAuthenticationProvider"
  957. class="org.springframework.security.cas.authentication.CasAuthenticationProvider">
  958. ...
  959. </bean>
  960. ]]></programlisting></para>
  961. <para> Another common requirement is that another bean in the context may require a
  962. reference to the <interfacename>AuthenticationManager</interfacename>. You can easily
  963. register an alias for the <interfacename>AuthenticationManager</interfacename> and use
  964. this name elsewhere in your application context. <programlisting language="xml"><![CDATA[
  965. <security:authentication-manager alias="authenticationManager">
  966. ...
  967. </security:authentication-manager>
  968. <bean id="customizedFormLoginFilter"
  969. class="com.somecompany.security.web.CustomFormLoginFilter">
  970. <property name="authenticationManager" ref="authenticationManager"/>
  971. ...
  972. </bean>
  973. ]]></programlisting></para>
  974. </section>
  975. </chapter>