namespace-config.xml 65 KB

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