faq.xml 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?oxygen RNGSchema="http://www.oasis-open.org/docbook/xml/5.0/rng/docbook.rng" type="xml"?>
  3. <article class="faq" xml:id="spring-security-faq" xmlns="http://docbook.org/ns/docbook"
  4. xmlns:xlink="http://www.w3.org/1999/xlink" version="5.0">
  5. <info>
  6. <title>Frequently Answered Questions (FAQ)</title>
  7. </info>
  8. <section>
  9. <title>General Questions</title>
  10. <qandaset>
  11. <qandadiv>
  12. <qandaentry xml:id="faq-other-concerns">
  13. <question>
  14. <para>Will Spring Security take care of all my application security
  15. requirements?</para>
  16. </question>
  17. <answer>
  18. <para> Spring Security provides you with a very flexible framework for your
  19. authentication and authorization requirements, but there are many other
  20. considerations for building a secure application that are outside its
  21. scope. Web applications are vulnerable to all kinds of attacks which you
  22. should be familiar with, preferably before you start development so you
  23. can design and code with them in mind from the beginning. Check out the
  24. <link xlink:href="http://www.owasp.org/">OWASP web site</link> for
  25. information on the major issues facing web application developers and
  26. the countermeasures you can use against them.</para>
  27. </answer>
  28. </qandaentry>
  29. <qandaentry xml:id="faq-web-xml">
  30. <question>
  31. <para>Why not just use web.xml security?</para>
  32. </question>
  33. <answer>
  34. <para>Let's assume you're developing an enterprise application based on
  35. Spring. There are four security concerns you typically need to address:
  36. authentication, web request security, service layer security (i.e. your
  37. methods that implement business logic), and domain object instance
  38. security (i.e. different domain objects have different permissions).
  39. With these typical requirements in mind: <orderedlist>
  40. <listitem>
  41. <para><emphasis>Authentication</emphasis>: The servlet specification
  42. provides an approach to authentication. However, you will need
  43. to configure the container to perform authentication which
  44. typically requires editing of container-specific "realm"
  45. settings. This makes a non-portable configuration, and if you
  46. need to write an actual Java class to implement the container's
  47. authentication interface, it becomes even more non-portable.
  48. With Spring Security you achieve complete portability - right
  49. down to the WAR level. Also, Spring Security offers a choice of
  50. production-proven authentication providers and mechanisms,
  51. meaning you can switch your authentication approaches at
  52. deployment time. This is particularly valuable for software
  53. vendors writing products that need to work in an unknown target
  54. environment.</para>
  55. </listitem>
  56. <listitem>
  57. <para><emphasis>Web request security:</emphasis> The servlet
  58. specification provides an approach to secure your request URIs.
  59. However, these URIs can only be expressed in the servlet
  60. specification's own limited URI path format. Spring Security
  61. provides a far more comprehensive approach. For instance, you
  62. can use Ant paths or regular expressions, you can consider parts
  63. of the URI other than simply the requested page (e.g. you can
  64. consider HTTP GET parameters) and you can implement your own
  65. runtime source of configuration data. This means your web
  66. request security can be dynamically changed during the actual
  67. execution of your webapp.</para>
  68. </listitem>
  69. <listitem>
  70. <para><emphasis>Service layer and domain object security:</emphasis>
  71. The absence of support in the servlet specification for services
  72. layer security or domain object instance security represent
  73. serious limitations for multi-tiered applications. Typically
  74. developers either ignore these requirements, or implement
  75. security logic within their MVC controller code (or even worse,
  76. inside the views). There are serious disadvantages with this
  77. approach: <orderedlist>
  78. <listitem>
  79. <para><emphasis>Separation of concerns:</emphasis>
  80. Authorization is a crosscutting concern and should be
  81. implemented as such. MVC controllers or views
  82. implementing authorization code makes it more difficult
  83. to test both the controller and authorization logic,
  84. more difficult to debug, and will often lead to code
  85. duplication.</para>
  86. </listitem>
  87. <listitem>
  88. <para><emphasis>Support for rich clients and web
  89. services:</emphasis> If an additional client type must
  90. ultimately be supported, any authorization code embedded
  91. within the web layer is non-reusable. It should be
  92. considered that Spring remoting exporters only export
  93. service layer beans (not MVC controllers). As such
  94. authorization logic needs to be located in the services
  95. layer to support a multitude of client types.</para>
  96. </listitem>
  97. <listitem>
  98. <para><emphasis>Layering issues:</emphasis> An MVC
  99. controller or view is simply the incorrect architectural
  100. layer to implement authorization decisions concerning
  101. services layer methods or domain object instances.
  102. Whilst the Principal may be passed to the services layer
  103. to enable it to make the authorization decision, doing
  104. so would introduce an additional argument on every
  105. services layer method. A more elegant approach is to use
  106. a ThreadLocal to hold the Principal, although this would
  107. likely increase development time to a point where it
  108. would become more economical (on a cost-benefit basis)
  109. to simply use a dedicated security framework.</para>
  110. </listitem>
  111. <listitem>
  112. <para><emphasis>Authorisation code quality:</emphasis> It is
  113. often said of web frameworks that they "make it easier
  114. to do the right things, and harder to do the wrong
  115. things". Security frameworks are the same, because they
  116. are designed in an abstract manner for a wide range of
  117. purposes. Writing your own authorization code from
  118. scratch does not provide the "design check" a framework
  119. would offer, and in-house authorization code will
  120. typically lack the improvements that emerge from
  121. widespread deployment, peer review and new versions.
  122. </para>
  123. </listitem>
  124. </orderedlist></para>
  125. </listitem>
  126. </orderedlist></para>
  127. <para> For simple applications, servlet specification security may just be
  128. enough. Although when considered within the context of web container
  129. portability, configuration requirements, limited web request security
  130. flexibility, and non-existent services layer and domain object instance
  131. security, it becomes clear why developers often look to alternative
  132. solutions. </para>
  133. </answer>
  134. </qandaentry>
  135. <qandaentry xml:id="faq-requirements">
  136. <question>
  137. <para>What Java and Spring Framework versions are required?</para>
  138. </question>
  139. <answer>
  140. <para> Spring Security 3.0 and 3.1 require at least JDK 1.5 and also
  141. require Spring 3.0.3 as a minimum. Ideally you should be using the latest
  142. release versions to avoid problems.
  143. </para>
  144. <para> Spring Security 2.0.x requires a minimum JDK version of 1.4 and is
  145. built against Spring 2.0.x. It should also be compatible with
  146. applications using Spring 2.5.x. </para>
  147. </answer>
  148. </qandaentry>
  149. <qandaentry xml:id="faq-start-simple">
  150. <question>
  151. <para> I'm new to Spring Security and I need to build an application that
  152. supports CAS single sign-on over HTTPS, while allowing Basic
  153. authentication locally for certain URLs, authenticating against multiple
  154. back end user information sources (LDAP and JDBC). I've copied some
  155. configuration files I found but it doesn't work. What could be wrong? </para>
  156. <para>Or subsititute an alternative complex scenario...</para>
  157. </question>
  158. <answer>
  159. <para> Realistically, you need an understanding of the technolgies you are
  160. intending to use before you can successfully build applications with
  161. them. Security is complicated. Setting up a simple configuration using a
  162. login form and some hard-coded users using Spring Security's namespace
  163. is reasonably straightforward. Moving to using a backed JDBC database is
  164. also easy enough. But if you try and jump straight to a complicated
  165. deployment scenario like this you will almost certainly be frustrated.
  166. There is a big jump in the learning curve required to set up systems
  167. like CAS, configure LDAP servers and install SSL certificates properly.
  168. So you need to take things one step at a time. </para>
  169. <para> From a Spring Security perspective, the first thing you should do is
  170. follow the <quote>Getting Started</quote> guide on the web site. This
  171. will take you through a series of steps to get up and running and get
  172. some idea of how the framework operates. If you are using other
  173. technologies which you aren't familiar with then you should do some
  174. research and try to make sure you can use them in isolation before
  175. combining them in a complex system. </para>
  176. </answer>
  177. </qandaentry>
  178. </qandadiv>
  179. </qandaset>
  180. </section>
  181. <section>
  182. <title>Common Problems</title>
  183. <qandaset>
  184. <qandadiv>
  185. <title>Authentication</title>
  186. <qandaentry xml:id="faq-bad-credentials">
  187. <question>
  188. <para>When I try to log in, I get an error message that says "Bad
  189. Credentials". What's wrong?</para>
  190. </question>
  191. <answer>
  192. <para>This means that authentication has failed. It doesn't say why, as it
  193. is good practice to avoid giving details which might help an attacker
  194. guess account names or passwords.</para>
  195. <para>This also means that if you ask this question in the forum, you will
  196. not get an answer unless you provide additional information. As with any
  197. issue you should check the output from the debug log, note any exception
  198. stacktraces and related messages. Step through the code in a debugger to
  199. see where the authentication fails and why. Write a test case which
  200. exercises your authentication configuration outside of the application.
  201. More often than not, the failure is due to a difference in the password
  202. data stored in a database and that entered by the user. If you are using
  203. hashed passwords, make sure the value stored in your database is
  204. <emphasis>exactly</emphasis> the same as the value produced by the
  205. <interfacename>PasswordEncoder</interfacename> configured in your
  206. application.</para>
  207. </answer>
  208. </qandaentry>
  209. <qandaentry xml:id="faq-login-loop">
  210. <question>
  211. <para>My application goes into an "endless loop" when I try to login, what's
  212. going on?</para>
  213. </question>
  214. <answer>
  215. <para>A common user problem with infinite loop and redirecting to the login
  216. page is caused by accidently configuring the login page as a "secured"
  217. resource. Make sure your configuration allows anonymous access to the
  218. login page, either by excluding it from the security filter chain or
  219. marking it as requiring ROLE_ANONYMOUS.</para>
  220. <para>If your AccessDecisionManager includes an AuthenticatedVoter, you can
  221. use the attribute "IS_AUTHENTICATED_ANONYMOUSLY". This is automatically
  222. available if you are using the standard namespace configuration setup. </para>
  223. <para> From Spring Security 2.0.1 onwards, when you are using
  224. namespace-based configuration, a check will be made on loading the
  225. application context and a warning message logged if your login page
  226. appears to be protected. </para>
  227. </answer>
  228. </qandaentry>
  229. <qandaentry xml:id="faq-anon-access-denied">
  230. <question>
  231. <para>I get an exception with the message "Access is denied (user is
  232. anonymous);". What's wrong?</para>
  233. </question>
  234. <answer>
  235. <para> This is a debug level message which occurs the first time an
  236. anonymous user attempts to access a protected resource.
  237. <programlisting>
  238. DEBUG [ExceptionTranslationFilter] - Access is denied (user is anonymous); redirecting to authentication entry point
  239. org.springframework.security.AccessDeniedException: Access is denied
  240. at org.springframework.security.vote.AffirmativeBased.decide(AffirmativeBased.java:68)
  241. at org.springframework.security.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:262)
  242. </programlisting>
  243. It is normal and shouldn't be anything to worry about. </para>
  244. </answer>
  245. </qandaentry>
  246. <qandaentry xml:id="faq-cached-secure-page">
  247. <question>
  248. <para>Why can I still see a secured page even after I've logged out of my
  249. application?</para>
  250. </question>
  251. <answer>
  252. <para>The most common reason for this is that your browser has cached the
  253. page and you are seeing a copy which is being retrieved from the
  254. browsers cache. Verify this by checking whether the browser is actually
  255. sending the request (check your server access logs, the debug log or use
  256. a suitable browser debugging plugin such as <quote>Tamper Data</quote>
  257. for Firefox). This has nothing to do with Spring Security and you should
  258. configure your application or server to set the appropriate
  259. <literal>Cache-Control</literal> response headers. Note that SSL
  260. requests are never cached.</para>
  261. </answer>
  262. </qandaentry>
  263. <qandaentry xml:id="auth-exception-credentials-not-found">
  264. <question>
  265. <para>I get an exception with the message "An Authentication object was not
  266. found in the SecurityContext". What's wrong?</para>
  267. </question>
  268. <answer>
  269. <para> This is a another debug level message which occurs the first time an
  270. anonymous user attempts to access a protected resource, but when you do
  271. not have an <classname>AnonymousAuthenticationFilter</classname> in your
  272. filter chain configuration.
  273. <programlisting>
  274. DEBUG [ExceptionTranslationFilter] - Authentication exception occurred; redirecting to authentication entry point
  275. org.springframework.security.AuthenticationCredentialsNotFoundException:
  276. An Authentication object was not found in the SecurityContext
  277. at org.springframework.security.intercept.AbstractSecurityInterceptor.credentialsNotFound(AbstractSecurityInterceptor.java:342)
  278. at org.springframework.security.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:254)
  279. </programlisting>
  280. It is normal and shouldn't be anything to worry about. </para>
  281. </answer>
  282. </qandaentry>
  283. <qandaentry xml:id="faq-ldap-authentication">
  284. <question><para>I can't get LDAP authentication to work. What's wrong with my configuration?</para></question>
  285. <answer>
  286. <para>
  287. Note that the permissions for an LDAP directory often do not allow you to read the password
  288. for a user. Hence it is often not possible to use the <link linkend="faq-what-is-userdetailservice"><interfacename>UserDetailsService</interfacename>
  289. approach</link> where Spring Security compares the stored password with the one submitted by the user.
  290. The most common approach is to use LDAP <quote>bind</quote>, which is one of the operations
  291. supported by <link xlink:href="http://en.wikipedia.org/wiki/Lightweight_Directory_Access_Protocol">the LDAP protocol</link>.
  292. With this approach, Spring Security validates the password by attempting to authenticate to the directory
  293. as the user.
  294. </para>
  295. <para>The most common problem with LDAP authentication is a lack of knowledge of the
  296. directory server tree structure and configuration. This will be different in different
  297. companies, so you have to find it out yourself. Before adding a Spring Security LDAP
  298. configuration to an application, it's a good idea to write a simple test using standard
  299. Java LDAP code (without Spring Security involved), and make sure you can
  300. get that to work first. For example, to authenticate a user, you could use
  301. the following code:
  302. <programlisting language="java"><![CDATA[
  303. @Test
  304. public void ldapAuthenticationIsSuccessful() throws Exception {
  305. Hashtable<String,String> env = new Hashtable<String,String>();
  306. env.put(Context.SECURITY_AUTHENTICATION, "simple");
  307. env.put(Context.SECURITY_PRINCIPAL, "cn=joe,ou=users,dc=mycompany,dc=com");
  308. env.put(Context.PROVIDER_URL, "ldap://mycompany.com:389/dc=mycompany,dc=com");
  309. env.put(Context.SECURITY_CREDENTIALS, "joespassword");
  310. env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
  311. InitialLdapContext ctx = new InitialLdapContext(env, null);
  312. }
  313. ]]></programlisting>
  314. </para>
  315. </answer>
  316. </qandaentry>
  317. </qandadiv>
  318. <qandadiv xml:id="faq-session-management">
  319. <title>Session Management</title>
  320. <para> Session management issues are a common source of forum questions. If you are
  321. developing Java web applications, you should understand how the session is
  322. maintained between the servlet container and the user's browser. You should also
  323. understand the difference between secure and non-secure cookies and the
  324. implications of using HTTP/HTTPS and switching between the two. Spring Security
  325. has nothing to do with maintaining the session or providing session identifiers.
  326. This is entirely handled by the servlet container. </para>
  327. <qandaentry xml:id="faq-concurrent-session-same-browser">
  328. <question>
  329. <para>I'm using Spring Security's concurrent session control to prevent
  330. users from logging in more than once at a time. When I open another
  331. browser window after logging in, it doesn't stop me from logging in
  332. again. Why can I log in more than once? </para>
  333. </question>
  334. <answer>
  335. <para>Browsers generally maintain a single session per browser instance. You
  336. cannot have two separate sessions at once. So if you log in again in
  337. another window or tab you are just reauthenticating in the same session.
  338. The server doesn't know anything about tabs, windows or browser
  339. instances. All it sees are HTTP requests and it ties those to a
  340. particular session according to the value of the the JSESSIONID cookie
  341. that they contain. When a user authenticates during a session, Spring
  342. Security's concurrent session control checks the number of
  343. <emphasis>other authenticated sessions</emphasis> that they have. If
  344. they are already authenticated with the same session, then
  345. re-authenticating will have no effect. </para>
  346. </answer>
  347. </qandaentry>
  348. <qandaentry xml:id="faq-new-session-on-authentication">
  349. <question>
  350. <para>Why does the session Id change when I authenticate through Spring
  351. Security?</para>
  352. </question>
  353. <answer>
  354. <para>With the default configuration, Spring Security changes the session ID
  355. when the user authenticates. If you're using a Servlet 3.1 or newer
  356. container, the session ID is simply changed. If you're using an older
  357. container, Spring Security invalidates the existing session, creates a
  358. new session, and transfers the session data to the new session. Changing
  359. the session identifier in this manner prevents
  360. <quote>session-fixation</quote> attacks. You can find more about this
  361. online and in the reference manual. </para>
  362. </answer>
  363. </qandaentry>
  364. <qandaentry xml:id="faq-tomcat-https-session">
  365. <question>
  366. <para> I'm using Tomcat (or some other servlet container) and have enabled
  367. HTTPS for my login page, switching back to HTTP afterwards. It doesn't
  368. work - I just end up back at the login page after authenticating.
  369. </para>
  370. </question>
  371. <answer>
  372. <para> This happens because sessions created under HTTPS, for which the
  373. session cookie is marked as <quote>secure</quote>, cannot subsequently
  374. be used under HTTP. The browser will not send the cookie back to the
  375. server and any session state will be lost (including the security
  376. context information). Starting a session in HTTP first should work as
  377. the session cookie won't be marked as secure. However, Spring
  378. Security's <link
  379. xlink:href="http://static.springsource.org/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#ns-session-fixation"
  380. >Session Fixation Protection</link> can interfere with this because
  381. it results in a new session ID cookie being sent back to the user's
  382. browser, usually with the secure flag. To get around this, you can
  383. disable session fixation protection, but in newer Servlet containers
  384. you can also configure session cookies to never use the secure flag.
  385. Note that switching between HTTP and HTTPS
  386. is not a good idea in general, as any application which uses HTTP at all
  387. is vulnerable to man-in-the-middle attacks. To be truly secure, the user
  388. should begin accessing your site in HTTPS and continue using it until
  389. they log out. Even clicking on an HTTPS link from a page accessed over
  390. HTTP is potentially risky. If you need more convincing, check out a tool
  391. like <link xlink:href="http://www.thoughtcrime.org/software/sslstrip/"
  392. >sslstrip</link>. </para>
  393. </answer>
  394. </qandaentry>
  395. <qandaentry>
  396. <question>
  397. <para>I'm not switching between HTTP and HTTPS but my session is still
  398. getting lost</para>
  399. </question>
  400. <answer>
  401. <para>Sessions are maintained either by exchanging a session cookie or by
  402. adding the a <literal>jsessionid</literal> parameter to URLs (this
  403. happens automatically if you are using JSTL to output URLs, or if you
  404. call <literal>HttpServletResponse.encodeUrl</literal> on URLs (before a
  405. redirect, for example). If clients have cookies disabled, and you are
  406. not rewriting URLs to include the <literal>jsessionid</literal>, then
  407. the session will be lost. Note that the use of cookies is preferred for
  408. security reasons, as it does not expose the session information in the
  409. URL. </para>
  410. </answer>
  411. </qandaentry>
  412. <qandaentry xml:id="faq-session-listener-missing">
  413. <question>
  414. <para>I'm trying to use the concurrent session-control support but it won't
  415. let me log back in, even if I'm sure I've logged out and haven't
  416. exceeded the allowed sessions. </para>
  417. </question>
  418. <answer>
  419. <para>Make sure you have added the listener to your web.xml file. It is
  420. essential to make sure that the Spring Security session registry is
  421. notified when a session is destroyed. Without it, the session
  422. information will not be removed from the registry.</para>
  423. <programlisting language="xml"><![CDATA[
  424. <listener>
  425. <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
  426. </listener> ]]>
  427. </programlisting>
  428. </answer>
  429. </qandaentry>
  430. <qandaentry xml:id="faq-unwanted-session-creation">
  431. <question>
  432. <para>Spring Security is creating a session somewhere, even though I've
  433. configured it not to, by setting the <literal>create-session</literal>
  434. attribute to <literal>never</literal>. </para>
  435. </question>
  436. <answer>
  437. <para>This usually means that the user's application is creating a session
  438. somewhere, but that they aren't aware of it. The most common culprit is
  439. a JSP. Many people aren't aware that JSPs create sessions by default. To
  440. prevent a JSP from creating a session, add the directive <literal>&lt;%@
  441. page session="false" %&gt;</literal> to the top of the page. </para>
  442. <para> If you are having trouble working out where a session is being
  443. created, you can add some debugging code to track down the location(s).
  444. One way to do this would be to add a
  445. <literal>javax.servlet.http.HttpSessionListener</literal> to your
  446. application, which calls <literal>Thread.dumpStack()</literal> in the
  447. <literal>sessionCreated</literal> method. </para>
  448. </answer>
  449. </qandaentry>
  450. </qandadiv>
  451. <qandadiv>
  452. <title>Miscellaneous</title>
  453. <qandaentry xml:id="faq-no-security-on-forward">
  454. <question>
  455. <para> I'm forwarding a request to another URL using the RequestDispatcher,
  456. but my security constraints aren't being applied. </para>
  457. </question>
  458. <answer>
  459. <para> Filters are not applied by default to forwards or includes. If you
  460. really want the security filters to be applied to forwards and/or
  461. includes, then you have to configure these explicitly in your web.xml
  462. using the &lt;dispatcher&gt; element, a child element of
  463. &lt;filter-mapping&gt;. </para>
  464. </answer>
  465. </qandaentry>
  466. <qandaentry xml:id="faq-method-security-in-web-context">
  467. <question>
  468. <para>I have added Spring Security's &lt;global-method-security&gt; element
  469. to my application context but if I add security annotations to my Spring
  470. MVC controller beans (Struts actions etc.) then they don't seem to have
  471. an effect.</para>
  472. </question>
  473. <answer>
  474. <para> In a Spring web application, the application context which holds the
  475. Spring MVC beans for the dispatcher servlet is often separate from the
  476. main application context. It is often defined in a file called
  477. <literal>myapp-servlet.xml</literal>, where <quote>myapp</quote> is the
  478. name assigned to the Spring <classname>DispatcherServlet</classname> in
  479. <filename>web.xml</filename>. An application can have multiple
  480. <classname>DispatcherServlet</classname>s, each with its own isolated
  481. application context. The beans in these <quote>child</quote> contexts
  482. are not visible to the rest of the application. The
  483. <quote>parent</quote> application context is loaded by the
  484. <classname>ContextLoaderListener</classname> you define in your
  485. <filename>web.xml</filename> and is visible to all the child contexts.
  486. This parent context is usually where you define your security
  487. configuration, including the
  488. <literal>&lt;global-method-security&gt;</literal> element). As a result
  489. any security constraints applied to methods in these web beans will not
  490. be enforced, since the beans cannot be seen from the
  491. <classname>DispatcherServlet</classname> context. You need to either
  492. move the <literal>&lt;global-method-security&gt;</literal> declaration
  493. to the web context or moved the beans you want secured into the main
  494. application context. </para>
  495. <para>Generally we would recommend applying method security at the service
  496. layer rather than on individual web controllers.</para>
  497. </answer>
  498. </qandaentry>
  499. <qandaentry xml:id="faq-no-filters-no-context">
  500. <question>
  501. <para>I have a user who has definitely been authenticated, but when I try to
  502. access the <classname>SecurityContextHolder</classname> during some
  503. requests, the <interfacename>Authentication</interfacename> is null. Why
  504. can't I see the user information? </para>
  505. </question>
  506. <answer>
  507. <para>If you have excluded the request from the security filter chain using
  508. the attribute <literal>filters='none'</literal> in the
  509. <literal>&lt;intercept-url></literal> element that matches the URL
  510. pattern, then the <classname>SecurityContextHolder</classname> will not
  511. be populated for that request. Check the debug log to see whether the
  512. request is passing through the filter chain. (You are reading the debug
  513. log, right?).</para>
  514. </answer>
  515. </qandaentry>
  516. </qandadiv>
  517. </qandaset>
  518. </section>
  519. <section>
  520. <title>Spring Security Architecture Questions</title>
  521. <qandaset>
  522. <qandadiv>
  523. <qandaentry xml:id="faq-where-is-class-x">
  524. <question>
  525. <para>How do I know which package class X is in?</para>
  526. </question>
  527. <answer>
  528. <para>The best way of locating classes is by installing the Spring Security
  529. source in your IDE. The distribution includes source jars for each of
  530. the modules the project is divided up into. Add these to your project
  531. source path and you can navigate directly to Spring Security classes
  532. (<command>Ctrl-Shift-T</command> in Eclipse). This also makes debugging
  533. easier and allows you to troubleshoot exceptions by looking directly at
  534. the code where they occur to see what's going on there. </para>
  535. </answer>
  536. </qandaentry>
  537. <qandaentry xml:id="faq-namespace-to-bean-mapping">
  538. <question>
  539. <para>How do the namespace elements map to conventional bean
  540. configurations?</para>
  541. </question>
  542. <answer>
  543. <para>There is a general overview of what beans are created by the namespace
  544. in the namespace appendix of the reference guide. There is also a
  545. detailed blog article called <quote>Behind the Spring Security
  546. Namespace</quote> on <link
  547. xlink:href="http://blog.springsource.com/2010/03/06/behind-the-spring-security-namespace/"
  548. >blog.springsource.com</link>. If want to know the full details then the
  549. code is in the <filename>spring-security-config</filename> module within
  550. the Spring Security 3.0 distribution. You should probably read the
  551. chapters on namespace parsing in the standard Spring Framework reference
  552. documentation first.</para>
  553. </answer>
  554. </qandaentry>
  555. <qandaentry xml:id="faq-role-prefix">
  556. <question>
  557. <para>What does <quote>ROLE_</quote> mean and why do I need it on my role
  558. names?</para>
  559. </question>
  560. <answer>
  561. <para>Spring Security has a voter-based architecture which means that an
  562. access decision is made by a series of
  563. <interfacename>AccessDecisionVoter</interfacename>s. The voters act on
  564. the <quote>configuration attributes</quote> which are specified for a
  565. secured resource (such as a method invocation). With this approach, not
  566. all attributes may be relevant to all voters and a voter needs to know
  567. when it should ignore an attribute (abstain) and when it should vote to
  568. grant or deny access based on the attribute value. The most common voter
  569. is the <classname>RoleVoter</classname> which by default votes whenever
  570. it finds an attribute with the <quote>ROLE_</quote> prefix. It makes a
  571. simple comparison of the attribute (such as <quote>ROLE_USER</quote>)
  572. with the names of the authorities which the current user has been
  573. assigned. If it finds a match (they have an authority called
  574. <quote>ROLE_USER</quote>), it votes to grant access, otherwise it votes
  575. to deny access. </para>
  576. <para> The prefix can be changed by setting the
  577. <literal>rolePrefix</literal> property of
  578. <classname>RoleVoter</classname>. If you only need to use roles in your
  579. application and have no need for other custom voters, then you can set
  580. the prefix to a blank string, in which case the
  581. <classname>RoleVoter</classname> will treat all attributes as roles.
  582. </para>
  583. </answer>
  584. </qandaentry>
  585. <qandaentry xml:id="faq-what-dependencies">
  586. <question>
  587. <para>How do I know which dependencies to add to my application to work with
  588. Spring Security?</para>
  589. </question>
  590. <answer>
  591. <para>It will depend on what features you are using and what type of
  592. application you are developing. With Spring Security 3.0, the project
  593. jars are divided into clearly distinct areas of functionality, so it is
  594. straightforward to work out which Spring Security jars you need from
  595. your application requirements. All applications will need the
  596. <filename>spring-security-core</filename> jar. If you're developing a
  597. web application, you need the <filename>spring-security-web</filename>
  598. jar. If you're using security namespace configuration you need the
  599. <filename>spring-security-config</filename> jar, for LDAP support you
  600. need the <filename>spring-security-ldap</filename> jar and so on. </para>
  601. <para> For third-party jars the situation isn't always quite so obvious. A
  602. good starting point is to copy those from one of the pre-built sample
  603. applications WEB-INF/lib directories. For a basic application, you can
  604. start with the tutorial sample. If you want to use LDAP, with an
  605. embedded test server, then use the LDAP sample as a starting point. The
  606. reference manual also includes
  607. <link xlink:href="http://static.springsource.org/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#appendix-dependencies">an appendix</link> listing the first-level
  608. dependencies for each Spring Security module with some information on
  609. whether they are optional and what they are required for. </para>
  610. <para> If you are building your project with maven, then adding the
  611. appropriate Spring Security modules as dependencies to your pom.xml will
  612. automatically pull in the core jars that the framework requires. Any
  613. which are marked as "optional" in the Spring Security POM files will
  614. have to be added to your own pom.xml file if you need them. </para>
  615. </answer>
  616. </qandaentry>
  617. <qandaentry xml:id="faq-apacheds-deps">
  618. <question><para>What dependencies are needed to run an embedded ApacheDS LDAP server?</para></question>
  619. <answer><para>If you are using Maven, you need to add the folowing to your pom dependencies:<programlisting><![CDATA[
  620. <dependency>
  621. <groupId>org.apache.directory.server</groupId>
  622. <artifactId>apacheds-core</artifactId>
  623. <version>1.5.5</version>
  624. <scope>runtime</scope>
  625. </dependency>
  626. <dependency>
  627. <groupId>org.apache.directory.server</groupId>
  628. <artifactId>apacheds-server-jndi</artifactId>
  629. <version>1.5.5</version>
  630. <scope>runtime</scope>
  631. </dependency>
  632. ]]></programlisting>. The other required jars should be pulled in transitively.
  633. </para></answer>
  634. </qandaentry>
  635. <qandaentry xml:id="faq-what-is-userdetailservice">
  636. <question>
  637. <para>What is a <interfacename>UserDetailsService</interfacename> and do I need
  638. one?</para>
  639. </question>
  640. <answer>
  641. <para><interfacename>UserDetailsService</interfacename> is a DAO interface
  642. for loading data that is specific to a user account. It has no other
  643. function other to load that data for use by other components within the
  644. framework. It is not responsible for authenticating the user.
  645. Authenticating a user with a username/password combination is most
  646. commonly performed by the
  647. <classname>DaoAuthenticationProvider</classname>, which is injected with
  648. a <interfacename>UserDetailsService</interfacename> to allow it to load
  649. the password (and other data) for a user in order to compare it with the
  650. submitted value. Note that if you are using LDAP,
  651. <link linkend="faq-ldap-authentication">this approach may not work</link>.</para>
  652. <para> If you want to customize the authentication process then you should
  653. implement <interfacename>AuthenticationProvider</interfacename>
  654. yourself. See this <link
  655. xlink:href="http://blog.springsource.com/2010/08/02/spring-security-in-google-app-engine/"
  656. > blog article</link> for an example integrating Spring Security
  657. authentication with Google App Engine. </para>
  658. </answer>
  659. </qandaentry>
  660. </qandadiv>
  661. </qandaset>
  662. </section>
  663. <section>
  664. <title>Common <quote>Howto</quote> Requests</title>
  665. <qandaset>
  666. <qandadiv>
  667. <qandaentry xml:id="faq-extra-login-fields">
  668. <question>
  669. <para>I need to login in with more information than just the username. How
  670. do I add support for extra login fields (e.g. a company name)?</para>
  671. </question>
  672. <answer>
  673. <para>This question comes up repeatedly in the Spring Security forum so you
  674. will find more information there by searching the archives (or through
  675. google).</para>
  676. <para> The submitted login information is processed by an instance of
  677. <classname>UsernamePasswordAuthenticationFilter</classname>. You will
  678. need to customize this class to handle the extra data field(s). One
  679. option is to use your own customized authentication token class (rather
  680. than the standard
  681. <classname>UsernamePasswordAuthenticationToken</classname>), another is
  682. simply to concatenate the extra fields with the username (for example,
  683. using a ":" as the separator) and pass them in the username property of
  684. <classname>UsernamePasswordAuthenticationToken</classname>. </para>
  685. <para> You will also need to customize the actual authentication process. If
  686. you are using a custom authentication token class, for example, you will
  687. have to write an <classname>AuthenticationProvider</classname> to handle
  688. it (or extend the standard
  689. <classname>DaoAuthenticationProvider</classname>). If you have
  690. concatenated the fields, you can implement your own
  691. <interfacename>UserDetailsService</interfacename> which splits them up
  692. and loads the appropriate user data for authentication. </para>
  693. </answer>
  694. </qandaentry>
  695. <qandaentry xml:id="faq-matching-url-fragments">
  696. <question>
  697. <para>How do I apply different <literal>intercept-url</literal> constraints
  698. where only the fragment value of the requested URLs differs (e.g.
  699. <literal>/foo#bar</literal> and <literal>/foo#blah</literal>?</para>
  700. </question>
  701. <answer>
  702. <para>You can't do this, since the fragment is not transmitted from the
  703. browser to the server. The URLs above are identical from the server's
  704. perspective. This is a common question from GWT users.</para>
  705. </answer>
  706. </qandaentry>
  707. <qandaentry xml:id="faq-request-details-in-user-service">
  708. <question>
  709. <para>How do I access the user's IP Address (or other web-request data) in a
  710. <interfacename>UserDetailsService</interfacename>?</para>
  711. </question>
  712. <answer>
  713. <para> Obviously you can't (without resorting to something like thread-local
  714. variables) since the only information supplied to the interface is the
  715. username. Instead of implementing
  716. <interfacename>UserDetailsService</interfacename>, you should implement
  717. <interfacename>AuthenticationProvider</interfacename> directly and
  718. extract the information from the supplied
  719. <interfacename>Authentication</interfacename> token. </para>
  720. <para> In a standard web setup, the <methodname>getDetails()</methodname>
  721. method on the <interfacename>Authentication</interfacename> object will
  722. return an instance of <classname>WebAuthenticationDetails</classname>.
  723. If you need additional information, you can inject a custom
  724. <interfacename>AuthenticationDetailsSource</interfacename> into the
  725. authentication filter you are using. If you are using the namespace, for
  726. example with the <literal>&lt;form-login&gt;</literal> element, then you
  727. should remove this element and replace it with a
  728. <literal>&lt;custom-filter&gt;</literal> declaration pointing to an
  729. explicitly configured
  730. <classname>UsernamePasswordAuthenticationFilter</classname>. </para>
  731. </answer>
  732. </qandaentry>
  733. <qandaentry xml:id="faq-access-session-from-user-service">
  734. <question>
  735. <para>How do I access the <interfacename>HttpSession</interfacename> from a
  736. <interfacename>UserDetailsService</interfacename>?</para>
  737. </question>
  738. <answer>
  739. <para>You can't, since the <interfacename>UserDetailsService</interfacename>
  740. has no awareness of the servlet API. If you want to store custom user
  741. data, then you should customize the
  742. <interfacename>UserDetails</interfacename> object which is returned.
  743. This can then be accessed at any point, via the thread-local
  744. <classname>SecurityContextHolder</classname>. A call to
  745. <literal>SecurityContextHolder.getContext().getAuthentication().getPrincipal()</literal>
  746. will return this custom object. </para>
  747. <para> If you really need to access the session, then it must be done by
  748. customizing the web tier. </para>
  749. </answer>
  750. </qandaentry>
  751. <qandaentry xml:id="faq-password-in-user-service">
  752. <question>
  753. <para>How do I access the user's password in a
  754. <interfacename>UserDetailsService</interfacename>?</para>
  755. </question>
  756. <answer>
  757. <para>You can't (and shouldn't). You are probably misunderstanding its purpose.
  758. See <quote><link linkend="faq-what-is-userdetailservice">What is a UserDetailsService?</link></quote>
  759. above.
  760. </para>
  761. </answer>
  762. </qandaentry>
  763. <qandaentry xml:id="faq-dynamic-url-metadata">
  764. <question>
  765. <para>How do I define the secured URLs within an application
  766. dynamically?</para>
  767. </question>
  768. <answer>
  769. <para>People often ask about how to store the mapping between secured URLs
  770. and security metadata attributes in a database, rather than in the
  771. application context. </para>
  772. <para> The first thing you should ask yourself is if you really need to do
  773. this. If an application requires securing, then it also requires that
  774. the security be tested thoroughly based on a defined policy. It may
  775. require auditing and acceptance testing before being rolled out into a
  776. production environment. A security-conscious organization should be
  777. aware that the benefits of their diligent testing process could be wiped
  778. out instantly by allowing the security settings to be modified at
  779. runtime by changing a row or two in a configuration database. If you
  780. have taken this into account (perhaps using multiple layers of security
  781. within your application) then Spring Security allows you to fully
  782. customize the source of security metadata. You can make it fully dynamic
  783. if you choose. </para>
  784. <para> Both method and web security are protected by subclasses of
  785. <classname>AbstractSecurityInterceptor</classname> which is configured
  786. with a <interfacename>SecurityMetadataSource</interfacename> from which
  787. it obtains the metadata for a particular method or filter invocation.
  788. For web security, the interceptor class is
  789. <classname>FilterSecurityInterceptor</classname> and it uses the marker
  790. interface
  791. <interfacename>FilterInvocationSecurityMetadataSource</interfacename>.
  792. The <quote>secured object</quote> type it operates on is a
  793. <classname>FilterInvocation</classname>. The default implementation
  794. which is used (both in the namespace <literal>&lt;http&gt;</literal> and
  795. when configuring the interceptor explicitly, stores the list of URL
  796. patterns and their corresponding list of <quote>configuration
  797. attributes</quote> (instances of
  798. <interfacename>ConfigAttribute</interfacename>) in an in-memory map. </para>
  799. <para> To load the data from an alternative source, you must be using an
  800. explicitly declared security filter chain (typically Spring Security's
  801. <classname>FilterChainProxy</classname>) in order to customize the
  802. <classname>FilterSecurityInterceptor</classname> bean. You can't use the
  803. namespace. You would then implement
  804. <interfacename>FilterInvocationSecurityMetadataSource</interfacename> to
  805. load the data as you please for a particular
  806. <classname>FilterInvocation</classname><footnote>
  807. <para>The <classname>FilterInvocation</classname> object contains the
  808. <classname>HttpServletRequest</classname>, so you can obtain the URL
  809. or any other relevant information on which to base your decision on
  810. what the list of returned attributes will contain.</para>
  811. </footnote>. A very basic outline would look something like this: <programlisting language="java"><![CDATA[
  812. public class MyFilterSecurityMetadataSource implements FilterInvocationSecurityMetadataSource {
  813. public List<ConfigAttribute> getAttributes(Object object) {
  814. FilterInvocation fi = (FilterInvocation) object;
  815. String url = fi.getRequestUrl();
  816. String httpMethod = fi.getRequest().getMethod();
  817. List<ConfigAttribute> attributes = new ArrayList<ConfigAttribute>();
  818. // Lookup your database (or other source) using this information and populate the
  819. // list of attributes
  820. return attributes;
  821. }
  822. public Collection<ConfigAttribute> getAllConfigAttributes() {
  823. return null;
  824. }
  825. public boolean supports(Class<?> clazz) {
  826. return FilterInvocation.class.isAssignableFrom(clazz);
  827. }
  828. }
  829. ]]></programlisting> For more information, look at the code for
  830. <classname>DefaultFilterInvocationSecurityMetadataSource</classname>.
  831. </para>
  832. </answer>
  833. </qandaentry>
  834. <qandaentry xml:id="faq-ldap-authorities">
  835. <question>
  836. <para>How do I authenticate against LDAP but load user roles from a
  837. database?</para>
  838. </question>
  839. <answer>
  840. <para> The <classname>LdapAuthenticationProvider</classname> bean (which handles
  841. normal LDAP authentication in Spring Security) is configured with two
  842. separate strategy interfaces, one which performs the authenticatation
  843. and one which loads the user authorities, called
  844. <interfacename>LdapAuthenticator</interfacename> and
  845. <interfacename>LdapAuthoritiesPopulator</interfacename> respectively.
  846. The <classname>DefaultLdapAuthoritiesPopulator</classname> loads the
  847. user authorities from the LDAP directory and has various configuration
  848. parameters to allow you to specify how these should be retrieved. </para>
  849. <para> To use JDBC instead, you can implement the interface yourself, using
  850. whatever SQL is appropriate for your schema: <programlisting language="java"><![CDATA[
  851. public class MyAuthoritiesPopulator implements LdapAuthoritiesPopulator {
  852. @Autowired
  853. JdbcTemplate template;
  854. List<GrantedAuthority> getGrantedAuthorities(DirContextOperations userData, String username) {
  855. List<GrantedAuthority> = template.query("select role from roles where username = ?",
  856. new String[] {username},
  857. new RowMapper<GrantedAuthority>() {
  858. /**
  859. * We're assuming here that you're using the standard convention of using the role
  860. * prefix "ROLE_" to mark attributes which are supported by Spring Security's RoleVoter.
  861. */
  862. public GrantedAuthority mapRow(ResultSet rs, int rowNum) throws SQLException {
  863. return new GrantedAuthorityImpl("ROLE_" + rs.getString(1);
  864. }
  865. }
  866. }
  867. }
  868. ]]></programlisting> You would then add a bean of this type to your application context and inject
  869. it into the <code>LdapAuthenticationProvider</code>. This is covered in
  870. the section on configuring LDAP using explicit Spring beans in the LDAP
  871. chapter of the reference manual. Note that you can't use the namespace
  872. for configuration in this case. You should also consult the Javadoc for
  873. the relevant classes and interfaces. </para>
  874. </answer>
  875. </qandaentry>
  876. <qandaentry xml:id="faq-namespace-post-processor">
  877. <question>
  878. <para>I want to modify the property of a bean that is created by the
  879. namespace, but there is nothing in the schema to support it. What can I
  880. do short of abandoning namespace use?</para>
  881. </question>
  882. <answer>
  883. <para>The namespace functionality is intentionally limited, so it doesn't
  884. cover everything that you can do with plain beans. If you want to do
  885. something simple, like modify a bean, or inject a different dependency,
  886. you can do this by adding a
  887. <interfacename>BeanPostProcessor</interfacename> to your configuration.
  888. More information can be found in the <link
  889. xlink:href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#beans-factory-extension-bpp"
  890. >Spring Reference Manual</link>. In order to do this, you need to know a
  891. bit about which beans are created, so you should also read the blog
  892. article in the above question on <link
  893. linkend="faq-namespace-to-bean-mapping">how the namespace maps to
  894. Spring beans</link>. </para>
  895. <para> Normally, you would add the functionality you require to the
  896. <methodname>postProcessBeforeInitialization</methodname> method of
  897. <interfacename>BeanPostProcessor</interfacename>. Let's say that you
  898. want to customize the
  899. <interfacename>AuthenticationDetailsSource</interfacename> used by the
  900. <classname>UsernamePasswordAuthenticationFilter</classname>, (created by
  901. the <literal>form-login</literal> element). You want to extract a
  902. particular header called <literal>CUSTOM_HEADER</literal>from the
  903. request and make use of it while authenticating the user. The processor
  904. class would look like this: <programlisting language="java"><![CDATA[
  905. public class BeanPostProcessor implements BeanPostProcessor {
  906. public Object postProcessAfterInitialization(Object bean, String name) {
  907. if (bean instanceof UsernamePasswordAuthenticationFilter) {
  908. System.out.println("********* Post-processing " + name);
  909. ((UsernamePasswordAuthenticationFilter)bean).setAuthenticationDetailsSource(
  910. new AuthenticationDetailsSource() {
  911. public Object buildDetails(Object context) {
  912. return ((HttpServletRequest)context).getHeader("CUSTOM_HEADER");
  913. }
  914. });
  915. }
  916. return bean;
  917. }
  918. public Object postProcessBeforeInitialization(Object bean, String name) {
  919. return bean;
  920. }
  921. }
  922. ]]></programlisting> You would then register this bean in your application context. Spring will
  923. automatically invoke it on the beans defined in the application context.
  924. </para>
  925. </answer>
  926. </qandaentry>
  927. </qandadiv>
  928. </qandaset>
  929. </section>
  930. </article>