From 01308f830848709c03d4d17757234ad46b32ccce Mon Sep 17 00:00:00 2001 From: Luke Taylor Date: Mon, 24 May 2010 17:01:55 +0100 Subject: [PATCH] Added FAQ on using BeanPostProcessor to customize namespace-created beans. --- docs/faq/src/docbook/faq.xml | 51 ++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/docs/faq/src/docbook/faq.xml b/docs/faq/src/docbook/faq.xml index a4d350c5b7..cb7120ee4c 100644 --- a/docs/faq/src/docbook/faq.xml +++ b/docs/faq/src/docbook/faq.xml @@ -453,7 +453,7 @@ are identical from the server's perspective. This is a common question from GWT users. - + How do I access the user's IP Address (or other web-request data) in a UserDetailsService? @@ -482,7 +482,7 @@ You can't, since the UserDetailsService has no awareness of the servlet API. If you want to store custom user data, then you should customize the UserDetails object which is returned. This can then be accessed at any point, via the thread-local SecurityContextHolder. - A call to SecurityContextHolder.getContext().getAuthentication().getPrincipal() will return this custom + A call to SecurityContextHolder.getContext().getAuthentication().getPrincipal() will return this custom object. @@ -598,6 +598,53 @@ configuration in this case. You should also consult the Javadoc for the relevant classes and interfaces. + + I want to modify the property of a bean that is created by the namespace, but there is + nothing in the schema to support it. What can I do short of abandoning namespace use? + + The namespace functionality is intentionally limited, so it doesn't cover everything that you can + do with plain beans. If you want to do something simple, like modify a bean, or inject a different + dependency, you can do this by adding a BeanPostProcessor to your + configuration. More information can be found in the + Spring Reference Manual. + In order to do this, you need to know a bit about which beans are created, so you should also read the + blog article in the above question on how the + namespace maps to Spring beans. + + + Normally, you would add the functionality you require to the postProcessBeforeInitialization + method of BeanPostProcessor. Let's say that you want to customize the + AuthenticationDetailsSource used by the UsernamePasswordAuthenticationFilter, + (created by the form-login element). You want to extract a particular header called + CUSTOM_HEADERfrom the request and make use of it while authenticating the user. + The processor class would look like this: + + You would then register this bean in your application context. Spring will automatically invoke it on the + beans defined in the application context. + + + +